[Vuejs]-How do I get my variable to show in my store function

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!

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)

Leave a comment