Я пытаюсь загрузить параметр загрузки в красноречивом Laravel 5.8 - PullRequest
0 голосов
/ 20 октября 2019

Я пытаюсь загрузить данные из отношения «многие ко многим» в красноречивом laravel 5.8. Приведенный ниже код работал в laravel 5.5, но теперь он не работает в моей новой версии, т.е. 5.8

$data = Recruitment::find($id);

$candidates = $data->candidates()
    ->with([
        'status'=> function ($query) use ($id)
        {
            return  $query->where('recruitment_id', $id);
        },
        'interviews'=> function ($query) use ($id)
        {
            return  $query->where('recruitment_id', $id);
        },
        'contracts'=> function ($query) use ($id)
        {
            return  $query->where('recruitment_id', $id);
        }
        ...
        ...
        ...
    ])->get();

Мое отношение таково:

В Recruitment.php

public function candidates()
{
    return $this->belongsToMany(Candidate::class)->withPivot('status');
}

В Candidate.php

public function status()
{
    return $this->belongsToMany(Recruitment::class, 'candidate_recruitment')
        ->select(['candidate_id','status']);
}

Результат в dump :

#relations: array:1 [
    "status" => MediableCollection {
    #items: []
}

даетмне пустые результаты. Я что-то упустил в новой версии. Заранее благодарен за вашу помощь и извините за мой английский

Следующее также не работает с использованием whereHas:

$candidates = Recruitment::
        with('candidates.status')
        ->whereHas('candidates.status', function ($query) use ($id){
            $query->where('recruitment_id', $id);

         })
         ->find($id);

ОБНОВЛЕНО структурами таблицы:

таблица кандидатов-кандидатов

id | candidate_id  | recruitment_id  | status         
---------------------------------------------
 19 | 33           | 13              | 0
 20 | 30           | 13              | 0
 21 | 31           | 13              | 0

таблица кандидатов

id  | employee_no | name 
--------------------------
30  | 001         | john
31  | 002         | mike
32  | 003         | paul

таблица приема на работу

id | department_id| employee_status_id  | qualification_id
----------------------------------------------------------
 13| 1            | 1                   | 6

ОБНОВЛЕНИЯ

Это вывод из enableQueryLog для laravel 5.5

array:11 [▼
  0 => array:3 [▼
    "query" => "select *, CONCAT(first_name, " ", surname) AS name, `candidates`.*, `candidate_recruitment`.`recruitment_id` as `pivot_recruitment_id`, `candidate_recruitment`. ▶"
    "bindings" => array:1 [▼
      0 => 13
    ]
    "time" => 1.64
  ]
  1 => array:3 [▼
    "query" => "select `media`.*, `mediables`.`mediable_id` as `pivot_mediable_id`, `mediables`.`media_id` as `pivot_media_id`, `mediables`.`tag` as `pivot_tag`, `mediables`.`o ▶"
    "bindings" => array:5 [▼
      0 => 30
      1 => 31
      2 => 32
      3 => 33
      4 => "App\Candidate"
    ]
    "time" => 1.72
  ]
  2 => array:3 [▼
    "query" => "select `job_titles`.`id`, `job_titles`.`description` from `job_titles` where `job_titles`.`id` in (?) and `job_titles`.`deleted_at` is null"
    "bindings" => array:1 [▼
      0 => null
    ]
    "time" => 0.47
  ]
  3 => array:3 [▼
    "query" => "select * from `candidate_previous_employments` where `candidate_previous_employments`.`candidate_id` in (?, ?, ?, ?) and `candidate_previous_employments`.`delet ▶"
    "bindings" => array:4 [▼
      0 => 30
      1 => 31
      2 => 32
      3 => 33
    ]
    "time" => 0.52
  ]
  4 => array:3 [▶]
  5 => array:3 [▼
    "query" => "select `candidate_id`, `status`, `candidate_recruitment`.`candidate_id` as `pivot_candidate_id`, `candidate_recruitment`.`recruitment_id` as `pivot_recruitment_ ▶"
    "bindings" => array:5 [▼
      0 => 30
      1 => 31
      2 => 32
      3 => 33
      4 => "13"
    ]
    "time" => 0.6
  ]
  6 => array:3 [▼
    "query" => "select `interviews`.*, `candidate_interview_recruitment`.`candidate_id` as `pivot_candidate_id`, `candidate_interview_recruitment`.`interview_id` as `pivot_inte ▶"
    "bindings" => array:5 [▼
      0 => 30
      1 => 31
      2 => 32
      3 => 33
      4 => "13"
    ]
    "time" => 1.0
  ]
  7 => array:3 [▼
    "query" => "select `media`.*, `mediables`.`mediable_id` as `pivot_mediable_id`, `mediables`.`media_id` as `pivot_media_id`, `mediables`.`tag` as `pivot_tag`, `mediables`.`o ▶"
    "bindings" => array:2 [▼
      0 => 5
      1 => "App\Interview"
    ]
    "time" => 0.64
  ]
  8 => array:3 [▼
    "query" => "select `candidate_id`, `comment`, `status`, `recruitment_status`.`candidate_id` as `pivot_candidate_id`, `recruitment_status`.`recruitment_id` as `pivot_recruit ▶"
    "bindings" => array:5 [▼
      0 => 30
      1 => 31
      2 => 32
      3 => 33
      4 => "13"
    ]
    "time" => 0.92
  ]
  9 => array:3 [▼
    "query" => "select `candidate_id`, `contract_id`, `start_date`, `signed_on`, `comments`, `contract_recruitment`.`candidate_id` as `pivot_candidate_id`, `contract_recruitmen ▶"
    "bindings" => array:5 [▼
      0 => 30
      1 => 31
      2 => 32
      3 => 33
      4 => "13"
    ]
    "time" => 0.55
  ]
  10 => array:3 [▼
    "query" => "select `candidate_id`, `offer_id`, `starting_on`, `signed_on`, `comments`, `offer_recruitment`.`candidate_id` as `pivot_candidate_id`, `offer_recruitment`.`offe ▶"
    "bindings" => array:5 [▼
      0 => 30
      1 => 31
      2 => 32
      3 => 33
      4 => "13"
    ]
    "time" => 0.56
  ]
]

