简体   繁体   中英

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

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

You'll need to specify, in latest() the full column. latest('process.created_at') or instead of using latest() use a custom orderBy.

Thats because you are querying the 'created_at' column from two tables. You have to specify wich colums you need, for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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