Home » Php » Unable to update particular column of mysql database

Unable to update particular column of mysql database

Posted by: admin November 30, 2017 Leave a comment

Questions:

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'=>'[email protected]
));

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?

Answers:

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>

Questions:
Answers:

use url as that

<a href="{!! URL::to('accept/$productAccept->id') !!}">Accept</a>

Questions:
Answers:
@foreach($product as $productAccept)
    <a href="{{route('accept-product', ['id' => $productAccept->id] )}}">Accept</a>
@endforeach

Your problem is from this: /{!! $productAccept->id !!}

Questions:
Answers:

Try this,

<a href="{{ route('accept-product', $productAccept->id)}}">Accept</a>

Questions:
Answers:

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

  1. You can do model binding

Route::model('product', 'Products');

and the route would be

Route::get('accept/{product}',array(
    'as'=>'accept-product',
    'uses'=>'[email protected]
));

and you can get product in the controller as

public function updateProduct($product ) 
{
    $product->conditions = 'accept';
    $product->save();
}