PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] I seem to have issues uploading images. i keep getting "Cannot use object of type App\Http\Controllers\PostsController as array. "

 January 10, 2022     controller, laravel, php     No comments   

Issue

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

Solution

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')


Answered By - lagbox
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing