Lafoune
Lafoune

Reputation: 486

how to search from specifc data in laravel?

let's say i have this table in database...

enter image description here

So i want to search from specific data, for example

so I did it like this...

$schedule = DB::table('schedules')
                ->where('station_id', 1)
                ->where('schedule_number', 'like', '%'.$query.'%')
                ->orWhere('route_name', 'like', '%'.$query.'%')
                ->orWhere('user_first', 'like', '%'.$query.'%')
                ->orWhere('id', 'like', '%'.$query.'%')
                ->orWhere('created_at', 'like', '%'.$query.'%')
                ->get();

But this will search from all schedules

Upvotes: 0

Views: 37

Answers (1)

Ghiffari Assamar
Ghiffari Assamar

Reputation: 661

you can use advanced where here's documentation laravel docs

in your code it shouldbe like

  $schedule= DB::table('schedules')
                            ->where('station_id', 1)
                            ->where('schedule_number', 'like', '%'.$query.'%')
                            ->where(function($q)use($query){
                                $q->where('route_name', 'like', '%'.$query.'%')
                                ->orWhere('user_first', 'like', '%'.$query.'%')
                                ->orWhere('id', 'like', '%'.$query.'%')
                                ->orWhere('created_at', 'like', '%'.$query.'%');
                            })
                            ->get();

remember to pass the variable needed by place use($variable) like in my example you need to pass use($query)

Upvotes: 1

Related Questions