$name = $image->getClientOriginalName();
$path = Storage::putFile('images', $image);
Images upload for post
Good afternoon. I would like to know how to properly configure the controller so as to load multiple images for the post. Now the controller looks like this:
public function storePosts(Request $request)
{
$imagesLoad = Blog::create([
'title' => $request->title,
'slug' => $request->slug,
'description' => $request->description,
'categories_id' => $request->categories_id,
]);
if ($request->hasfile('images')) {
$images = $request->file('images');
foreach($images as $image) {
$name = $request->file('images')->getClientOriginalName();
$path = Storage::putFile('images', $request->file('images'));
Images::create([
'name' => $name,
'path' => $path
]);
}
}
dd($imagesLoad);
}
. When I upload files and check which fields were filled in via the dd command, I do not find the filled image field. The project has three tables Blog, Images_blog, Images Blog Migration:
$table->id();
$table->unsignedBigInteger('categories_id');
$table->foreign('categories_id')
->references('id')
->on('categories')
->onDelete('cascade');
$table->string('title');
$table->string('slug');
$table->text('description');
$table->timestamps();
Images Migration:
$table->string('name')->nullable();
$table->string('path')->nullable();
Images_Blog Migration:
$table->integer('images_id');
$table->integer('blog_id');
Model Images looks like this:
protected $table = 'images';
protected $fillable = ['name', 'path'];
public function imageForBlog()
{
return $this->belongsToMany(Blog::class);
}
The Blog model looks like this:
public function images()
{
return $this->belongsToMany(Images::class);
}
Laravel himself does not give any errors when loading, he specified a new array for the blog in the file system, so that when loading the files when creating a post, they fall into the post folder. Here is the filesystems code:
'blog' => [
'driver' => 'local',
'root' => storage_path('app/public/blog'),
'url' => env('APP_URL').'/storage',
'visibility' => 'blog',
],
How to make sure that when creating a post, the file is dropped with the name of the post, and entered into the database with the name and path to the image?
Please or to participate in this conversation.