skip to Main Content

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


  1. It’s enough to check $search_results variable. I changed findOrFail with find because findOrFail may throw an error.

    public function search(Request $request) {
      $search_results = Produto::find($request->input('id'));
    
      if ($search_results == NULL) {
        abort(404);
      }
    
      return view('produtos.show', ['produto' => $search_results, 'id' => $request->input('id')]);
    }
    
    Login or Signup to reply.
  2. to show a 404 page use this code :

    public function search(Request $request)
    {   
        //if not found it will trigger 404 not found page
        $produto = Produto::findOrFail($id = $request->input('id'));
    
        //otherwise it will return the view of produtos.show
        return view('produtos.show', compact('produto', 'id'));
    }
    

    or you can use this code too to use a custom return

    public function search(Request $request)
    {   
        $id = $request->input('id');
        if($produto = Produto::find($id)) {
           return view('produtos.show', compact('produto', 'id'));
        }
    
        //otherwise return your error view or message
        return 'Not found';
         
    }
    

    -> your route must be get not post

    Route::get('/produtos/editar/{id?}','ProdutosController@search')->name('searchproduct');
    

    -> no need for @csrf for get method

    <form id="search" method="GET" action="{{ route('searchproduct') }}" >
        <input id="q" type="text" /></br>
        <button type="submit" id="submitButton" >Alterar</button>
    </form>
    
    Login or Signup to reply.
  3. Also yo can use:

    public function search(Request $request) {
      $search_results = Produto::where('id', '=', $request->input('id'))->first();
    
      if ($search_results == NULL) {
        abort(404);
      }
    
      return view('produtos.show', ['produto' => $search_results, 'id' => $request->input('id')]);
    }
    
    Login or Signup to reply.
  4. Two ways to go about it:

    exists:

    if (Produto::where('id', $id)->exists()) {
        $search_results=Produto::find($id);
    }
    

    findOr:

    $search_results = Produto::findOr($id, function () {
        // whatever you want to do if no record is found
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search