Isalanctos's avatar

Model::create is not storing a file's URL in the database

My database fields looks like this(keep in mind that i'm using a laravel migration as an example):

    $table->id();
    $table->string('name');
    $table->string('genre');
    $table->longText('image')->nullable();
    $table->timestamps();

When the user store something, i run this command in my controller file(The 'image' field is optional, that's why it is not present in validate():

public function store(Request $request){

$formFields = $request->validate([
    'name' => ['required', Rule::unique('game', 'name')],
    'genre' => 'required',
    'submitGame' => 'required'
]);
//unset($formFields['submitGame']);

//game image
if($request->hasFile('image')){
    
    $image = $request->file('image')->store('gamePoster', 'public');
}

$insert = [
    'name' => $formFields['name'],
    'genre' => $formFields['genre'],
    'image' => $image
];
//storing in database
Game::create($insert);

The image is being uploaded to my folder succesfully, the problem is that somehow the 'image' field does'nt go to the database.

it is really confusing, since the command $image = $request->file('image')->store('gamePoster', 'public'); actually returns a string, i already checked it multiple times. The $image variable(unless the user did not upload any file at all) is never empty.

0 likes
8 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Check $fillable on the model. Maybe its missing 'image'

2 likes
vincent15000's avatar

@Sinnbeck I thought that mass assignment was only when using fill(). I will read the documentation once again ;).

Isalanctos's avatar

Yup, i just somehow managed to forget about the $fillable. Thank you for the attention guys

1 like

Please or to participate in this conversation.