【发布时间】:2017-03-03 07:19:59
【问题描述】:
我在从我的日程表中呼叫出发国际民航组织和到达国际民航组织时遇到问题。 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();
});
这里是模型
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');
}
}
当我尝试使用以下代码进行查询时,我得到了错误
SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列'vaos_airports.depicao'(SQL:select * from
vaos_airportswherevaos_airports.depicaoin (1))
$schedules = ScheduleTemplate::with('depicao')->with('arricao')->get();
最终目标是将结果放入表格中。如果有兴趣,这是该代码。
@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
编辑:
我解决了关系问题显然我让他们交换了。以下是更新后的模型类
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');
}
}
现在错误在于视图文件。我会得到一个 BelongsTo 错误:
未定义属性:Illuminate\Database\Eloquent\Relations\BelongsTo::$name
或者如果我有没有“()”的 arricao 或 depicao
试图获取非对象的属性
【问题讨论】:
标签: php mysql laravel eloquent