Home > Net >  How check if id exist in database before return?
How check if id exist in database before return?

Time:11-03

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?}','App\Http\Controllers\ProdutosController@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

CodePudding user response:

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')]);
}

CodePudding user response:

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')]);
}

CodePudding user response:

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
});

CodePudding user response:

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>

CodePudding user response:

You can use exists method.

  if (Produto::where('id', $id)->exists()) {
        // exists
    }

I'm not sure about your case but where exists is very helpful. Here is the documentation: https://laravel.com/docs/9.x/queries#where-exists-clauses

  • Related