Laravel有One-belongsToMany关系吗?
Laravel hasOne-belongsToMany relationship?
所以,我一直在努力使这个模式起作用(laravel 的新手,顺便说一下......)我有一个 'photos' table 和一个音频 'tracks' table,我希望能够做这样的事情:
赛道模型:
// Get the photo associated with this track.
public function cover_art() {
return $this->hasOne('App\Photo');
}
照片模特:
// Get all the tracks associated with this photo.
public function tracks() {
return $this->belongsToMany('App\Track');
}
表示这种关系的最佳解决方案是什么(一个音轨只能有一张照片,而一张照片可以属于多个音轨)?
好吧,最后我设置了所有内容并得到了预期的错误:
照片 table:
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->string('path');
$table->string('filename');
$table->string('original_filename');
$table->boolean('is_cover')->default(false);
$table->string('title');
$table->string('slug')->unique();
$table->string('caption')->nullable();
$table->timestamps();
});
曲目table:
Schema::create('tracks', function (Blueprint $table) {
$table->increments('id');
$table->integer('photo_id')->unsigned()->nullable();
$table->string('path');
$table->string('filename');
$table->string('original_filename');
$table->string('title');
$table->string('slug')->unique();
$table->string('caption')->nullable();
$table->timestamps();
$table->foreign('photo_id')->references('id')->on('photos')->onDelete('set null');
});
所以现在,例如,当我在我的视图或控制器中调用适当的方法时,我得到这些错误:
@foreach($tracks as $track)
{{ $track->cover_art }}
@endforeach
SQLSTATE[42S22]:未找到列:1054 'where clause' 中的未知列 'photos.track_id'(SQL:select * 来自 photos
其中 photos
.track_id
= 1 和 photos
.track_id
不是空限制 1)
@foreach($photos as $photo)
{{ $photo->tracks }}
@endforeach
SQLSTATE[42S02]:未找到基础 table 或视图:1146 Table 'iagomarta.photo_track' 不存在(SQL:select tracks
.*, photo_track
.photo_id
作为 pivot_photo_id
, photo_track
.track_id
作为 pivot_track_id
从 tracks
内连接 photo_track
on tracks
.id
= photo_track
.track_id
where photo_track
.photo_id
= 1)
有什么建议吗?
// Track Model
public function cover_art() {
return $this->belongsTo('App\Photo');
}
// Photo Model
public function tracks() {
return $this->hasMany('App\Track');
}
这样想 - 'cover art'/'photo' 代替了专辑,所以 'album'(照片)有很多曲目,而且所有曲目属于一个专辑。
可能只是打字错误,如下所示:
create_table_tracks_migration :
Schema::create('tracks', function (Blueprint $table) {
$table->increments('id');
$table->integer('photo_id')->unsigned();
$table->string('path');
$table->string('filename');
$table->string('original_filename');
$table->string('title');
$table->string('slug')->unique();
$table->string('caption')->nullable();
$table->timestamps();
});
Schema::table('tracks', function(Blueprint $table)
{
$table->foreign('photo_id')
->references('id')
->on('photos')
->onDelete('cascade');
});
通常最好的做法是以这种方式设置外键引用,因为我 运行 自己遇到了一些问题而没有这样做。
附带说明一下,我真的不喜欢创建可为 null 的外键,因为它会造成巨大的混乱。
另外看起来很奇怪的是那个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'photos.track_id' in 'where clause' (SQL: select * from photos where photos.track_id = 1 and photos.track_id is not null limit 1)
无论如何,你的照片 table 上不应该有 track_id 栏。
除此之外,您可以尝试设置 belongsTo(App\Photo) 关系而不是 hasOne。它会起作用,因为一个轨道只能有一张照片。此外,您不需要枢轴 table,因为它是一对多关系,而不是多对多关系。
所以,我一直在努力使这个模式起作用(laravel 的新手,顺便说一下......)我有一个 'photos' table 和一个音频 'tracks' table,我希望能够做这样的事情:
赛道模型:
// Get the photo associated with this track.
public function cover_art() {
return $this->hasOne('App\Photo');
}
照片模特:
// Get all the tracks associated with this photo.
public function tracks() {
return $this->belongsToMany('App\Track');
}
表示这种关系的最佳解决方案是什么(一个音轨只能有一张照片,而一张照片可以属于多个音轨)?
好吧,最后我设置了所有内容并得到了预期的错误:
照片 table:
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->string('path');
$table->string('filename');
$table->string('original_filename');
$table->boolean('is_cover')->default(false);
$table->string('title');
$table->string('slug')->unique();
$table->string('caption')->nullable();
$table->timestamps();
});
曲目table:
Schema::create('tracks', function (Blueprint $table) {
$table->increments('id');
$table->integer('photo_id')->unsigned()->nullable();
$table->string('path');
$table->string('filename');
$table->string('original_filename');
$table->string('title');
$table->string('slug')->unique();
$table->string('caption')->nullable();
$table->timestamps();
$table->foreign('photo_id')->references('id')->on('photos')->onDelete('set null');
});
所以现在,例如,当我在我的视图或控制器中调用适当的方法时,我得到这些错误:
@foreach($tracks as $track)
{{ $track->cover_art }}
@endforeach
SQLSTATE[42S22]:未找到列:1054 'where clause' 中的未知列 'photos.track_id'(SQL:select * 来自 photos
其中 photos
.track_id
= 1 和 photos
.track_id
不是空限制 1)
@foreach($photos as $photo)
{{ $photo->tracks }}
@endforeach
SQLSTATE[42S02]:未找到基础 table 或视图:1146 Table 'iagomarta.photo_track' 不存在(SQL:select tracks
.*, photo_track
.photo_id
作为 pivot_photo_id
, photo_track
.track_id
作为 pivot_track_id
从 tracks
内连接 photo_track
on tracks
.id
= photo_track
.track_id
where photo_track
.photo_id
= 1)
有什么建议吗?
// Track Model
public function cover_art() {
return $this->belongsTo('App\Photo');
}
// Photo Model
public function tracks() {
return $this->hasMany('App\Track');
}
这样想 - 'cover art'/'photo' 代替了专辑,所以 'album'(照片)有很多曲目,而且所有曲目属于一个专辑。
可能只是打字错误,如下所示:
create_table_tracks_migration :
Schema::create('tracks', function (Blueprint $table) {
$table->increments('id');
$table->integer('photo_id')->unsigned();
$table->string('path');
$table->string('filename');
$table->string('original_filename');
$table->string('title');
$table->string('slug')->unique();
$table->string('caption')->nullable();
$table->timestamps();
});
Schema::table('tracks', function(Blueprint $table)
{
$table->foreign('photo_id')
->references('id')
->on('photos')
->onDelete('cascade');
});
通常最好的做法是以这种方式设置外键引用,因为我 运行 自己遇到了一些问题而没有这样做。
附带说明一下,我真的不喜欢创建可为 null 的外键,因为它会造成巨大的混乱。
另外看起来很奇怪的是那个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'photos.track_id' in 'where clause' (SQL: select * from photos where photos.track_id = 1 and photos.track_id is not null limit 1)
无论如何,你的照片 table 上不应该有 track_id 栏。
除此之外,您可以尝试设置 belongsTo(App\Photo) 关系而不是 hasOne。它会起作用,因为一个轨道只能有一张照片。此外,您不需要枢轴 table,因为它是一对多关系,而不是多对多关系。