2016-08-15 70 views
0

我使用Laravel 5.2和Entrust軟件包爲ACL工作。 在這個項目中,我需要一個角色('venue_owner'),場地是所有者。我也有表venue,我不知道如何使這種關係,因爲表users是所有類型的用戶通用。特定角色關係

如何使這種關係知道什麼user角色venue_owner是什麼venues的所有者?

回答

1

您是否創建了Migrationsphp artisan enthrust:migration?如果沒有它,運行它,然後所生成的文件裏面,Enthrust遷移文件的up()方法中添加自己的表象下面這樣:

<?php 

    public function up() { 
     // SOME OTHER TABLE CREATION CODES... 

     Schema::create('venue_owner', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer("user_id")->unsigned(); 
      $table->timestamps(); 
      // CREATE THE ASSOCIATION/RELATIONSHIP USING FOREIGN KEY 
      $table->foreign('id') 
        ->references('id') 
        ->on('venue') 
        ->onDelete('cascade'); 
     }); 

     Schema::create('venues', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer("venue_owner_id")->unsigned(); 
      $table->string("venue"); 
      $table->timestamps(); 
      // CREATE THE ASSOCIATION/RELATIONSHIP USING FOREIGN KEY 
      $table->foreign('venue_owner_id') 
        ->references('id') 
        ->on('venue_owner'); 
     });   
    } 

    public function down() { 
     // OTHER DROP COMMAND CODES... 
     Schema::drop('venue_owner'); 
     Schema::drop('venues'); 
    } 
在你的口才模型類

然後你可以明確地設置$this->hasMany()像這樣:

<?php 

    namespace App; 
    use Illuminate\Database\Eloquent\Model; 

    class VenueOwner extends Model { 
     /** 
     * GET ALL THE venues FOR THE venue_owner . 
     */ 
     public function venues() { 
      return $this->hasMany('App\Venues'); 
     } 

     /** 
     * GET ALL THE user FOR THE venue_owner . 
     */ 
     public function user() { 
      return $this->hasOne('App\User'); 
     } 

而在你Venues口才模型類,你這樣做:

<?php 

    namespace App; 
    use Illuminate\Database\Eloquent\Model; 

    class Venues extends Model { 
     /** 
     * GET THE venue_owner FOR venue(s). 
     */ 
     public function venueOwner() { 
      return $this->belongsTo('App\VenueOwner'); 
     } 

最後,但在你的Users雄辯模型類並非最不重要的,你這樣做:

<?php 

    namespace App; 
    use Illuminate\Database\Eloquent\Model; 

    class Users extends Model { 
     /** 
     * GET THE user Information FOR venue_owner. 
     */ 
     public function venueOwner() { 
      return $this-> hasOne('App\VenueOwner'); 
     } 

現在,你可以使用user_id有關venue_owner和他venuesroles & permissions的所有信息。