Support the ongoing development of Laravel.io →
posted 8 years ago
Views

Hi. I am trying to return a view in a different method of the class instead of the standard resource controller methods (i.e, my own function instead of index).

Controller:

class TestController extends Controller
{
    public function callView($name, $date)
    {
      //dd($name, $date);  //this works. I.e., control reaches here & dumps the values.
      return view('test.index')->with('name', $name)->with('date', $date);
    }
    public function index()
    {
        $name = 'John';
        $date = \Carbon\Carbon::now();
        //return view('test.index')->with('name', $name)->with('date', $date); //call a function which returns the view, instead of returning view directly
        $this->callView($name,$date);
    }

View:

@extends('vendor.layout')

@section('content')
<div class="container">
<br><br><br>
<h1>hello {{ $name }}</h1>
<h3>{{ $date }}</h3>
</div>
@endsection

I end up with a blank screen in the browser in the above code sample (and commented dd line dumps the values correctly when enabled in the callView() method to confirm that data is alright before invoking return view()). However, "return view()" works perfectly when called from within the index() method of the controller.

I am relatively new to laravel. This behavior is stumping me out. I could not find out much in the documentation & hence asking for support from the Gurus here. Thanks

Last updated 3 years ago.
0

Try returning that function call

return $this->callView($name,$date);
0

laracademy said:

Try returning that function call

return $this->callView($name,$date);

Edit: worked. Thanks. Will be great if you could explain the reasoning behind it. Closure issue?

Last updated 8 years ago.
0

try return $this->callView($name,$date);

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

prepzone prepzone Joined 22 Oct 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.