Reputation: 15
I want to store a file name to my db so it can retrieve that image and display it, I have implemented something that prevents files with the same name being added to my images folder by adding the time of upload in front of the actual file name as seen in the '$newImageName' variable, my question is how do I pass the '$newImageName' to be stored as that in the db given with the current code I have?
public function store(Request $request)
{
$this->validate($request, [
'subject' => 'required|min:5',
'tags' => 'required',
'thread' => 'required|min:25',
'image' => 'required|mimes:png,jpg,jpeg'
]);
$newImageName = time() . '-' . $request->subject . '.' . $request->image->extension();
$request->image->move(public_path('images'), $newImageName);
$thread = auth()->user()->marketthreads()->create($request->all());
$thread->tags()->attach($request->tags);
return back()->withMessage('Market thread has been created');
Any help will be highly appreciated
Thank you :)
Upvotes: 0
Views: 1695
Reputation: 58
You need to create a table in your database for storing the images. In your table, you basically need an ID and the name of the image file in your storage, in your case called $newImageName. You can do this by migration as below:
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('filename');
$table->timestamps();
});
In your controller whenever you are uploading a new item, do accordingly:
$image = new Image();
$image->filename = $newImageName;
$image->save();
This will save the Image filename in your DB for further usage.
Upvotes: 1