Umar Akbar
Umar Akbar

Reputation: 133

updated_at column ambigious while updating with eloquent

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)

Upvotes: 3

Views: 5527

Answers (6)

Josh Alecyan
Josh Alecyan

Reputation: 1176

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

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

Upvotes: 2

Ruberandinda Patience
Ruberandinda Patience

Reputation: 3755

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.

Upvotes: 0

spaceemotion
spaceemotion

Reputation: 1546

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…

Upvotes: 0

Rahman Qaiser
Rahman Qaiser

Reputation: 672

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();

}

Upvotes: 0

Chamandeep
Chamandeep

Reputation: 116

If you use modal please update your modal with this line

public $timestamps = false;

try, it may be helpful

Upvotes: -1

muratyuksel
muratyuksel

Reputation: 21

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.

Upvotes: 2

Related Questions