'Laravel 9 - name of image does not get saved in the database, while the image saves
The image is getting succesfully saved, the imageName is getting created, but when I try to save imageName in the image name, it shows as NULL. The odd thing is that whatever I write for that column, it still shows as NULL. The image column in the database is a simple varchar (250) column. What's wrong? I can't figure out.
if($request->hasfile('file')) {
$image = $request->file('file');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$request->file = $imageName;
$car = new Car([
'brand' => $request->brand,
'model' => $request->model,
'fuel' => $request->fuel,
'consumption' => $request->consumption,
'body'=> $request->body,
'seats' => $request->seats,
'transmission' => $request->transmission,
'year' => $request->year,
'price' => $request->price,
'image' => $request->file
]);
Image::make($image)->resize(500, 500)->save(public_path('/images/cars/' . $imageName));
$car->save();
}
return response()->json('Car successfully added');
Solution 1:[1]
Please check your Model it should be something like this:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Crud extends Model
{
protected $fillable = [
'brand' ,
'model' ,
'fuel',
'consumption' ,
'body',
'seats',
'transmission' ,
'year' ,
'price',
'image',
];
}
/////DO NOT FORGET TO CHANGE Crud EXTENTION WITH YOUR MODEL NAME/////
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | a bcd |