简体   繁体   English

SQLSTATE [23000]:完整性约束违规:1052 列“created_at”在 order 子句中不明确 Laravel 8

[英]SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 8

I just don't know what's wrong with my code and why it produces this error:我只是不知道我的代码有什么问题以及为什么会产生此错误:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous (SQL: select * from cars inner join car_books on car_books . car_id = cars . id order by created_at desc) SQLSTATE [23000]:完整性约束违规:1052 列“created_at”在 order 子句中不明确(SQL:select * from cars inner join car_books on car_books car_id = cars id order by created_at desc)

Here's my code:这是我的代码:

public function carBook()
{
    $car_books = Car::join('car_books','car_books.car_id','=','cars.id')->latest()->get();
    return view('admin.car_book',compact('car_books'));
}

You have to order by cars.created_at or car_books.created_at .您必须通过cars.created_atcar_books.created_at

Both tables have created_at column, so it is ambiguous.两个表都有created_at列,所以它是模棱两可的。

The error is telling you that there is a created_at field in both the cars and car_books table.该错误告诉您carscar_books表中都有一个created_at字段。 You need to specify exactly which of those you want to use within the order by clause.您需要在order by子句中准确指定要使用的那些。 So either:所以要么:

order by cars.created_at desc

Or:或者:

order by car_books.created_at desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM