doulan9287 2018-04-15 13:18
浏览 30
已采纳

我只想显示经过身份验证的用户图片

Hello I am learning Laravel. I have a picture gallery tha shows all pictures paginated but I want to only show on another page the users photos So this is the controller for the only users photos to show

    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();
        });
    }
'

And this is the route:

//users gallery
Route::get('/profile','ProfileController@show')->name('profile');

This is my photos model:

 // protected $guarded = [];
    protected $fillable = [
        'user_id','title','image'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

And this is my user model:

 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);
   }

The problem is that is still shows all the photos not only the user's one. Please tell me what is that I am doing wrong?

  • 写回答

1条回答 默认 最新

  • dongnan4571 2018-04-15 13:22
    关注

    your show method in your controller should be

    public function show()
    {           
        return view('profile',['images' => Photo::where('user_id', $user->id)->paginate(10),'user' => $user]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了