/ / Konwersja surowych zapytań na laravel - php, laravel, laravel-5.3

Konwersja surowych zapytań do laravel - php, laravel, laravel-5.3

Mam ten schemat

product_categories

id | product_category
---------------------
1  | ABC
2  | DBC
3  | EBA

store_product_categories

id | category_id | store_id
------------------------
1  | 2        | 11
2  | 1        | 11
3  | 3        | 11

Utworzyłem zapytanie w ławce roboczej mysql

SELECT pc.* FROM product_categories pc LEFT JOIN store_product_categories spc ON pc.category = pc.id AND spc.store_id = 11 WHERE spc.category IS NULL;

To zapytanie faktycznie pobiera wszystkie te kategorie product_categories tabela, której nie ma w store_product_categories.

Teraz jestem naprawdę bardzo zdezorientowany, jak to zbudować, to Laravel Eloq ..

Próbowałem tego.

$exclusive_categories = Product_category::join("store_product_categories","store_product_categories.category_id","=","product_categories.id")
->where("store_product_categories.store_id","=",session("store_id"))
->where("store_product_categories.category_id","=","NULL")->get();

Ale to nie daje mi rezultatu

Odpowiedzi:

0 dla odpowiedzi № 1
$exclusive_categories = Product_category::leftJoin("store_product_categories spc", function ($join) {
$join->on("spc.category_id", "=", "product_categories.id");
$join->on("spc.store_id", "=", DB::raw(session("store_id")));
})
->whereNull("spc.category_id")
->get(["product_categories.*"]);

1 dla odpowiedzi nr 2

Ponieważ łączysz się w dwóch różnych kolumnach, musisz przekazać to przez funkcję / zamknięcie:

$exclusive_categories = Product_category::leftJoin("store_product_categories", function($join) {
$join->on("store_product_categories.category_id","=","product_categories.id")
->on("store_product_categories.store_id","=",session("store_id"));
})
->where("store_product_categories.store_id","=","NULL")->get();

Nie jestem pewien, czy to jest dokładnie to, czego chcesz. Jeśli szukasz gdzie ID_sklepu to NULL LUB ID_sklepu = identyfikator sesji, możesz przekazać to przez inne zamknięcie / funkcję.


0 dla odpowiedzi № 3
$exclusive_categories = Product_category::leftJoin("store_product_categories","store_product_categories.category_id","=","product_categories.id")
->where("store_product_categories.store_id","=",session("store_id"))
->whereNull("store_product_categories.store_id")->get();

https://laravel.com/docs/5.3/queries