vendredi 28 octobre 2016

Create nested OR condition using Laravel Eloquent

I want to create something like:

select * from documents where 'chapter'=3 and 'section'=4 and ('title' like '%abc% OR 'desc' like %abc%);

How can I do this using Eloquent? I've tried using Eloquent's whereOr method but that doesn't seem to do anything.

Some of what I've got:

    $docSearch = DB::table('documents');

    if ($chapter > 0) {
        $docSearch->where('chapter', $chapter);
    }

    $existingManagementArray = Management::getAllManagementIDsAsArray();
    if ($management_id > 0 && in_array($management_id, $existingManagementArray)) {
        $docSearch->where('management_id', $management_id);
    } else {
        $docSearch->whereIn('management_id', $existingManagementArray);
    }

    if (strlen($term) > 0) {
        //These don't seem to change the SQL
        $docSearch->whereOr('title', 'like', "%$term%"); 
        $docSearch->whereOr('desc', 'like', "%$term%");
    }

    DB::enableQueryLog();
    $allFoundDocs = $docSearch->get();
    dd(DB::getQueryLog());



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire