簡體   English   中英

使用 Eloquent 將 Laravel 4 與 3 個表聯系起來

[英]Relations Laravel 4 with 3 tables using Eloquent

我想使用 ORM 與 3 個表建立關系但不能。 我的桌子

用戶表

id | userame | name |
 1    Ellie    Elen
 2    Pol      Paul

記錄表

id | user_id| item_id| hour|
 1    2         1       3
 2    2         2       5

項目表表

id |  title 
 1    item 1  
 2    item 2

我正在使用此邏輯但無法正常工作

class User Extends Eloquent {

  public function record()
   {
    return $this->hasMany('VolunteerRecord');
   }

}


class VolunteerRecord Extends Eloquent {

    function item() {
        return $this->hasMany('VolunteerItem');
    }

}

我不明白該怎么做?

看起來您想要UsersItems之間的多對多關系,但您還想在數據透視表上跟蹤小時數。 所以首先,您將使用belongsToMany()定義多對多關系,然后您將使用withPivot()函數告訴 Laravel 您的數據透視表上有額外的數據。 您的課程將如下所示:

class User extends Eloquent {
    protected $table = 'users';

    public function items() {
        return $this->belongsToMany('Item', 'records')->withPivot('hour');
    }
}

class Item extends Eloquent {
    protected $table = 'items';

    public function users() {
        return $this->belongsToMany('User', 'records')->withPivot('hour');
    }
}

然后,要訪問hour字段,您可以這樣做:

$user = User::first(); // First User
$item = $user->items()->first(); // User's first Item
$hour = $item->pivot->hour; // The 'hour' on the User-Item relationship

此外,您當前的列命名方案對於 Laravel 是正確的,因此您不需要更改它。 如果更改列名,則需要在belongsToMany()方法中定義它們,如下所示:

$this->belongsToMany('ModelName', 'pivot_table', 'foreign_key', 'other_key');

// For example, in Items::users() you would have this:
$this->belongsToMany('User', 'records', 'users_id', 'items_id');

最后,我假設您的表被命名為usersitemsrecords 如果不是,則只需將usersitemsrecords的所有實例替換為您的實際表名。

根據您的表名,我建議如下,首先,按如下方式更改您的記錄表:

id | users_id| items_id| hour|
 1    2         1       3
 2    2         2       5

這些是您的模型的類:

class Users extends Eloquent
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    public function records()
    {
        return $this->hasMany('Records');
    }
}


class Records extends Eloquent
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'records';

    public function record()
    {
        return $this->hasOne('Users');
    }

    public function item()
    {
        return $this->hasOne('Items');
    }
}

class Items extends Eloquent
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'items';

    public function records()
    {
        return $this->hasMany('Records');
    }
}

這些包含您的模型的關系。 如果您要選擇幾條記錄,對於每條記錄,您可以獲得用戶和項目。 如果您要選擇一個項目,以及該項目的所有記錄。 您還可以獲取每條記錄的用戶。

在用戶模型中

public function images()
{
    return $this->belongsToMany('Item')->withPivot('hour');
}

在用戶控制器中

public function view($username)
{
    $user = User::where('name',$username)->firstOrFail();
    return View::make('view')->with('user',$user);
}

在視圖中

    @foreach ($users->items as $item)
        name: {{$image->title}}<br>

        hour: {{$image->pivot->hour}}<br>
    @endforeach

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM