php – Relations Laravel 4 with 3 tables using Eloquent-ThrowExceptions

Exception or error:

I want to make a relation with 3 table using ORM but cant. My tables

User table

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

record table

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

Item table table

id |  title 
 1    item 1  
 2    item 2

I am using this logic but not work properly

class User Extends Eloquent {

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

}


class VolunteerRecord Extends Eloquent {

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

}

I cant understand how to do it?

How to solve:

It seems like you want a Many-To-Many relationship between Users and Items but you also want to track hours on the pivot table. So first, you’ll define the many-to-many relationships using belongsToMany(), and you’ll tell Laravel that you have extra data on your pivot table with the withPivot() function. Your classes will look like this:

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');
    }
}

Then, to access the hour field you would do this:

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

Also, your current column naming scheme is correct for Laravel so don’t feel like you need to change it. If you change your column names, then you’ll need to define them in the belongsToMany() method like this:

$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');

Finally, I’m assuming that your tables are named users, items, and records. If they are not, then just replace all instances of users, items, and records with your actual table names.

Answer:

Based on your table names, I’d suggest the following, first of all, change your record table as follows:

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

And these are the classes for your models:

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');
    }
}

These contain the relations for your models.
If you were to select a few records, for each record you can get the user and the item. If you were to select an item, and all records for that item. You can also get the user for each record.

Answer:

In User Model

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

In user controller

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

In view

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *