Laravel DB :: raw(选择)

时间:2018-05-15 05:37:49

标签: mysql database laravel laravel-5.6

SQL

Trip::select(
        'trips.id',

        DB::raw('
            (      
                    SELECT 
                        DATE_FORMAT(
                            FROM_UNIXTIME(
                                UNIX_TIMESTAMP(trips.date_trip) + start_time
                            )
                            , "%H:%i"
                        ) 
                    FROM 
                        trip_regions 
                    WHERE 
                        region_id="'.$from.'" and trip_id=trips.id
            ) 
                    as date_star
        '),

        DB::raw('
                (
                    SELECT 
                        DATE_FORMAT(
                            FROM_UNIXTIME(
                                UNIX_TIMESTAMP(trips.date_trip) + start_time
                            )
                            , "%H:%i (%d %M)"
                        ) 
                    FROM 
                        trip_regions 
                    WHERE 
                        region_id="'.$where.'" and trip_id=trips.id
                ) 
                    as date_end
        '),



        'trips.regions','trips.type_trips','trips.mail','trips.price','trips.price_mail',
        'trips.passenger','trips.min_weigh_ton','trips.max_weigh_ton','trips.contract','trips.multiple_booking',
        'trips.smoking','trips.animals','trips.people_back','trips.small_cargo','trips.with_place','trips.delivery_door',
        'trips.date_trip','trips.date_back','trips.user_id','trips.car_id',
        DB::raw('
            (
                SELECT
                        DATE_FORMAT(
                            FROM_UNIXTIME(
                                UNIX_TIMESTAMP(trips.date_trip) + start_time
                            )
                            , "%Y-%m-%d %H:%i:%s"
                        ) as time
                FROM
                    trip_regions
                WHERE 
                    region_id="'.$from.'" and trip_id=trips.id
            ) as date
        ')


    )

        ->where(function($query) use ($req) {
            if($req->type == 1){
                $query->where('trips.type_trips','passenger');
            }elseif($req->type == 2){
                $query->where('trips.mail',1);
            }elseif($req->type == 3){
                $query->where('trips.type_trips','cargo');
            }
        })
        ->where('trips.regions','like','%|f|'.$from."|%")
        ->where('trips.regions','like','%|w|'.$where."|%")
       //->whereBetween('date',[$date, $date_today])
        ->havingRaw('date between "'. $date.'" and "'. $date_today.'"')

        ->with([
            'user_id' => function($query){
                $query->select('id','firstname','lastname','avatar');
            },
            'car_id' => function($q){
                $q->select('id','car_name','color','year')->with(
                    ['car_name' => function($qq){
                        $qq->select('id','name','car_models_id')->with(
                            ['car_models_id' => function($qqq){
                                $qqq->select('id','name');
                            }]
                        );
                    }]
                );
            },
            'trips_regions' => function($query) use ($from, $where){
                $query->select('id','region_id','trip_id','price','km','type')
                    ->whereIn('region_id',[$from,$where])
                    ->orderBy(DB::raw('FIELD(region_id, '.$from.','.$where.') '))
                    //->where('region_id',$where)
                    ->with([
                        'region_id' => function($qqq) {
                            $qqq->select('id', 'name_ru');
                        }
                    ]);
            },
            'orders' => function($order){
                $order->select('id','trip_id','count_order');
            }
        ])
        ->orderBy('date','asc')
        ->paginate(10);

这是完整的代码。我从trip_regions中获取了这个并添加了trips.date_trip,并且应该检查一下旅行日期,但是你可以看到它不可能有头痛(

错误

"Column not found: 1054 Unknown column 'date' in 
'having clause' (SQL: select count(*) as aggregate from `trips` where 
(`trips`.`type_trips` = passenger) and .... or .... and ... having date between "2018-05-14 00:00:00" and "2018-05-14 
23:59:59" and date between "2018-05-14 00:00:00" and "2018-05-14 23:59:59")"

大家好,我尝试了很多次,没有任何事情发生,请帮助这段代码2天我找不到解决方案。以前谢谢你

0 个答案:

没有答案