How can I make my view profile.blade.php which is controlled by controller FreelancerController.php, access an already existing function called public function show which is in the controller ApplicantController.php?
Which are the best methods to achieve this and solve the error (ErrorException (E_ERROR) Undefined variable: job)?
View (profile.blade.php):
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8 my-5">
<div class="card-header">
<a href="{{url("/job/application/$job->id")}}"><button class="btn btn-primary btn-block"> Next </button></a>
</div>
</div>
</div>
</div>
Controller (ApplicantController.php):
public function show($id) {
$job = Job::find($id);
return view('jobpost.application')->withJob($job);
}
CodePudding user response:
The view
helper accepts data as the second parameters. Pass the job there.
view('jobpost.application', ['job' => $job])
CodePudding user response:
You pass your variable with compact
method also
return view('jobpost.application',compact('job'));
CodePudding user response:
Why use the same controller method to return two different views? It's better to keep these things separated IMO.
But if you really want to do it like that you can check the route and use it as a condition on what view to return.
https://laravel.com/docs/8.x/routing#accessing-the-current-route
Something like this should work:
$job = Job::find($id);
if (Route::currentRouteName() === 'your route name') {
$view = 'jobpost.application';
} elseif (Route::currentRouteName() === 'your other route name') {
$view = 'jobpost.profile';
}
return view($view, ['job' => $job]);
And don't forget on use Illuminate\Support\Facades\Route;