Call to a member function extension() on array I have tried to upload the image using Postman and the image is stored in the preferred directory and the name and is stored in database but i am getting the error response.
public function store(Request $request)
{
$design_studio = new DesignStudio;
$design_studio->user_id = 1;
$design_studio->title = $request->title;
$design_studio->lang = $request->lang;
$design_studio->body = $request->body;
if($request->has('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/slideShow'), $filename);
$design_studio->image = $request->file('image')->getClientOriginalName();
}
if ($sliders = $request->file('sliders')) {
foreach ($sliders as $slider) {
$filename = rand(111111, 999999);
$extension = $request->file('sliders')->extension();
$getFileExt = $filename + '.' +$extension;
$slider->move(public_path('images/design-studios/'), $getFileExt);
$data[] = $getFileExt;
}
}
$design_studio->sliders = json_encode($data);
$design_studio->save();
$design_studio->categories()->attach($request->category);
return redirect()->route('design-studios.index');
}
Considering you're doing a foreach loop, I believe your $sliders variable is indeed an array, and therefore the extension() method cannot be called.
What you need to do is get the file extension of each file in your loop.
foreach ($sliders as $slider) {
$filename = rand(111111, 999999);
$extension = $slider->extension();
$getFileExt = $filename + '.' +$extension;
$slider->move(public_path('images/design-studios/'), $getFileExt);
$data[] = $getFileExt;
}
Please sign in or create an account to participate in this conversation.