Laravel where('user_id') seems completely ignored

Posted 2 months ago by ArneDB

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: https://stackoverflow.com/questions/19325312/how-to-create-multiple-where-clause-query-using-laravel-eloquent

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.