Php 拉维关系法&x27;属于造口';投掷误差
摘要 尝试调用关系时,我收到以下错误: 类的对象Illumb\Database\Eloquent\Relations\BelongTomany 无法转换为字符串 我的设置非常基本,由两个模型组成,Php 拉维关系法&x27;属于造口';投掷误差,php,orm,laravel,laravel-4,Php,Orm,Laravel,Laravel 4,摘要 尝试调用关系时,我收到以下错误: 类的对象Illumb\Database\Eloquent\Relations\BelongTomany 无法转换为字符串 我的设置非常基本,由两个模型组成,用户和角色 用户模型[User.php] <?php use Illuminate\Auth\UserInterface; class User extends Eloquent implements UserInterface { protected $table = 'users'
用户
和角色
用户模型[User.php]
<?php
use Illuminate\Auth\UserInterface;
class User extends Eloquent implements UserInterface {
protected $table = 'users';
protected $hidden = array('password');
protected $fillable = array('id', 'username', 'password');
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
}
<?php
class Role extends Eloquent {
protected $table = "roles";
protected $fillable = array(
'id',
'code',
'name'
);
public function foo() {
return $this->belongsToMany('User', 'map_role_user', 'role_id', 'user_id');
}
}
从
或
如果集合被强制转换为字符串,它将作为JSON返回:
<?php
$roles = (string) User::find(1)->roles;
来自
或
如果集合被强制转换为字符串,它将作为JSON返回:
<?php
$roles = (string) User::find(1)->roles;
如果不想向查询添加更多约束,则必须使用动态属性
概念。所以
$user = App\User::find(1);
foreach ($user->posts as $post) {
//
}
如果要添加更多约束,请执行此操作
App\User::find(1)->posts()->where('title', 'LIKE', '%Best%')->get()
如果不想向查询添加更多约束,则必须使用动态属性
概念。所以
$user = App\User::find(1);
foreach ($user->posts as $post) {
//
}
如果要添加更多约束,请执行此操作
App\User::find(1)->posts()->where('title', 'LIKE', '%Best%')->get()
试试这个Role::find(1)->foo
就是这样。干杯试试这个Role::find(1)->foo
就是这样。干杯我是个笨蛋!谢谢我花了一个小时盯着它看。感谢您的关注,角色返回user
加上透视表中的数据,map\u role\u user
,但不返回role
。我需要手动迭代,对吗?User::find(1)->roles将返回关于角色的所有数据,但不返回关于用户的任何数据。如果您需要用户和角色的信息,您可以执行$user=user::find(1),然后您可以使用$users作为集合,并调用$users->roles来显示有关该用户角色的数据。我是个笨蛋!谢谢我花了一个小时盯着它看。感谢您的关注,角色返回user
加上透视表中的数据,map\u role\u user
,但不返回role
。我需要手动迭代,对吗?User::find(1)->roles将返回关于角色的所有数据,但不返回关于用户的任何数据。如果需要用户和角色的信息,可以执行$user=user::find(1),然后使用$users作为集合并调用$users->roles来显示有关该用户角色的数据。