Lumen 5.5.2 및 illuminate/database v5.5.17 사용.Eloquent relationship은 null을 반환하지만 비슷한 것이 좋습니다.
나는 3 개의 모델이 설정되어 있습니다. 하나는 다른 모델 2에 속해 있습니다. 따라서 견적에는 면적과 저장소가 있습니다.
저장소와의 관계가 예상대로 작동하면 영역은 null을 반환합니다. 이 목적이 아니기 때문에, 예
$quoteModel = new Quote();
$quote = $quoteModel
->with('area')
->with('depot')
->where('id', '=', $id)
->first();
echo 'depot id : ' , $quote->depot->id , "<br>\n";
echo 'area id : ' , $quote->area->id , "<br>\n";
디포 ID가 에코 될 것이기
는 영역은 오류가 발생할 것이다.
모델 이름을 배열 ->with(['area', 'depot'])
으로 전달하거나 영역 요청 (두 가지 방법 모두)으로 문제가 해결되지 않습니다. 내가 Area.php에서 구문 분석 오류를 생성하는 경우
Quote.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Quote extends EloquentModel {
protected $table = 'quotes';
public function area() {
return $this->belongsTo('\App\Models\Area', 'area_id', 'id');
}
public function depot() {
return $this->belongsTo('\App\Models\Depot', 'depot_id', 'id');
}
}
Area.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Area extends EloquentModel {
protected $table = 'areas';
public $timestamps = false;
public $incrementing = false;
public function quotes() {
return $this->hasMany('\App\Models\Quote', 'area_id', 'id');
}
}
Depot.php
는namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Depot extends EloquentModel {
protected $table = 'depots';
public $timestamps = false;
public $incrementing = false;
public function quotes() {
return $this->hasMany('\App\Models\Quote', 'depot_id', 'id');
}
}
스크립트가 입증 실패 그것이 포함됩니다.
쿼리를 기록 할 수 있도록 수신기가 설정되어있어 정상적으로 표시됩니다.
select * from `quotes` where `id` = 99192 limit 1
select * from `areas` where `areas`.`id` in (072)
select * from `depots` where `depots`.`id` in (07)
수동으로 영역 쿼리를 실행하면 예상 한 행을 반환합니다.
지역 관계의 이름을 변경하려고 시도했지만 도움이되지 않습니다.
웅변적인 버전을 사용하고 계십니까? 그들을 연결하는 대신 -> ([ 'area', 'depot'])을 사용해 보시겠습니까? – matiit
버전 알림을 보내 주셔서 감사합니다. 그들은 글을 쓰는 시점에서 최신 버전입니다 (5.4에 있었고 업그레이드가 도움이되지 않았거나 다치게하지 못했습니다). - 제 질문을 업데이트했습니다. 슬프게도 배열 구문이 도움이되지 않았습니다 – CodeMonkey