laravel Eloquent 多个外键来自同一表的两列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40168861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eloquent Multiple Foreign Keys from same table in two columns
提问by Taylor Broad
I am having issues calling a departure ICAO and arrival ICAO from my schedules table. Laravel keeps giving me errors that my relationships are screwed up. Here is the code.
我在从我的日程表中调用离开 ICAO 和到达 ICAO 时遇到问题。Laravel 不断给我错误,说我的人际关系搞砸了。这是代码。
Schema::create('airports', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('city');
$table->string('country');
$table->string('iata');
$table->string('icao');
$table->double('lat');
$table->double('lon');
$table->longText('data')->nullable(); //JSON Data for All gate information for the system.
$table->softDeletes();
});
Schema::create('schedule_templates', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->string('flightnum');
$table->integer('depicao')->unsigned();
$table->foreign('depicao')->references('id')->on('airports')->onDelete('cascade');
$table->integer('arricao')->unsigned();
$table->foreign('arricao')->references('id')->on('airports')->onDelete('cascade');
$table->string('aircraft')->nullable();
$table->boolean('seasonal');
$table->date('startdate');
$table->date('enddate');
$table->time('deptime');
$table->time('arrtime');
$table->integer('type');
$table->boolean('enabled');
$table->text('defaults');
$table->timestamps();
$table->softDeletes();
});
Here are the Models
这是模型
class ScheduleTemplate extends Model
{
public $table = "schedule_templates";
public function depicao()
{
return $this->hasOne('App\Models\Airport', 'depicao');
}
public function arricao()
{
return $this->hasOne('App\Models\Airport', 'arricao');
}
}
class Airport extends Model
{
//
public $timestamps = false;
public function schedules()
{
return $this->belongsToMany('App\ScheduleTemplate');
}
}
When I attempt to query using the following code, I get the error
当我尝试使用以下代码进行查询时,出现错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'vaos_airports.depicao' in 'where clause' (SQL: select * from
vaos_airports
wherevaos_airports
.depicao
in (1))
SQLSTATE[42S22]:未找到列:1054 未知列 'vaos_airports.depicao' in 'where 子句'(SQL:select * from
vaos_airports
wherevaos_airports
.depicao
in (1))
$schedules = ScheduleTemplate::with('depicao')->with('arricao')->get();
The end goal is to pull the results into a table. Here is that code if interested.
最终目标是将结果放入表格中。如果有兴趣,这是该代码。
@foreach($schedules as $s)
<tr>
<td>{{$s->code}}</td>
<td>{{$s->flightnum}}</td>
<td>{{$s->depicao()->name}}</td>
<td>{{$s->arricao()->name}}</td>
<td>{{$s->aircraft}}</td>
<td>{{$s->seasonal}}</td>
<td>{{$s->type}}</td>
</tr>
@endforeach
EDIT:
编辑:
I fixed the relationship problem Apparently I had them swapped. Here are the updated Model classes
我解决了关系问题显然我让他们交换了。这是更新的模型类
class ScheduleTemplate extends Model
{
public $table = "schedule_templates";
public function depicao()
{
return $this->belongsTo('App\Models\Airport', 'depicao');
}
public function arricao()
{
return $this->belongsTo('App\Models\Airport', 'arricao');
}
}
class Airport extends Model
{
//
public $timestamps = false;
public function schedules()
{
return $this->hasMany('App\ScheduleTemplate');
}
}
The error now lies in the view file. I will either get a BelongsTo error:
错误现在存在于视图文件中。我要么得到一个 BelongsTo 错误:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
未定义的属性:Illuminate\Database\Eloquent\Relations\BelongsTo::$name
or this if I have arricao or depicao without "()"
或者如果我有没有“()”的 aricao 或 depicao
Trying to get property of non-object
试图获取非对象的属性
回答by Jan Willem
The point is, that the second argument of a relationship should be the foreign key, and the second the local key.
关键是,关系的第二个参数应该是外键,第二个是本地键。
From the docs:
从文档:
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
So in your case, try this:
所以在你的情况下,试试这个:
public function depicao()
{
return $this->hasOne('App\Models\Airport', 'id', 'depicao');
}
public function arricao()
{
return $this->hasOne('App\Models\Airport', 'id', 'arricao');
}
UpdateThe errors are thrown because you have the same column name for this relationship. In my opinion, two solutions:
更新抛出错误是因为此关系的列名相同。在我看来,两种解决方案:
Try to get the first object out of the relationship, like this. But note here: eager loading will not work!
<td>{{$s->depicao()->first()->name}}</td> <td>{{$s->arricao()->first()->name}}</td>
Rename your relationships or the columns, so they don't overlap current column names. This is by far the best option.
尝试从关系中取出第一个对象,就像这样。但请注意:急切加载将不起作用!
<td>{{$s->depicao()->first()->name}}</td> <td>{{$s->arricao()->first()->name}}</td>
重命名您的关系或列,使它们不会与当前列名称重叠。这是迄今为止最好的选择。
For example, you could change the columns to depeciao_idand arricao_id, this also indicates that the columns are referenced to another table with corresponding ID, which is more descriptive.
例如,您可以将列更改为depeciao_id和arricao_id,这也表明这些列引用了具有相应 ID 的另一个表,这更具描述性。