Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Randy_Johnson's avatar

Eloquent Not working

I have a database table called 'images' and a model called 'Image' but when I run

Image::create([
                'user_id' => $user->id,
                'url' => 'storage/img/user/'.$imageName
            ]);

Nothing happens. Running the next command works perfectly.

DB::table('images')->insert([
                'user_id' => $user->id, 
                'url' => 'storage/img/user/'.$imageName
            ]);
0 likes
5 replies
Randy_Johnson's avatar

I found the problem - didn't have

use App\Models\Image;

Why it wasn't showing it as an error I don't know but I fear it had something to do with the try catch statement.

MichalOravec's avatar

When it's hasOne why do you named relationship in plural form?

public function image()
{
    return $this->hasOne('App\Models\Image');
}
$user->image()->create(['url' => 'storage/img/user/'.$imageName]);
Randy_Johnson's avatar

I just copied the db name. I have no idea how the eloquent works. Its like a magic box.

Snapey's avatar

relationships should be named according to if the relationship brings a single model or multiple models.

  • belongsTo should be singular method name

  • hasOne should be singular method name

  • hasMany should be plural method name

  • belongsToMany should be plural method name

1 like

Please or to participate in this conversation.