frankosa
frankosa

Reputation: 15

Laravel 8 Two Table Where Search

My Code;

$posts=Term::where('user_id',$user_id)->where('status',1)->where('type','product')->with('preview','attributes','category','price','options','stock','affiliate')->withCount('reviews');

       if(!empty($request->term)){
            $data= $posts->where('title','LIKE','%'.$request->term.'%');
       }

This my code searches for title from term table. But I want to search from another table without breaking the structure. So Example;

 if(!empty($request->term)){

    $data= $termTABLE->where('title','LIKE','%'.$request->term.'%');
    $data= $stockTABLE->where('code','LIKE','%'.$request->term.'%');
    
}

Since I don't know about Laravel, I couldn't explain it fully. I hope I get help. Thanks.

Good Luck

Upvotes: 0

Views: 450

Answers (1)

MeGaZ
MeGaZ

Reputation: 74

The below example is not like yours' but you can sort it out according to your need.

For example, if user has a company you can search in the company table like

          $user->where('name', 'LIKE', '%' . $searchPhrase . '%') //Searching in user table
                //Searching in user table
               ->orWhere('email', 'LIKE', '%' . $searchPhrase . '%')
                //Checking if company exist
               ->orWhereHas('company', function ($query) use ($searchPhrase) { 
                    //Searching in company table
                    $query->where('name', 'LIKE', '%' . $searchPhrase . '%'); 
               });

Upvotes: 1

Related Questions