Laravelの熱心な読み込みを使用して情報を取得する際に問題が発生しています。すべてのPostオブジェクトを呼び出して、敬意を表しているユーザをクエリに追加しようとしています。ここで幼虫の投稿のユーザー情報の取得に関する問題
は私が持っているものです。
クエリは、私が適切な関係あると信じて、私はそれぞれに定義されている私のユーザーとポストモデルでは、
public function index()
{
$posts = Post::with('user')->get();
return view('posts.index', compact('posts', $posts));
}
あり、ここで彼らはまったく同じです。
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function posts() {
return $this->hasMany('App\Post');
}
}
そしてポストは次のとおりです。
<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{私が手にエラーが、このようなすべてのポストを表示するとき、私は記事インデックスページのためのループで使用する自分の名前、など、ユーザーに関する情報なしでちょうどポストされてリターンです
protected $fillable = [
'title', 'content', 'followers',
];
public function user() {
return $this->belongsTo('App\User');
}
}
。
私が見逃したことはありますか?
ポストとユーザーはどのように接続されていますか? – Neat