Home > Mobile >  Search Laravel Using Button
Search Laravel Using Button

Time:10-31

I want to search for data using a primary key, with PO as an example. Btw, I'm new to Laravel. Below is my code for my controller. I want to make the system go to the new page if the data that was searched exists(click on search button). If not, it will stay on the same page. Actually, I don't know whether my code is correct or not.

public function supplierindex(){

        $supp_details = Supplier::where('PO','LIKE','%'.$searchPO.'%')->get();
        return view ('frontend.praiBarcode.getweight')
           ->with('supp_details',$supp_details);

  }

    public function searchindex()
    {
      return view ('frontend.praiBarcode.getweight');
    }

    public function searchPO()
    {
      $searchPO = Supplier::where('PO','like',"%".$search."%")->get();

      if (Supplier::where('PO','like',"%".$search."%")->exists()) {

      return view('frontend.praiBarcode.getweight',compact('searchPO')); }
      
      else {
        
        return view('frontend.praiBarcode.index');
      }

    }

Below is my code in blade.php. However, the data does not come out on the screen.

      <div class= "form-group">  
@foreach ($supp_details as s)
      <div style="font-size: 16px;" >
                <label for="supp_name" class = "col-sm-2">PO</label>
                <label for="supp_name" class = "col-sm-1">:</label>
                <div >
                    <label> {{ $s->PO }}</label>
                </div>
        </div> 
@endforeach

CodePudding user response:

In order to pass data into the view, you have to use the second argument of the view function.

Example:

public function supplierindex(){

        $supp_details = Supplier::where('PO','LIKE','%'.$searchPO.'%')->get();
        return view ('frontend.praiBarcode.getweight' ,['supp_details' => $supp_details]);

  }

CodePudding user response:

You can use count() to check if query result is empty or not

public function searchPO()
{
  $searchPO = Supplier::where('PO','like',"%".$search."%")->get();
  $countsearchPO = $searchPO->count();
  if ($countsearchPO ) {

  return view('frontend.praiBarcode.getweight',compact('searchPO')); }
  
  else {
    
    return view('frontend.praiBarcode.index');
  }

}

And in your blade your variable is stored in session

$supp_details = Session::get('supp_details');
@foreach ($supp_details as s)
 <div style="font-size: 16px;" >
            <label for="supp_name" class = "col-sm-2">PO</label>
            <label for="supp_name" class = "col-sm-1">:</label>
            <div >
                <label> {{ $s->PO }}</label>
            </div>
    </div>
@endforeach
  • Related