Level 17
any ideas?
I have cars , images and car_image tables
in addController.php I get an array of images name and save to storage floder and database too.
store function
public function store(Request $request)
{
$array = [];
$this->validate($request, [
'profile_image.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
]);
$car = Car::create([
'user_id' => $request->user_id,
]);
if ($request->hasFile('profile_image')) {
foreach ($this->uploadImage($request) as $image) {
$array[] = Photo::create(['image' => $image])->id;
}
$car->images()->attach($array);
}
return redirect()->back()->with('success', "image successfully uploaded!!");
}
and uploadImage function
protected function uploadImage($request){
$imageall = [];
foreach($request->file('profile_image') as $file){
$filenamewithextension = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$filenametostore = 'getauto_'.uniqid().'.'.$extension;
$imageall[] = $filenametostore;
Storage::put('public/cars/'. $filenametostore, fopen($file, 'r+'));
Storage::put('public/cars/thumb/'. $filenametostore, fopen($file, 'r+'));
$thumbnailpath = public_path('storage/cars/thumb/'.$filenametostore);
$mainimagepath = public_path('storage/cars/'.$filenametostore);
}
return $imageall;
}
the problem is updating function how can i update and delete which related user images ??
You can use foreign key constraints and on delete cascade for the database part.
https://laravel.com/docs/5.7/migrations#foreign-key-constraints
As for the files loop over the ones connected to the user and use unlink
Please or to participate in this conversation.