Good day.
The question is the following:
I have a PictureJobs database which consists of four fields (img_alt, img_title, img_path, works_id).
Also in the controller there is a form for adding the main image for using the library.
How to correctly implement the form for sending files to the database so that you can pull them out into your project.
** UploadController**
$this->validate($request, [
'title' => 'required',
'slug' => 'required',
'url' => 'max:100',
'description' => 'required',
'tools_one' => 'nullable',
'tools_two' => 'nullable',
'date_production' => 'required',
]);
$work = new Works([
'title' => $request->get('title'),
'slug' => $request->get('slug'),
'url' => $request->get('url'),
'description' => $request->get('description'),
'tools_one' => $request->get('tools_one'),
'tools_two' => $request->get('tools_two'),
'date_production' => $request->get('date_production'),
]);
if ($request->hasFile('image') && $request->file('image')->isValid()){
$work->addMediaFromRequest('image')->toMediaCollection('works');
};
if ($request->hasFile('images')){
$images = $work->images;
$files = $request->file('images');
foreach ($files as $file) {
$extention = $file->getClientOriginalExtension();
$fileNameToStore = 'works/' . $work->id . '/images'.sha1_file($file).".".$extention;
$path = $file->storeAs('public', $fileNameToStore);
$images[] = "/storage/".$fileNameToStore;
}
$work->images = $images;
};
dd($work);
Form
<form action="{{ route('work.store') }}" method="post" enctype="multipart/form-data">
<div class="flex flex-wrap justify-evenly">
<label for="Images" class="label-forms">
<input type="file" name="images">
<input type="text" name="img_alt">
<input type="text" name="img_title">
</div>
</form>
model Work
public function images() {
return $this->hasMany(WokrsImages::class);
};
**model Images**
```php
public function works() {
return $this->belogsTo(Wokrs::class);
};