您好我正在学习Laravel。 我有一个图片库,显示所有图片分页但我想只在另一页显示用户的照片 所以这是唯一用户照片显示的控制器
public function __construct()
{
$this->middleware('auth');
}
public function show(Photos $photos)
{
$user = auth()->user();
$images = Photos::all();
if($images->user->user_id==auth()->id())
{
return view('profile',['images'=>$images,
'user'=>$user]);
}
}
//This is my photos table
public function up()
{
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->unsignedinteger('user_id')->index();
$table->string('title');
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('photos');
}
public function up()
{
Schema::create('photos', function (Blueprint $table) {
$table->increments('id');
$table->unsignedinteger('user_id')->index();
$table->string('title');
$table->string('image');
$table->timestamps();
});
}
'
这就是路线:
//users gallery
Route::get('/profile','ProfileController@show')->name('profile');
这是我的照片模型:
// protected $guarded = [];
protected $fillable = [
'user_id','title','image'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
这是我的用户模型:
protected $fillable = [
'username','firstname','lastname','phone', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function photos()
{
return $this->hasMany(Photos::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function favorite()
{
return $this->hasOne(Favorites::class);
}
问题是仍然显示所有照片不仅是用户的照片。请告诉我,我做错了什么?
答案 0 :(得分:0)
控制器中的show
方法应为
public function show()
{
return view('profile',['images' => Photo::where('user_id', $user->id)->paginate(10),'user' => $user]);
}