Home > Mobile >  Search in DB in Laravel 8
Search in DB in Laravel 8

Time:12-17

I am beginner in Laravel. I make my first project in Laravel. I use Laravel 8 in my project.

I have this code:

$query = Immovable::query()
            ->leftJoin('streets', 'streets.gus_id', '=', 'immovables.street_gus_id')
            ->select('immovables.id',
                'immovables.street_gus_id',
                'immovables.building_number',
                'immovables.apartment_number',
                'streets.name as street_name'
            );
if (request()->has('search')) {
                        $query->where('streets.name', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.community', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.city', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.building_number', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.granted_comments', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.inspections', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.oze_installations', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.pesel', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.name', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.surname', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.email1', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.email2', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.email3', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.phone1', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.phone2', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.phone3', 'like', "%" . $request->search['value'] . "%");
                        $query->orWhere('immovables.description', 'like', "%" . $request->search['value'] . "%");
                    }
$query->get();

It's work fine.

I have problem when I try find user in name and surname at one time. For example when I write: Karol - I have result. When I write Krawczyk - I have result. When I write: Karol Krawczyk - I haven't result

How can I repair it?

CodePudding user response:

One way is to break your search value by delimiters (in this case, a space may be the suitable one), and loop for the orWhere part

$query = Immovable::query()
    ->leftJoin('streets', 'streets.gus_id', '=', 'immovables.street_gus_id')
    ->select('immovables.id',
        'immovables.street_gus_id',
        'immovables.building_number',
        'immovables.apartment_number',
        'streets.name as street_name'
    );
if (request()->has('search')) {
    $searches = explode(" ", $request->search['value']);
    foreach ($searches as $index => $search) {
        if ($index === 0) {
            $query->where('streets.name', 'like', "%" . $search . "%");
        } else {
            $query->orWhere('streets.name', 'like', "%" . $search . "%");
        }
        $query->orWhere('immovables.community', 'like', "%" . $search . "%");
        $query->orWhere('immovables.city', 'like', "%" . $search . "%");
        $query->orWhere('immovables.building_number', 'like', "%" . $search . "%");
        $query->orWhere('immovables.granted_comments', 'like', "%" . $search . "%");
        $query->orWhere('immovables.inspections', 'like', "%" . $search . "%");
        $query->orWhere('immovables.oze_installations', 'like', "%" . $search . "%");
        $query->orWhere('immovables.pesel', 'like', "%" . $search . "%");
        $query->orWhere('immovables.name', 'like', "%" . $search . "%");
        $query->orWhere('immovables.surname', 'like', "%" . $search . "%");
        $query->orWhere('immovables.email1', 'like', "%" . $search . "%");
        $query->orWhere('immovables.email2', 'like', "%" . $search . "%");
        $query->orWhere('immovables.email3', 'like', "%" . $search . "%");
        $query->orWhere('immovables.phone1', 'like', "%" . $search . "%");
        $query->orWhere('immovables.phone2', 'like', "%" . $search . "%");
        $query->orWhere('immovables.phone3', 'like', "%" . $search . "%");
        $query->orWhere('immovables.description', 'like', "%" . $search . "%");
    }
}
$query->get();

Let me know if this works for you. Thanks!

  • Related