【发布时间】:2019-05-17 12:58:37
【问题描述】:
我对 Laravel 很陌生。 我想显示我需要的数据,并且我创建了与我的表的关系,但它不起作用。你能找出我的错误吗?
我有两个表 Users 和 Places
用户模型
protected $fillable = array('name', 'email', 'password','role','places_id');
public function places(){
return $this->belongsTo('App\Places');
}
地点模型
protected $fillable = array('name','email','phone','address','bio');
public function users()
{
return $this->hasMany('App\User');
}
控制器
$users = User::with('Places')->get();
return view('/dashboard')->with('users',$users);
输出是
Collection {#121 ▼
#items: array:3 [▼
0 => User {#322 ▶}
1 => User {#323 ▶}
2 => User {#324 ▼
#fillable: array:5 [▶]
#hidden: array:2 [▶]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"Places" => null
]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
]
}
【问题讨论】:
-
那么到底是什么不起作用?从我站的地方看,一切似乎都很好。
-
关系“Places”返回NULL,我希望它返回数据
标签: php mysql laravel web eloquent