0š
For what do you have the parameter $image? This is not specified in your axios.post.
public function store(Request $request)
{
$category = new Category();
$input = $this->safeInput($request);
$category->fill($input);
dd($this->postImage($request));
$slug = $category->slug($category->title);
$category->slug = $slug;
if($request->has('active'))
{
$category->active = 1;
}else{
$category->active = 0;
}
$category_order = $category->order_number();
$category->order = $category_order;
$category->save();
}
public function postImage($request)
{
if($request->hasFile('image'))
{
$names = [];
foreach($request->file('image') as $image)
{
$destinationPath = 'product_images/category/';
$filename = $image->getClientOriginalName();
$image->move($destinationPath, $filename);
array_push($names, $filename);
}
$image = json_encode($names);
return $image;
}
}
0š
In your create.blade you use āformUrlā => route(ācategory.storeā), this route calls the āstoreā method, right? If so, it also needs to pass the $image parameter. It would be easier to identify the problem if we could se your web routes file too.
If route(ācategory.storeā) does call the store method you have a few options.
1 ā If you donāt really need the $image parameter for the store method, you could just remove it.
2 ā If you need it in a few cases, just make the parameter optional and check if itās received before handling it. Example: store(Request $request, $image = null)
3 ā If this parameter actually is required, you will have to pass it everytime, even when calling routes. Example: route(ācategory.storeā, [āimageā => $something]). Looking at your code at this moment in create.blade you donāt have the content to pass though, so I donāt think this is an option.
0š
If the $request is available there, Then there is no need to pass extra $image variable.
have you tried
dd($request)
or
print_r($request->toArray()); exit;
for see whatās in your request!
- [Vuejs]-Vue.js pass data to route using beforeRouteUpdate
- [Vuejs]-Module.exports is undefined when adding a class method
0š
The problem isnāt the image missing in the request object sent through the form, it is the second parameter required by the category.store method.
Even if you now send the image in the form with a hidden field, you would still need to pass it as a parameter everytime you call the category.store.
Your store method is defined like
store(Request $request, $image)
So, when you call this method, even if youāre just getting the route URL with route(ācategory.storeā), you do need to send the image parameter in this call.
Example:
route('category.store', ['image' => 'image id here']);
The same goes for the route definition in your web routes file. Youāre using a resource route, but laravel donāt expect a second parameter for the store method in a default resource, so you will need to change that.
/*
adds exception to the resource so it will not handle the store method
*/
Route::resource('admin/category', 'Admin\CategoryController')->except(['store']);
//adds a custom route that supports the $image parameter.
Route::post('admin/category/{image}', 'Admin\CategoryController@store')
Now, if youāre planning to send the image through the request object, you donāt need it as a second parameter, so the only thing you will need to change is to make your category.store method like that.
public function store(Request $request)
- [Vuejs]-Can I have a Vue app that has some static server rendered routes and some dynamic routes
- [Vuejs]-Proper way to show array inside an array with v-for