SQLSTATE[23000]:违反完整性约束:1052 顺序子句中的列 'created_at' 不明确 Laravel 5.5

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

我只是不知道我的代码有什么问题以及为什么它会产生这个错误

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous (SQL: select * from processes inner join bags on processes.bag_id = bags.id where bags.type = Recyclable and date(processes.created_at) = 2018-09-18 00:00:00 order by created_at desc limit 1) and here's my code

$bag = Bagcollect::join('bags', 'bagcollects.bag_id', '=', 'bags.id')
        ->select('bags.type')
        ->where('bagcollects.bag_id', $request->input('bag_id'))
        ->first();

   //this query produce error
    $processexist = Process::join('bags', 'processes.bag_id', '=', 'bags.id')
        ->where('bags.type', $bag->type)
        ->whereDate('processes.created_at', Carbon::today())
        ->latest()
        ->first();

您需要在 latest() 中指定完整的列。 latest('process.created_at') 或不使用 latest() 而使用自定义 orderBy。

那是因为您正在从两个表中查询 'created_at' 列。您必须指定您需要的列,例如:

$processexist = Process::join('bags', 'processes.bag_id', '=', 'bags.id')
        ->select('bags.column1', 'bags.columns2')
        ->where('bags.type', $bag->type)
        ->whereDate('processes.created_at', Carbon::today())
        ->latest()
        ->first();