【问题标题】:Laravel Relationships 2 tablesLaravel 关系 2 表
【发布时间】:2014-05-08 04:39:19
【问题描述】:

我有两个要连接的表,

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

Second table = Rooms
ID
Owner 
Room_ID
Roome_name

我的目标是获取所有 Johny 朋友,然后检查他们是否有房间,如果是,则检索所有者、房间 ID、房间名称。我在谷歌搜索结果,但我找不到它。这是我第一次接触关系,我不知道如何在那里使用 where 语句。简单明了的建议,我将不胜感激。

这是我的课程:

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

对不起,我的英语不好。

【问题讨论】:

    标签: orm laravel laravel-4


    【解决方案1】:

    假设您有一个 users 表,您希望将 friends 表用作用户到其自身的数据透视表。这听起来很复杂,但实际上它最终很容易......

    我修改了您的一些专栏,因为有些事情没有多大意义。不知道为什么rooms 需要一个 id 列和一个 room_id 列。这应该可以为您提供一个很好的基础,并且希望它对您来说是相当可扩展的。您可能需要一个 room_user 表来存储谁在哪个房间。

    迁移

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

    用户模型

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

    使用

    $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>";
        }
    }
    

    如果您需要更多帮助,请尽管问。

    编辑:

    如果某人可以拥有多个房间,只需将该关系更改为 hasMany()。那么你将不得不使用它只是有点不同......

    $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>";
            }
        }
    }
    

    每天 3 个房间的逻辑并不真正属于这里。当允许他们创建房间时,这更像是一个验证问题。

    【讨论】:

    • 嘿,我的房间需要 room_ID 因为它是来自其他数据库的其他 ID 我正在使用 teampeak 3 api 框架,当用户创建房间时,服务器查询会给出房间 ID,我需要这个 id 作为邀请链接.我正在为朋友创建房间时制作通知系统,并且此链接需要 Room_id javascript:location.href='ts3server://localhost/?port=9987&cid= 所以当用户点击它时,他会去朋友频道:)
    • 哦,太棒了,我将它们添加回来并修改了它的输出内容,因此它应该为您输出链接。
    【解决方案2】:

    这就是Eloquent 的全部魅力所在。您不必将where 放在任何地方!你只需调用类似的东西

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

    ...是的,它就这么简单

    第二次输入belongsTohasMany,Eloquent 已经告诉 Laravel 它应该对查询执行一种操作:分别为 where id = 'child_id'where = foreign_id = "id"。我不知道我是否说清楚了。有任何疑问,请发表评论! =D

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 2014-04-19
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2015-02-22
      • 2016-12-07
      相关资源
      最近更新 更多