jeudi 26 octobre 2017

Search Data using Models in Laravel 5.4.36

I have created a search filter. Basically I'm getting records from single table so it works fine. But when i try to get data from another table, it is not correct. This is my drivers_table ( driver_id is PK )

Drivers Table

I'm using this query..

$drivers = Drivers::where('city', 'like', "$keywords[0]%")
            ->Where('first_name', 'like', "$keywords[1]%")
            ->get();

I have two dropdowns, Lets suppose I have 50 drivers, When i select City "DOHA" from first dropdown, Using ajax it gets me records of 20 drivers out of 50, and then i select first_name "ASFAND" from 2nd dropdown, it gets me records of people whose first name is Asfand from that list of 20 drivers. There was 5 people whose name was Asfand and also they were from DOHA City ( because i have selected both DOHA as city from first dropdown and first_name as Asfand from second drop down.

Everything works perfect. But now my question is I have a 3rd dropdown from which i will select Bank name, but bank name is not in the Drivers table, There is another table.

Banks (id, driver_id, bank_name).. Bank Table

How can i get records now?

$drivers = DB::table('drivers')
            ->join('bank','bank.driver_id','=','drivers.driver_id')
            ->where('city', 'like', "$keywords[0]%")
            ->Where('first_name', 'like', "$keywords[1]%")
            ->orWhere('bank.bank_name', 'like', "$keywords[3]%")
            ->get();

Now if i select doha from first dropdown it doesnot show me any record.. if i select bank name from 3rd dropdown then it shows me record.. I want dropdowns to be independent..

If i don't select anything from 3rd dropdown, it should still show what i selected from first two dropdowns.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2xpReAS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire