Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

untested, but you can try something like this

Series::where('series_id', '!=', 3)->products;
Last updated 2 years ago.
0
Series::where('series_id', '<>', 3)->products;

That should do it. If not:

Series::whereRaw('series_id <> 3')->products;
Last updated 2 years ago.
0

Both no, in Series there is no 'series_id', but 'id'
With this (my very first try)

Series::where('id', '!=', 3)->products;

I get

Undefined property: Illuminate\Database\Eloquent\Builder::$products

If everything was so simple, I would not ask this question :)

Last updated 2 years ago.
0

how about this?

Series::where('id', '!=', 3)->first()->products;
Last updated 2 years ago.
0

alainbelez said:

how about this?

Series::where('id', '!=', 3)->first()->products;

nope, this adds to the query

WHERE `product_series`.`series_id` = '1'
Last updated 2 years ago.
0

I really dont know how to write it with eloquent, probably would use

DB::table('products')->join('product_series', 'products.id', '=', 'product_series.product_id')->where('product_series.series_id', '!=', '3')->get();

If anyone would find solution, it would be great, the query isn't that hard.

Last updated 2 years ago.
0
Series::with('products')->where('id', '!=', 3)->get(); // eager loading

Series::where('id', '!=', 3)->get()->products; // lazy loading
Last updated 2 years ago.
0

zenry said:

Series::with('products')->where('id', '!=', 3)->get(); // eager loading

Series::where('id', '!=', 3)->get()->products; // lazy loading

Eager loading - your query:

SELECT `products`.*, `product_series`.`series_id` as `pivot_series_id`, `product_series`.`product_id` as `pivot_product_id` FROM `products` inner join `product_series` on `products`.`id` = `product_series`.`product_id` WHERE `product_series`.`series_id` in ('1', '2', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100', '101')

This is a very bad and slow query, should never use it in this case (imagine 1000 IN) ;)

The second query gives the same result as before.

Undefined property: Illuminate\Database\Eloquent\Collection::$products

So, still no solution with Eloquent :(

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.