Ben*_*Ben 34 pivot-table laravel eloquent laravel-4
在Laravel 4中,当处理http://four.laravel.com/docs/eloquent#many-to-many中描述的多对多关系时,我怎样才能真正让Laravel为我创建数据透视表?
我是否需要在迁移中为所涉及的两个模型添加一些内容?我是否需要为数据透视表手动创建迁移?或者Laravel如何知道创建数据透视表?
到目前为止,我所做的就是将belongsToMany信息添加到两个相应的模型中,即
class User extends Eloquent
{
public function roles()
{
return $this->belongsToMany('Role');
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会触发数据透视表的创建?我错过了什么步骤?
Ben*_*Ben 56
看起来好像需要手动创建数据透视表(即Laravel不会自动执行此操作).这是怎么做的:
1.)按字母顺序使用单个表名创建新的迁移(默认):
php artisan make:migration create_alpha_beta_table --create --table=alpha_beta
Run Code Online (Sandbox Code Playgroud)
2.)在新创建的迁移中,将up函数更改为:
public function up()
{
Schema::create('alpha_beta', function(Blueprint $table)
{
$table->increments('id');
$table->integer('alpha_id');
$table->integer('beta_id');
});
}
Run Code Online (Sandbox Code Playgroud)
3.)如果需要,添加外键约束.(我还没有达到那个目的).
现在,使用来自beta的键来播放alpha表,您可以在AlphaTableSeeder中执行以下操作:
public function run()
{
DB::table('alpha')->delete();
Alpha::create( array(
'all' => 'all',
'your' => 'your',
'stuff' => 'stuff',
) )->beta()->attach( $idOfYourBeta );
}
Run Code Online (Sandbox Code Playgroud)
小智 34
我使用Jeffrey Way的Laravel-4-Generators或Laravel-5-Generators-Extended.
然后你可以使用这个工匠命令:
php artisan generate:pivot table_one table_two
Run Code Online (Sandbox Code Playgroud)
Afz*_*ivE 20
扩展Ben的答案(我试图编辑它,但评论者说它增加了太多):
要添加外键约束,请确保alpha id是无符号的,alpha_id在数据透视表中也是无符号的.这个迁移将在Ben的答案之后(2)运行,因为它改变了当时创建的表.
public function up()
{
Schema::table('alpha_beta', function(Blueprint $table)
{
$table->foreign('alpha_id')->references('id')->on('alpha');
$table->foreign('beta_id')->references('id')->on('beta');
});
}
Run Code Online (Sandbox Code Playgroud)
Ada*_*ery 10
php artisan make:migration create_alpha_beta_table --create=alpha_beta
Run Code Online (Sandbox Code Playgroud)
public function up() {
Schema::create('alpha_beta', function(Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('alpha_id');
$table->unsignedBigInteger('beta_id');
// foreign keys
$table->foreign('alpha_id')->references('id')->on('alphas');
$table->foreign('beta_id')->references('id')->on('betas');
});
}
Run Code Online (Sandbox Code Playgroud)
对于多对多关系,您可以手动创建数据库的迁移文件,如下所示:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateAccountTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('account_tag', function (Blueprint $table) {
// $table->timestamps(); // not required
// $table->softDeletes(); // not required
$table->integer('account_id')->unsigned();
$table->foreign('account_id')->references('id')->on('accounts');
$table->integer('tag_id')->unsigned()->nullable();
$table->foreign('tag_id')->references('id')->on('tags');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('account_tag');
}
}
Run Code Online (Sandbox Code Playgroud)
注意:如果你timestamps在数据透视表上,你必须设置withTimestamps两端的关系,如下所示:
return $this->belongsToMany(\Mega\Modules\Account\Models\Tag::class)->withTimestamps();
Run Code Online (Sandbox Code Playgroud)
.
return $this->belongsToMany(\Mega\Modules\Account\Models\Account::class)->withTimestamps();
Run Code Online (Sandbox Code Playgroud)
对于最新的 Laravel 版本:
composer require --dev laracasts/generators
php artisan make:migration:pivot table1 table2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37647 次 |
| 最近记录: |