Php 拉维关系查询
我在我的项目中使用了Laravel4,我在Mysql数据库中有这些表Php 拉维关系查询,php,mysql,laravel,Php,Mysql,Laravel,我在我的项目中使用了Laravel4,我在Mysql数据库中有这些表 Users : - id - username 1 - user1 2 - user2 - - - 在laravel中,我可以使用hasMany和Belognomany关系成功地处理三个表。。但是4张桌子!对我来说太复杂了 我花了2天的时间在这上面,但我不知道如何通过一个简单的查询来实现这一点 我想做的是,当我登录到blog1时,我需要列出我可以访问的所有博客,例如: 博客(所有者) 博客2(编
Users :
- id - username
1 - user1
2 - user2
-
-
-
在laravel中,我可以使用hasMany和Belognomany关系成功地处理三个表。。但是4张桌子!对我来说太复杂了
我花了2天的时间在这上面,但我不知道如何通过一个简单的查询来实现这一点
我想做的是,当我登录到blog1时,我需要列出我可以访问的所有博客,例如:
- 博客(所有者)
- 博客2(编辑)
$user = User::find(1);
print_r($user->access_blogs); // list blogs that I have assigned roles to them
也许以后
$myrole = Blog::find(1)->where('user_id', 2);
echo $myrole->myrole; // editor
--
。。等等,你想到了……) 看看这个。缺点是,它还不能处理第三个表的快速加载。这看起来很有趣!我要去看看,看看它对我的问题有什么帮助,非常感谢Jarek:)
Assigned_roles:
- id - role_id - user_id - blog_id
1 - 1 - 1 - 1 // user1 owns blog1
2 - 2 - 1 - 2 // user1 editor in blog2
$user = User::find(1);
print_r($user->access_blogs); // list blogs that I have assigned roles to them
$myrole = Blog::find(1)->where('user_id', 2);
echo $myrole->myrole; // editor
// 1
use LaravelBook\Ardent\Ardent;
class User extends Ardent {
public function blogs()
{
return $this->hasMany('Blog');
}
}
// 2
use LaravelBook\Ardent\Ardent;
class Assignedroles extends Ardent {
protected $table = 'assigned_roles';
}