LMAO3D's avatar

Laravel file upload with auth user

Hi everyone, I am working on laravel project in which the logged in user can upload a file and the uploaded file will only be displayed to that logged in user on his/her dashboard. looking for help

0 likes
1 reply
nolros's avatar

@lmao3d that is a very big question. Suggest you look at documentation and try and then post when you run into issues, but as a starting point. There are a number of approaches in your controller. Use auth in each controller method or the right way middleware - both are ok if you are starting out.


  public function saveImage(Request $request)
  {
    if (auth()->check()) {
      if ($request->has("image")) {
        $image = $request->image;
        return $image->storeAs('images', $image->getClientOriginalExtension());
      }
    }

    return response()->json(['Unauthorized']);
  }

  public function viewImage($id)
  {
    if (auth()->check()) {
      $image = Image::find($id);
      return view('dashboard.main')->with(['image' => $image]);
    }

    return response()->json(['Unauthorized']);
  }



// OR Middlewar

  public function __construct()
  {
    $this->middleware('auth');
  }

  public function viewImage(Request $request)
  {
    if ($request->has("image")) {
      $image = $request->image;
      return $image->storeAs('images', $image->getClientOriginalExtension());
    }
  }

  public function viewImage($id)
  {
      return view('dashboard.main')->with(['image' => Image::find($id)]);
  }

Please or to participate in this conversation.