Reputation: 2647
I am not able to update a particular column of my database. In my database there is a "conditions" column which has a default value of "pending". I want to update this column with value "accept", but I am unable to do so.
My routes.php
Route::get('accept/{id}',array(
'as'=>'accept-product',
'uses'=>'GoodsController@updateProduct
));
My controller:
public function updateProduct($id)
{
$product = Products::findOrFail($id);
$product->conditions = 'accept';
$product->save();
}
My view:
@foreach($product as $productAccept)
<a href="{{URL::route('accept-product')}}/{!! $productAccept->id !!} ">Accept</a>
@endforeach
When I clicked accept button, the url is like this
http://localhost/project/public/accept/{id}
Which I want like this:
http://localhost/project/public/accept/7
I mean I am unable to replace {id}
to integer like 7
,8
etc.
Is something wrong in my routes
?
Upvotes: 4
Views: 187
Reputation: 514
The most apparent issue that I see is may be that the the page is not being redirecting properly. I mean laravel may not be hitting that method of that controller and because there are no error message I am not sure.
One problem that can be is that you have already defined the route before somewhere in the code and it is being overwritten. If there are any error messages please share.
There are few improvements that I can see that you can make to the code above. 1. change
@foreach($product as $productAccept)
<a href="{{URL::route('accept-product')}}/{!! $productAccept->id !!} ">Accept</a>
@endforeach
to
@foreach($product as $productAccept)
<a href="{{ route('accept-product', [$productAccept->id]) }}">Accept</a>
@endforeach
you can pass the id as second parameter to the route
function( or URL::route
), route which is a helper function same same URL::route
Route::model('product', 'Products');
and the route would be
Route::get('accept/{product}',array(
'as'=>'accept-product',
'uses'=>'GoodsController@updateProduct
));
and you can get product in the controller as
public function updateProduct($product )
{
$product->conditions = 'accept';
$product->save();
}
Upvotes: 0
Reputation: 875
Try this,
<a href="{{ route('accept-product', $productAccept->id)}}">Accept</a>
Upvotes: 2
Reputation: 17545
@foreach($product as $productAccept)
<a href="{{route('accept-product', ['id' => $productAccept->id] )}}">Accept</a>
@endforeach
Your problem is from this: /{!! $productAccept->id !!}
Upvotes: 2
Reputation: 5443
use url as that
<a href="{!! URL::to('accept/$productAccept->id') !!}">Accept</a>
Upvotes: 2
Reputation: 9520
Your defined route expects one extra parameter - id. You need to pass the product id in the route function as extra parameter like this:
<a href="{{URL::route('accept-product', ['id' => $productAccept->id])}}">Accept</a>
Upvotes: 5