[Vuejs]-Pass data from vue.js to laravel 9

0👍

This might be your issue,

you are passing this object as the post data

{menu_id: item.id}

then you call a non-existing input in your controller $request->input('id')

$menu_id = $request->input('id');
$menu = Menu::find($menu_id);

it should be $request->input('menu_id');

but again, check your logs to see the actual error thrown

also, you should add a validation in your controller to make sure the ID you pass exist in your table

public function store(Request $request) {
    
    $request->validate([
        'menu_id' => 'required|exists:menus,id'
    ]);
    
    
    $menu_id = $request->input('menu_id');
    $menu = Menu::find($menu_id);
    $cart=new Cart();
    $cart->table=$request->table;
    $cart->menus_id=$menu_id;
    $response=$cart->save();
}

0👍

I passed menu_id from post menu.vue but $cart->table gets null value so I got this error : Integrity constraint violation
so for now I give this value directly

    public function store(Request $request)
{
    $menu_id = $request->input('menu_id');
    $menu = Menu::find($menu_id);
    $cart = new Cart();
    $cart->menus_id = $menu_id;
    $cart->table = 2;
    $response = $cart->save();
}

Thank you guys for helping me 😊

Leave a comment