What does the entire form look like? What route are you sending the form to?
Laravel 5.2 save data to mysql
I am trying to insert data in laravel 5.2 on my mysql db,getting the following error.
MethodNotAllowedHttpException in RouteCollection.php line 219:
in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 823
My route is
Route::post('/add-to-cart/{id}',['uses' => 'ProductController@AddToCart']);
In form i use
<a href="{{ URL::to(''add-to-cart/'.$item->id) }}">{{ Form::submit('Add To Cart',array('class'=>'btn btn-success')) }}</a>
What I am doing wrong. Any help is relly appreciated.
Thanks for the reply. This is the form i am using to add data
@foreach($items as $item)
<div class="col-md-3">
<div class="thumbnail" style="height:30%">
<div class="caption">
<center>
<big><b>{{ $item->name }}</b></big>
<a href="{{ URL::to('show_single_product/'.$item->id) }}"><img src="{{ $item->image }}" height=45%></a>
Product Price: ${{ $item->price }}
<br>
Product Size: {{ $item->size }}
<a href="{{ URL::to('add-to-cart/'.$item->id) }}">{{ Form::submit('Add To Cart',array('class'=>'btn btn-success')) }}</a>
</center>
</div>
</div>
</div>
@endforeach
You need to open and close your form. When you open your form, you declare the request type, POST is default, and the route. Something like this:
{!! Form::open(array('url' => '/add-to-cart')) !!}
//
{!! Form::close() !!}
The docs for the Forms package are really good: https://laravelcollective.com/docs/5.2/html
Thanks for the reply. The problem is i have to execute the link button to submit the data. I don't know this is the right way to do it. This link will actually show the button. The page contains images and add button. When add to cart button pressed the data is posted into the db and show the cart.
{{ Form::submit('Add To Cart',array('class'=>'btn btn-success')) }}
Could you post your entire view? Be sure to post it between three backticks (```) so the code is formatted nicely.
```
// Your code
```
Thanks for the reply. My form , route, and controller
When the button under the image is clicked, the button calls the form . It is working until the button click.
My form
@extends('layouts.app')
@section('content')
@foreach($items as $item)
<div class="thumbnail" style="height:30%">
<div class="caption">
<big><b>{{ $item->name }}</b></big>
<a href="{{ URL::to('show_single_product/'.$item->id) }}"><img src="{{ $item->image }}" height=45%></a>
Product Price: ${{ $item->price }}
Product Size: {{ $item->size }}
<a href="{{ URL::to('add-to-cart/'.$item->id) }}">{{ Form::submit('Add To Cart',array('class'=>'btn btn-success')) }}</a>
</div>
@endforeach
{!! Form::close() !!}
{{ $items->links() }}
```
my route
```
Route::post('/add-to-cart/{id}',['uses' => 'ProductController@doAddToCart']);
My controller
public function doAddToCart(Request $request)
{
$id = $request['id'];
$item_id = $request['item_id'];
$name = $request['name'];
$size = $request['size'];
$price = $request['price'];
$cart = new ShoppingCart();
$cart->id = $id;
$cart->user_id = Auth::user()->id;
$cart->item_id = $item_id;
$cart->name = $name;
$cart->size = $size;
$cart->price = $price;
$cart->quantity = 1;
$cart->save();
return Redirect::to('my-cart'); */
}
A few things. You aren't opening your form so your submit button has no where to send your data to. Also, your submit button is wrapped in an anchor tag so you arent even submitting anything, you're doing a get request to '/add-to-cart/{id}', which is why you are getting the method not allowed error. Last, you're trying to post to a route with a variable which is going to be an issue I think.
Try this.
Form:
@extends('layouts.app')
@section('content')
{!! Form::token() !}}
@foreach($items as $item)
{!! Form::open(array('url' => 'add-to-cart')) !!}
<div class="thumbnail" style="height:30%">
<div class="caption">
<big><b>{{ $item->name }}</b></big>
<a href="{{ URL::to('show_single_product/'.$item->id) }}"><img src="{{ $item->image }}" height=45%></a>
Product Price: ${{ $item->price }}
Product Size: {{ $item->size }}
{{ Form::hidden('item_id', $item->id) }}
{{ Form::submit('Add To Cart',array('class'=>'btn btn-success')) }}
</div>
</div>
{!! Form::close() !!}
@endforeach
{{ $items->links() }}
Route:
Route::post('/add-to-cart', ['uses' => 'ProductController@doAddToCart']);
Controller:
public function doAddToCart(Request $request)
{
$item_id = $request['item_id'];
$item = Item::find($item_id);
$cart = new ShoppingCart();
$cart->id = ??;
$cart->user_id = Auth::user()->id;
$cart->item_id = $item_id;
$cart->name = $item->name;
$cart->size = $item->size;
$cart->price = $item->price;
$cart->quantity = 1;
$cart->save();
return Redirect::to('my-cart');
}
Not quite sure what the difference between $id and $item_id is in your code...
This should work. The only thing that might give you trouble is the multiple forms on the page. If it doesnt work, try putting the item id as a property on the submit tag.
Thanks for your valuable Info. You are great. My concern is how can this statement calls the route and add to the cart. The button needs to be clicked . That is why i put inside the anchor. Right now it is showing the button, but no action. The item_id is the pri. key of cart table id is the pri.key of items table.
{{ Form::hidden('item_id', $item->id) }}
{{ Form::submit('Add To Cart',array('class'=>'btn btn-success')) }}
I tried like this but not able to click the button
{{ Form::hidden('item_id', $item->id) }}
{{ Form::submit('Add To Cart',array('class'=>'btn btn-success','action' => 'CartController@doAddToCart')) }} </a>
The action should be in the form open tag, not in the form submit tag. Form buttons can be clicked. Check out this example: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_submit
Thanks for your help. It was wonderful. I solved the problem, but it took some time . Basically what i did was did a get request the url below and inside the controller i use my method to query the database. It is working fine as expected now.
<a href="{{ URL::to('add-to-cart/'.$item->id) }}">{{ Form::submit('Add To Cart',array('class'=>'btn btn-success')) }}</a>
Keep up the good work. Your timely help is amazing.
Please or to participate in this conversation.