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
Try returning that function call
return $this->callView($name,$date);
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?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community