user34038
user34038

Reputation: 19

Laravel Relationships 2 tables

I have two tables which I want to connect,

First table = Friendship
ID
User1 = Tim 
User2 = Johny
Accepted = 0/1 <- friends if accepted = 1

Second table = Rooms
ID
Owner 
Room_ID
Roome_name

My goal is to get all Johny friends then check if any of them has Rooms if yes retrieve owner, room_id, room_name. I searched result in google but I could not find it. It's my first time with relationships and I don't know how to use where statments there. I would be greateful for simple and clear advice.

Here are my classes:

class Friendship extends Eloquent {
    protected $table = 'friendship';

    public function friendrooms()
    {
        return $this->hasMany('Room');
    }
}

class Room extends \Eloquent {    
    protected $table = 'rooms';

    public function roomowner()
    {
        return $this->belongsTo('Friendship');
    }
}

Sorry for my bad english.

Upvotes: 0

Views: 433

Answers (2)

user1669496
user1669496

Reputation: 33048

Assuming you have a users table, you'd want to use your friends table as a pivot table for users onto itself. It sounds quite complicated, but it ends up being pretty easy in practice...

I modified a few of your columns because there were a few things that didn't make a lot of sense. Not sure why rooms needed an id column and a room_id column. This should get you a pretty good base and it's hopefully fairly extensible for you. You'd probably want a room_user table which stores who is in what room.

Migrations

Schema::create('friends', function($table)
{
    $table->increments('id');
    $table->integer('user_id');
    $table->integer('friend_id');
    $table->boolean('accepted');
    $table->boolean('deleted');
    $table->timestamps();
});

Schema::create('rooms', function($table)
{
    $table->increments('id');
    $table->integer('user_id');  // The is room's owner.
    $table->string('description');
    $table->integer('room_id');
    $table->string('room_name');
    $table->timestamps();
});

User Model

class User extends Eloquent implements UserInterface, RemindableInterface {

    public function friends()
    {
        return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')->wherePivot('accepted', '1');
    }

    public function room()
    {
        return $this->hasOne('Room');
    }

    public function hasRoom()
    {
        return (bool)$this->room()->count();
    }
}

Use

$user = User::find(1);

foreach($user->friends as $friend) {
    if($friend->hasRoom()) {
        echo "<a href='javascript:location.href='ts3server://localhost/?port=9987&cid=".$friend->room->room_id."'>Join ".$friend->room->name."</a>";
    }
}

If you need anymore help, ask away.

Edits:

If someone can have many rooms, simply change that relationship to a hasMany(). Then you would have to use it just a bit differently...

$user = User::find(1);
    $friends = $user->friends()->paginate(15);

foreach($friends as $friend) {
    if($friend->hasRoom()) {
        foreach($friend->rooms as $room) {
            echo "<a href='javascript:location.href='ts3server://localhost/?port=9987&cid=".$friend->room->room_id."'>Join ".$friend->room->name."</a>";
        }
    }
}

The logic for the 3 rooms per day doesn't really belong here. That would be more of a validation issue when allowing them to create rooms.

Upvotes: 1

Dennis Braga
Dennis Braga

Reputation: 1478

That's all the beauty in the Eloquent. You don't have to put a where in no place!! You just call something like

$friends = Friendship::find($friend_id);
$friends->friendrooms()->get(); //To get a list of all rooms

... yes, it's that simple!

The second you put a belongsTo or hasMany, the Eloquent already tells Laravel it should perform one kind of operation on the query: where id = 'child_id' and where = foreign_id = "id", respectively. I don't know if I have made myself clear. Any doubts, just comment! =D

Upvotes: 0

Related Questions