2015-05-28 70 views
1

我知道如何创建一个模型 - php artisan make:model nameofmodel,我知道,在模型的迁移,我必须声明将表中有哪些列:如何管理模型的关系(fk)?

class CreateNameofmodelTable extends Migration { 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('nameofmodels', function(Blueprint $table) 
     { 
      $table->increments('id'); 
      $table->string('name')->unique(); 
      $table->string('description'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('nameofmodels'); 
    } 
} 

但是,如果我想涉及使用FK两种模式,我必须在迁移文件或模型文件中设置哪些配置?

回答

1

假设您有另一个模型用户名为users。你可以有addressesusers如下之间定义的关系,

public function up() 
    { 
     Schema::create('addresses', function(Blueprint $table) 
     { 
      $table->increments('id'); 
      $table->string('add1'); 
      $table->string('add2'); 
      $table->string('city'); 
      $table->string('contact_no'); 
      $table->enum('is_primary',['yes','no'])->default('no'); 
      $table->integer('user_id')->unsigned(); 
      $table->timestamps(); 

      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
     }); 
    } 

在用户模型类,

class User extends Model { 
    /** 
    * The table associated with the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    public function addresses() 
    { 
     return $this->hasMany('App\Address'); 
    } 

} 

在Address类

class Address extends Model { 
    /** 
    * The table associated with the model. 
    * 
    * @var string 
    */ 
    protected $table = 'addresses'; 

    public function user() 
    { 
     return $this->belongsTo('App\User'); 
    } 

} 

有关创建表的更多详细信息,可以发现关于Laravel的官方Schema Docs。而关于关联船here

UPDATE

根据this问题的更多信息,你必须先引用表之前创建引用表。在我们的情况下,在运行迁移以创建addresses表之前,必须存在users

简单的用例

假设,在控制你想要找出多少地址特定用户,那么你可以做以下,

$addressCount = User::find($userId)->addresses->count(); 

请注意相关的模型而User::find($userId)->addresses返回集合类型User::find($userId)->addresses()返回对象HasMany

你也可以遍历集合如下,

foreach(User::find($userId)->addresses as $address) 
{ 
    echo '<pre>'; 
    print_r($address); 
} 

在另一边,你可以得到用户提供如下特定地址相关联,

$user = Address:find($addressId)->user; //returns object of type `User` 
echo $user->first_name; 

以下将工作

​​

注意,上面Address:find($addressId)->user不会返回集合,但类User的单个对象。这是因为belongsTo关系。

我希望我能为你明确事情。但没有官方docs好。

Laracast视频更适合与Laravel合作。

+0

你能不能解释一下发生在用户和地址类?我的意思是,据我所知,功能'adresses'和' user'具有告诉表之间的关系是什么样的功能,但是何时调用这些函数? –

+0

我得到一个错误150,其引用的FK,我不知道是如果laravel处理迁移的顺序,他们创建或评估,如果一个表之前必须存在,因为fks –

+0

我已更新我的答案解决您的担忧。 –

0

你可以像这样使用...

class CreateNameofmodelTable extends Migration { 

/** 
* Run the migrations. 
* 
* @return void 
*/ 
public function up() 
{ 
    Schema::create('nameofmodels', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->string('name')->unique(); 
     $table->string('description'); 
     $table->timestamps(); 
    }); 
Schema::create('your table name', function(Blueprint $table) { 
    $table->foreign('user_id')->references('id')->on('nameofmodels'); 
} 

} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::drop('nameofmodels'); 
} 

} 

检查it..may是,它正在完美....