Это вывод из enableQueryLog для laravel 5.8

array:10 [▼
  0 => array:3 [▼
    "query" => "select *, CONCAT(first_name, " ", surname) AS name, `candidate_recruitment`.`recruitment_id` as `pivot_recruitment_id`, `candidate_recruitment`.`candidate_id` a ▶"
    "bindings" => array:1 [▼
      0 => 13
    ]
    "time" => 1.3
  ]
  1 => array:3 [▼
    "query" => "select `media`.*, `mediables`.`mediable_id` as `pivot_mediable_id`, `mediables`.`media_id` as `pivot_media_id`, `mediables`.`mediable_type` as `pivot_mediable_t ▶"
    "bindings" => array:1 [▼
      0 => "App\Candidate"
    ]
    "time" => 0.76
  ]
  2 => array:3 [▼
    "query" => "select `job_titles`.`id`, `job_titles`.`description` from `job_titles` where 0 = 1 and `job_titles`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.49
  ]
  3 => array:3 [▼
    "query" => "select * from `candidate_previous_employments` where `candidate_previous_employments`.`candidate_id` in (19, 20, 21, 22) and `candidate_previous_employments`.`d ▶"
    "bindings" => []
    "time" => 0.56
  ]
  4 => array:3 [▼
    "query" => "select * from `candidate_qualifications` where `candidate_qualifications`.`candidate_id` in (19, 20, 21, 22) and `candidate_qualifications`.`deleted_at` is null"
    "bindings" => []
    "time" => 0.53
  ]
  5 => array:3 [▼
    "query" => "select `candidate_id`, `status`, `candidate_recruitment`.`candidate_id` as `pivot_candidate_id`, `candidate_recruitment`.`recruitment_id` as `pivot_recruitment_ ▶"
    "bindings" => array:1 [▼
      0 => "13"
    ]
    "time" => 0.68
  ]
  6 => array:3 [▼
    "query" => "select `interviews`.*, `candidate_interview_recruitment`.`candidate_id` as `pivot_candidate_id`, `candidate_interview_recruitment`.`interview_id` as `pivot_inte ▶"
    "bindings" => array:1 [▼
      0 => "13"
    ]
    "time" => 0.95
  ]
  7 => array:3 [▼
    "query" => "select `candidate_id`, `comment`, `status`, `recruitment_status`.`candidate_id` as `pivot_candidate_id`, `recruitment_status`.`recruitment_id` as `pivot_recruit ▶"
    "bindings" => array:1 [▼
      0 => "13"
    ]
    "time" => 0.64
  ]
  8 => array:3 [▼
    "query" => "select `candidate_id`, `contract_id`, `start_date`, `signed_on`, `comments`, `contract_recruitment`.`candidate_id` as `pivot_candidate_id`, `contract_recruitmen ▶"
    "bindings" => array:1 [▼
      0 => "13"
    ]
    "time" => 0.6
  ]
  9 => array:3 [▼
    "query" => "select `candidate_id`, `offer_id`, `starting_on`, `signed_on`, `comments`, `offer_recruitment`.`candidate_id` as `pivot_candidate_id`, `offer_recruitment`.`offe ▶"
    "bindings" => array:1 [▼
      0 => "13"
    ]
    "time" => 0.89
  ]
]

отсутствует идентификатор кандидата ...

Я получаю идентификатор сводной таблицы вместо кандидата_ид ... Любая идея, где эта проблема может быть спасибо...

...