Manas
Manas

Reputation: 3330

search function look for parameter's value in related table

I have 2 tables, merchants and transaction. merchants has (mid,name,age) and transaction has (id,mid,order_no,). I have displayed the data like (orderid,name) with eagerloading. now i have a search function on top where , i can search for all the orders by the merchant's name. how do i do that? im' trying something like this

  public function search($search){
      $query = Transaction::with('themerchant')
      ->join('merchants','transactions.mid', '=', 'merchants.mid' )
      ->select('merchants.name', '=' , '%'.$search.'%')->paginate(10);
      return $query;
  }

Upvotes: 0

Views: 32

Answers (1)

EddyTheDove
EddyTheDove

Reputation: 13259

Oh you are using eagerLoading Do this

public function search($search){
  return Transaction::with(['themerchant' => function($query){
      $query->where('name', 'like', '%'.$search.'%');
  }])->paginate(10);;

}

If you want an exact merchant match change the where like this

...
->where('name', '=' , $search)
...

If you want to use Join

public function search($search){
    return Transaction::join('merchants', 'merchants.mid', '=', 'transactions.mid')
    ->where('merchants.name', 'like', '%'.$search.'%')
    ->get();
}

Upvotes: 1

Related Questions