Ali Khiti Ali Khiti - 6 months ago 46
PHP Question

HasOneOrMany.php line 221 error in laravel when I try to upload an image

I'm working on a laravel project, I want to allow users to post images but I get this error:

FatalThrowableError in HasOneOrMany.php line 221: Type error: Argument
1 passed to
Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an
instance of Illuminate\Database\Eloquent\Model, none given, called in
my statuses controller.

Here's my statuses controller code:

class StatusController extends Controller {
public function postStatus(Request $request){
$this->validate($request, [
'status' => 'required|max:250',
'body' => $request->input('status'),

$image = $request->file('image');
$filename = Auth::user()->username . time() . "." . $image->getClientOriginalExtension();
->save(public_path("/uploads/images/" . $filename ));

$statuses = Auth::user()->statuses();
$statuses->image = $filename;

return redirect()->route('home')->with('info', 'Status posted.');

In my user model I have this

public function statuses() {
return $this->hasMany('App\Models\Status', 'user_id');

any suggestions please?


When you do this:

$statuses = Auth::user()->statuses();

You get a Collection of statuses. You don't need to do this because you created a new status on line 6. Change your code to:

Line 6:

$status = Auth::user()->statuses()->create([
        'body' => $request->input('status'),

Remove line 16:

$statuses = Auth::user()->statuses(); //Remove this line

And Change:

    $status->image = $filename; //Note we changed the var name to the created above.