I did a search and I want to display the result, but I cannot convey the variable to the view, although I specify it in the controller.
My piece of view code:
<div class="search col-md-6">
<p>
Найти сотрудника по id
</p>
<form action="{{route('searchID')}}" class="search-id" method="GET">
<div class="row">
<div class="col-xs-10">
<div class="form-group">
<input class="form-control" name="id" required="" type="text" value="{{ old('id') }}">
</input>
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
<input class="btn btn-info" type="submit" value="Искать">
</input>
</div>
</div>
</div>
{{$result}}
</form>
<div>
</div>
</div>
my route:
Route::match(['get', 'post'], 'searchID', 'SearchController@indexID')->name('searchID');
my method in the controller:
public function indexID(Request $request, View $view)
{
//$message= "Сотрудник не найден";
$id = $request->input('id');
dump($id);
$result = Staff::where('public_id', $id)->get();
if ($result == null) {
//dump($message);
return redirect()->back()->withInput($id);
} else {
dump($result);
return view('addworker')->with('result', $result);
}
}
But I constantly get an error: Undefined variable: result
I tried:
return view('addworker')->with($result);
and
return view('addworker',$result);
and
return view('addworker', ['result', $result]);
None of this helped me, I don't know what to do anymore
How to make the template access this variable only after the controller has been processed?
You can use compact for the same,
return view('addworker', compact('result'));
compact — Create array containing variables and their values
我希望这能帮到您
return view('addworker', ['result' => $result]);
You used the wrong syntax to send your variable to your view, there is a lot os ways to do that:
You could use the compact function:
return view('addworker', compact('result'));
You could use the with() method:
return view('addworker')->with('result', $result);
Or:
return view('addworker', ['result' => $result]);
You could also check the official documentation: click here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.