Laravel 5 - Eloquent 返回空集合的关系
Laravel 5 - Eloquent relationship returning an empty collection
我在一段 Laravel 5 关系中遇到了麻烦。我有 2 个模型 Crew
和 Event
以及相应的表 crews
和 events
。剧组有很多事件,事件有一个剧组。我按如下方式设置我的模型和迁移:
架构:
//Crews
Schema::connection('scheduling')->create('crews', function ($table) {
$table->increments('id');
$table->text('name');
$table->boolean('solo');
$table->boolean('active');
$table->text('phone');
});
//Events
Schema::connection('scheduling')->create('events', function ($table) {
$table->increments('id');
// ...
$table->integer('crew_id')->unsigned();
$table->foreign('crew_id')->references('id')->on('crews');
$table->text('notes');
// ...
$table->timestamps();
});
型号:
namespace App\Models\Scheduling;
use Illuminate\Database\Eloquent\Model;
class Crew extends Model {
public $connection = "scheduling";
public $table = "crews";
public function events() {
return $this->hasMany('App\Models\Scheduling\Event', 'id', 'crew_id');
}
public static function active() {
return Crew::where('active', 1)->get();
}
}
namespace App\Models\Scheduling;
use Illuminate\Database\Eloquent\Model;
class Event extends Model {
public $connection = "scheduling";
public $table = "events";
public function crew() {
return $this->belongsTo('App\Models\Scheduling\Crew', 'crew_id', 'id');
}
}
如果我 运行 Crew::find(102)->events;
我最终得到一个空集合。
如果我 运行 Events::where('crew_id', 102)->get();
我最终会得到我期望的事件列表。
知道我在这里做错了什么吗?
您对 events 关系的定义无效 - 您以错误的顺序传递参数。
替换:
return $this->hasMany('App\Models\Scheduling\Event', 'id', 'crew_id');
和
return $this->hasMany('App\Models\Scheduling\Event', 'crew_id', 'id');
或者干脆
return $this->hasMany('App\Models\Scheduling\Event');
因为您正在使用列名的默认值,所以不需要将它们传递给关系定义。
我在一段 Laravel 5 关系中遇到了麻烦。我有 2 个模型 Crew
和 Event
以及相应的表 crews
和 events
。剧组有很多事件,事件有一个剧组。我按如下方式设置我的模型和迁移:
架构:
//Crews
Schema::connection('scheduling')->create('crews', function ($table) {
$table->increments('id');
$table->text('name');
$table->boolean('solo');
$table->boolean('active');
$table->text('phone');
});
//Events
Schema::connection('scheduling')->create('events', function ($table) {
$table->increments('id');
// ...
$table->integer('crew_id')->unsigned();
$table->foreign('crew_id')->references('id')->on('crews');
$table->text('notes');
// ...
$table->timestamps();
});
型号:
namespace App\Models\Scheduling;
use Illuminate\Database\Eloquent\Model;
class Crew extends Model {
public $connection = "scheduling";
public $table = "crews";
public function events() {
return $this->hasMany('App\Models\Scheduling\Event', 'id', 'crew_id');
}
public static function active() {
return Crew::where('active', 1)->get();
}
}
namespace App\Models\Scheduling;
use Illuminate\Database\Eloquent\Model;
class Event extends Model {
public $connection = "scheduling";
public $table = "events";
public function crew() {
return $this->belongsTo('App\Models\Scheduling\Crew', 'crew_id', 'id');
}
}
如果我 运行 Crew::find(102)->events;
我最终得到一个空集合。
如果我 运行 Events::where('crew_id', 102)->get();
我最终会得到我期望的事件列表。
知道我在这里做错了什么吗?
您对 events 关系的定义无效 - 您以错误的顺序传递参数。
替换:
return $this->hasMany('App\Models\Scheduling\Event', 'id', 'crew_id');
和
return $this->hasMany('App\Models\Scheduling\Event', 'crew_id', 'id');
或者干脆
return $this->hasMany('App\Models\Scheduling\Event');
因为您正在使用列名的默认值,所以不需要将它们传递给关系定义。