Before looking for a page I wanted to check if the id exists, so if I don’t find it, give up looking I tried as follows:
My controller product
public function search(Request $request)
{
$id = $request->input('id');
if($produto = Produto::find($id)) {
return view('produtos.show', compact('produto', 'id'));
}
// $search_results=Produto::findOrFail($id);
return 'Not found';
}
->My Route->
Route::get('/produtos/editar/{id?}','AppHttpControllersProdutosController@search')->name('searchproduct');
->My Blade Form
<form id="search" method="GET" action="{{ route('searchproduct') }}" >
<input id="q" name="q" type="text" /></br>
<button type="submit" id="submitButton" >Alterar</button>
</form>
</div>
</div>
</div>
</div>
->My Jquery Script
jQuery(document).ready(function(){
jQuery("form#search").on('submit',function(e){
e.preventDefault();
var q = jQuery("#q").val();
window.location.href = jQuery(this).prop('action')+"/" + encodeURIComponent(q)
});
});
How can i check in database before? send It’s always going to the default 404 page
4
Answers
It’s enough to check $search_results variable. I changed findOrFail with find because findOrFail may throw an error.
to show a 404 page use this code :
or you can use this code too to use a custom return
-> your route must be get not post
-> no need for @csrf for get method
Also yo can use:
Two ways to go about it:
exists:
findOr: