I want to implement a search option for some data in my Laravel app, but I have some problems with this:
public function index()
{
$students= Student::all(); // When I use this form it appear all of Students even I put a name in Search option
$students= new Student;//When I use this form I have this error: Trying to get property 'name' of non-object ...
if (request()->filled('name')) {
$students->where('name', '=', request()->input('name'));
}
else{
$students=Student::all();
}
return view('students.index',['students'=>$students]);
}
And my view is:
<form action="{{action('StudentController@index')}}" class="d-flex w-100">
<div class="col-4 p-0">
<input class="form-control" type="text" name="name" id="name" placeholder="name">
</div>
</form>
When I try to display students' names, I get the error "Trying to get property 'name' of non-object"