3

I have 3 table cart,pharmacy and cart_pharmacies

cart table

cart_id | user_id | total_price | status | created_at | updated_at

pharmacy table

pharmacy_id | name | address_id | created_at | updated_at

cart_pharmacies table

cart_pharmacies_id | cart_id | pharmacy_id | created_at | updated_at

In cart modal i define relation

   public function pharmacy()
    {
        return $this->belongsToMany('App\Pharmacy','cart_pharmacies','cart_id','pharmacy_id');
    }

In parmacy modal i define

public function cart()
{
    return $this->belongsToMany('App\Cart','cart_pharmacies','pharmacy_id','cart_id');
}

In controller i have pharmacy_id i am trying to update cart status with code

$pharmacy_id=$request->input('pharmacy_id');
$pharmacy=  Pharmacy::findOrFail($pharmacy_id);
$pharmacy->cart()->update(['status'=>1]);

but it is giving me error

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 
'updated_at' in field list is ambiguous (SQL: update `cart` inner join 
`cart_pharmacies` on `cart`.`cart_id` = `cart_pharmacies`.`cart_id` set 
`status` = 1, 
`updated_at` = 2016-05-31 07:14:47 where `cart_pharmacies`.`pharmacy_id` = 5)
Umar Akbar
  • 123
  • 2
  • 7
  • 1
    How are you passing updated_at value? – chandresh_cool May 31 '16 at 07:21
  • you can see on my code i am not passing updated_at values – Umar Akbar May 31 '16 at 07:23
  • That is the problem you are missing the single quotes around the updated_at value. There seems to be some common code which is passing the value. Have a look at it. – chandresh_cool May 31 '16 at 07:24
  • Not this is not a problem the problem is botth table cart_pharmacies and cart have updated_at column it cause ambiguous. i don't know how to solve that problem – Umar Akbar May 31 '16 at 07:29
  • If you feel this is not the issue, just try running the output sql you added i.e. (update `cart` inner join `cart_pharmacies` on `cart`.`cart_id` = `cart_pharmacies`.`cart_id` set `status` = 1, `updated_at` = 2016-05-31 07:14:47 where `cart_pharmacies`.`pharmacy_id` = 5 ) in your phpmyadmin or any sql ui, you would get same error. – chandresh_cool May 31 '16 at 07:30
  • 1
    Actually the quote thing is handle by default in laravel. We have nothing to do with that. And even if i consider that its the problem with the quotes then the error will be different than this one. It will be a syntax error not about the ambigious. I am more concerned about how to handle the alias in the elequoent than handling the quotes as this can be done by laravel because thats the framework thing. – Umar Akbar May 31 '16 at 07:44
  • 1
    I know it's an old question, but maybe duplicated of [this](https://stackoverflow.com/questions/25320335/eloquent-update-failing-due-to-updated-at-table-ambiguity/25325707), and there is an useful answer to this question: https://stackoverflow.com/a/25325707/2116875 – rneves Jul 06 '17 at 19:00
  • @chandresh_cool Did you understand that those queries are composed by Eloquent? – Jhourlad Estrella Aug 31 '18 at 03:18

6 Answers6

2

SQL query error says there is multiple columns named 'updated_at' (both main and related tables) and cant decide which to update. It is a bug and I think there is no way to solve with eloquent as updated_at will be added at the end automatically without the table name specified.

2
$pharmacy->cart()->toBase()->update(['status'=>1, 'cart.updated_at' => now()]);

Use toBase() method and add 'cart.updated_at' => now() to update data

Josh Alecyan
  • 1,136
  • 8
  • 19
0

Your are using timestamps in your pivot table which could be automatically controlled by Eloquent with withTimestamps() method.

In your parmacy modal:

public function cart() {

return $this->belongsToMany('App\Cart','cart_pharmacies','pharmacy_id','cart_id')->withTimestamps();

}

Rahman Qaiser
  • 664
  • 5
  • 18
0

The only solution I found so far is the following:

Given that you have an array of values to update ($values) and your query ($query), you simply create a base query and add your columns manually:

$now = now();

// Manually add the timestamp columns
$values = $values + [
    'table.' . Model::CREATED_AT => $now,
    'table.' . Model::UPDATED_AT => $now,
];

$query->toBase()->update($values);

Model is an import of Illuminate\Database\Eloquent\Model, table is the name of the table you really want to update.

Not the best but it works, maybe this will become a feature in the future…

spaceemotion
  • 1,404
  • 4
  • 24
  • 32
0

Here is Link to overcome the issue

https://github.com/laravel/framework/issues/13909

The use of toBase() before updating assist me so much to solve the issue happy coding.

Ruberandinda Patience
  • 3,435
  • 3
  • 20
  • 18
-1

If you use modal please update your modal with this line

public $timestamps = false;

try, it may be helpful

Chamandeep
  • 116
  • 2
  • 8