Home > Blockchain >  I seem to have issues uploading images. i keep getting "Cannot use object of type App\Http\Co
I seem to have issues uploading images. i keep getting "Cannot use object of type App\Http\Co

Time:12-25

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function create()
    {
        return view('posts/create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'caption' =>  'required',
            'image' =>  'required|mimes:jpeg,png,jpg,gif,svg|max:2048',

        ]);

        $imagePath = request('image')->store('uploads', 'public');

        auth()->user()->posts()->create([
            'caption' => $this['captions'],
            'image' => $imagePath,
        ]);

        return redirect('/profile/' . auth()->user->id);
    }
}

CodePudding user response:

My assumption is you are trying to access one of the inputs that were validated when creating the Post for the User:

// getting the inputs that were validated as an array
$data = $this->validate(...);

...

auth()->user()->posts()->create([
    'caption' => $data['caption'], // <------- accessing data array
    'image' => $imagePath,
]);

If your version of Laravel isn't returning the validated data from the validate call you can access the data from the Request itself:

'caption' => $request->input('caption')
  • Related