Laravel Nested Eager Загрузка как использовать eloquent там, где условие изнутри с вложенным - PullRequest
0 голосов
/ 24 декабря 2018

Я ищу способ использования условия where в моих красноречивых отношениях с нетерпением при загрузке.

Вот мой код

  $data = Model::with(['stage_chatbot'=> function($stage_chatbot){
     $stage_chatbot->select('id', 'customer_id','stage','created_at');
     $stage_chatbot->orderBy('id', 'asc');
  }])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc');

  $data->where('stage_chatbot.stage', 'like', 'sampleValue')->get();
  return $data;

Спасибо!

Ответы [ 3 ]

0 голосов
/ 24 декабря 2018

Вы можете использовать красноречивый метод whereHas () следующим образом.

$data = Model::with(['stage_chatbot'=> function($stage_chatbot){
    $stage_chatbot->select('id', 'customer_id','stage','created_at');
    $stage_chatbot->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc');

$data->whereHas('stage_chatbot', function ($stage_chatbot) {
   $stage_chatbot->where('stage', 'like', 'sampleValue')
})->get();
return $data;
0 голосов
/ 24 декабря 2018

Определите переменную в поле или поле ввода для поиска и передайте его через , используйте в запросе модели

Попробуйте это

$search = $request->search;
$data = Model::with(['stage_chatbot'=> function($stage_chatbot) use ($search) {
    $stage_chatbot->select('id', 'customer_id','stage','created_at');
    $stage_chatbot->where('stage', 'like',$searchText);
    $stage_chatbot->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc')->get();
0 голосов
/ 24 декабря 2018

попробуйте это

, используя ключевое слово use в laravel

$searchText = "sampleValue";
$data = Model::with(['stage_chatbot'=> function($stage_chatbot) use ($searchText) {
        $stage_chatbot->select('id', 'customer_id','stage','created_at')
        ->where('stage_chatbot.stage', 'like', "%".$searchText."%")
        ->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc')->get();

для получения дополнительной информации о том, как использовать ключевое слово use в анонимной функции см.

...