Laravel - 一个节目可以有多个提供商
Laravel - A show can have multiple providers
所以我想找到一个解决方案,但不确定具体如何做。我有一个 table 存储所有发生的节目。在给定的节目中,我可以让多个提供者参加该节目。提供者也可以参加许多节目。那么如何将其存储在数据库中并建立 eloquent 关系?
显示架构
Schema::create('shows', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('number')->unsigned();
$table->dateTime('airDate');
$table->string('podcastUrl')->nullable();
$table->timestamps();
});
提供商架构
Schema::create('providers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('shortName')->nullable();
$table->string('image')->nullable();
$table->string('social')->nullable();
$table->timestamps();
});
我会在节目架构中存储 provider_id 吗?
更新 1
所以我为数据中心创建了一个新的迁移 table
Schema::create('provider_show', function (Blueprint $table) {
$table->integer('provider_id')->unsigned()->index();
$table->foreign('provider_id')->references('id')->on('providers')->onDelete('cascade');
$table->integer('show_id')->unsigned()->index();
$table->foreign('show_id')->references('id')->on('shows')->onDelete('cascade');
$table->primary(['provider_id', 'show_id']);
});
然后在展示模型中我创建了以下内容
public function providers()
{
return $this->belongsToMany(Provider::class);
}
现在,当我保存一个新节目时,我添加了一个 multiselect 到 select 我想要的提供商
$show = new Show;
$show->name = $request->name;
$show->number = $request->number;
$show->airDate = $request->airDate;
$show->podcastUrl = $request->podcastUrl;
$show->providers()->attach($request->providerList);
$show->save();
Session::flash('message', "Created Successfully!");
return back();
然后当我保存时出现以下错误
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: provider_show.show_id (SQL: insert into "provider_show" ("provider_id", "show_id") select 1 as "provider_id", as "show_id" union all select 2 as "provider_id", as "show_id")
创建一个 provider_show
迁移,它将作为您的支点 table。
table 将同时包含 provider_id
和 show_id
,这将提供这些实体之间的多对多关系。
然后在您的 Provider
模型上,您可以提供一个 shows()
方法,其中 returns 一个 BelongsToMany
关系。
// In your Provider model
public function shows()
{
return $this->belongsToMany('App\Show');
}
请注意,Laravel 默认情况下会根据两个关系的字母顺序查找枢轴 table 名称。
您还可以通过提供 providers()
方法在您的 Show
模型上添加逆函数,该方法也 returns BelongsToMany 关系。
所以我想找到一个解决方案,但不确定具体如何做。我有一个 table 存储所有发生的节目。在给定的节目中,我可以让多个提供者参加该节目。提供者也可以参加许多节目。那么如何将其存储在数据库中并建立 eloquent 关系?
显示架构
Schema::create('shows', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('number')->unsigned();
$table->dateTime('airDate');
$table->string('podcastUrl')->nullable();
$table->timestamps();
});
提供商架构
Schema::create('providers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('shortName')->nullable();
$table->string('image')->nullable();
$table->string('social')->nullable();
$table->timestamps();
});
我会在节目架构中存储 provider_id 吗?
更新 1
所以我为数据中心创建了一个新的迁移 table
Schema::create('provider_show', function (Blueprint $table) {
$table->integer('provider_id')->unsigned()->index();
$table->foreign('provider_id')->references('id')->on('providers')->onDelete('cascade');
$table->integer('show_id')->unsigned()->index();
$table->foreign('show_id')->references('id')->on('shows')->onDelete('cascade');
$table->primary(['provider_id', 'show_id']);
});
然后在展示模型中我创建了以下内容
public function providers()
{
return $this->belongsToMany(Provider::class);
}
现在,当我保存一个新节目时,我添加了一个 multiselect 到 select 我想要的提供商
$show = new Show;
$show->name = $request->name;
$show->number = $request->number;
$show->airDate = $request->airDate;
$show->podcastUrl = $request->podcastUrl;
$show->providers()->attach($request->providerList);
$show->save();
Session::flash('message', "Created Successfully!");
return back();
然后当我保存时出现以下错误
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: provider_show.show_id (SQL: insert into "provider_show" ("provider_id", "show_id") select 1 as "provider_id", as "show_id" union all select 2 as "provider_id", as "show_id")
创建一个 provider_show
迁移,它将作为您的支点 table。
table 将同时包含 provider_id
和 show_id
,这将提供这些实体之间的多对多关系。
然后在您的 Provider
模型上,您可以提供一个 shows()
方法,其中 returns 一个 BelongsToMany
关系。
// In your Provider model
public function shows()
{
return $this->belongsToMany('App\Show');
}
请注意,Laravel 默认情况下会根据两个关系的字母顺序查找枢轴 table 名称。
您还可以通过提供 providers()
方法在您的 Show
模型上添加逆函数,该方法也 returns BelongsToMany 关系。