ArneDB
ArneDB

Reputation: 33

Laravel where('user_id') seems completely ignored

I have a problem where user_id seems completely ignored by Laravels Eloquent ORM.

Pigeons Table
id user_id name father_id mother_id ringnumber gender color created_at updated_at landcode
(these are my columns (if someone knows how to format this better, let me know))

I have a search from which routes a search parameter q to my SearchController.php in which this function lives:

namespace App\Http\Controllers;

use App\Pigeon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class SearchController extends Controller
{
    public function index()
    {
        $q = Input::get('query');

        $userId = Auth::user()->id;

        $pigeons = Pigeon::where([
                ['user_id', '=', $userId],
                ['name','LIKE','%'.$q.'%']
            ])
            ->orWhere('ringnumber','LIKE','%'.$q.'%')
            ->sortable()
            ->paginate(15);
        dd($pigeons);

        return view('backend.pigeon.pigeonlist')->with('pigeons', $pigeons);
    }
}

For some reason this Eloquent query builder completely seems to ignore 'user_id', '=', $userId and this is an important part because I only want to search for pigeons for the current logged in user.

Below is a result of such a query, the problem is that there are pigeons with all kinds of user_id and not only the one user that searched for them.

LengthAwarePaginator {#259 ▼
    #total: 150
    #lastPage: 10
    #items: Collection {#267 ▼
      #items: array:15 [▼
        0 => Pigeon {#268 ▶}
        1 => Pigeon {#269 ▶}
        2 => Pigeon {#270 ▶}
        3 => Pigeon {#271 ▶}
        4 => Pigeon {#272 ▶}
        5 => Pigeon {#273 ▶}
        6 => Pigeon {#274 ▶}
        7 => Pigeon {#275 ▶}
        8 => Pigeon {#276 ▶}
        9 => Pigeon {#277 ▶}
        10 => Pigeon {#278 ▶}
        11 => Pigeon {#279 ▶}
        12 => Pigeon {#280 ▶}
        13 => Pigeon {#281 ▶}
        14 => Pigeon {#282 ▶}
      ]
    }
    #perPage: 15
    #currentPage: 1
    #path: "http://mywebsite.test/pigeon/search"
    #query: []
    #fragment: null
    #pageName: "page"
    +onEachSide: 3
  }

Small note, I got some of my information from here: How to create multiple where clause query using Laravel Eloquent?

Problem solved: First I had an orWhere which overruled the where so that was pretty stupid of me. Second my real problem was that I was trying to get only records for the current logged in user that works via this code:

$pigeons = Pigeon::where('user_id', \Auth::id())
                ->where(function($query) use ($q) {
                    $query->where('name', 'LIKE', '%'. $q .'%');
                })
                ->sortable()
                ->paginate(15);

Upvotes: 0

Views: 65

Answers (1)

Elias Soares
Elias Soares

Reputation: 10264

That's the right behavior since you are adding an orWhere clause after the where.

This will result in a query like this:

SELECT * FROM your_table WHERE (user_id = xxx) OR (some condition that results true)

Since false OR true is equal to true, the first clause is being ignored (because the second is true)

Upvotes: 1

Related Questions