2017-01-30 1 views
0

У меня есть эта схемаПреобразование исходных запросов в Laravel

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 

Я создал запрос в 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;

Этот запрос фактически получает все эти категории из таблицы product_categories, которые нет в store_product_categories.

Теперь я действительно очень смущен, как построить это Laravel Eloq ..

я попробовать это.

$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(); 

Но это не дает мне результат

ответ

0
$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.*']); 
0
$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

+0

Не работает! Внимательно проверьте запрос! Мой запрос – Alex

+0

Вы пытались выполнить запрос? Какие данные были возвращены? $ exclusive_categories = PRODUCT_CATEGORY :: LeftJoin ('store_product_categories', 'store_product_categories.category_id', '=', '') product_categories.id -> где ('store_product_categories.store_id', '=', сессия ('store_id ')) -> гдеNull (' store_product_categories.category_id ') -> get(); –

1

Поскольку вы присоединяетесь на двух разных колонках, вам нужно передать, что через функции/закрытия:

$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(); 

Я не уверен, что это то, чего вы хотите. Если вы ищете, где store_id NULL ИЛИ store_id = идентификатор сеанса, вы можете передать это через другое закрытие/функцию.

+0

Я получаю эту странную ошибку – Alex

+0

Столбец не найден: 1054 Неизвестный столбец '1' в 'on clause' (SQL: select * from 'product_categories' left join' store_product_categories' на 'store_product_categories'.'category_id' =' product_categories'. 'id' и' store_product_categories' .'store_id' = '1' где' store_product_categories' .'category_id' = NULL) – Alex

+0

Проверьте мой ответ ниже –