在两个参数之间搜索自定义查询

时间:2018-03-12 16:49:44

标签: mysql laravel

当我在两个参数(date_min和amp_max_max)之间进行搜索时,我想运行一个查询问题。

在我的控制器中,我使用过滤器在不同的参数中进行搜索,并添加了一个orWhere条件来选择两个日期之间的结果,但查询结果真的很奇怪:

  

完整性约束违规:1052列'created_at'在哪里   条款含糊不清

     

Connection-&gt; runQueryCallback('select count(*)as aggregate from   <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit.vintage.version}</version> <scope>test</scope> </dependency> ... <properties> <junit.version>4.12</junit.version> <junit.jupiter.version>5.1.0</junit.jupiter.version> <junit.vintage.version>5.1.0</junit.vintage.version> <junit.platform.version>1.1.0</junit.platform.version> </properties> indemnites上的structures已加入structuresid =   indemnitesclub_id其中club_id =?要么   indemnitescreated_at之间?和?和created_at =?要么   indemnitescreated_at之间?和?和created_at =?要么   indemnitescreated_at之间?和?和total!=?通过...分组   club_id',数组('6','2018-03-12','2018-03-31','12 -03-2018',   '2018-03-12','2018-03-31','31 -03-2018','2018-03-12','2018-03-31',   0),object(Closure))

这是我的控制器:

公共功能索引(请求$请求){

$structure = Structure::select('num_structure', 'nom_structure' , 'id')
    ->where('type_structure_id' , '4')
    ->get()
    ->mapWithKeys(function($i) {
        return [$i->id => $i->num_structure.' - '.$i->nom_structure];
    });

$query = Indemnites::query();

$filters = [
    'structure' => 'club_id',
    'dt_min' =>'created_at',
    'dt_max' =>'created_at',
];


$dt_min = $request->input('dt_min');
$dt_min = Carbon::parse($dt_min)->format('Y-m-d');
$dt_max = $request->input('dt_max');
$dt_max = Carbon::parse($dt_max)->format('Y-m-d');


foreach ($filters as $key => $column) {


    $query->when($request->has($key), function ($query) use ($column,$key, $request , $dt_min , $dt_max)  {
        $query->where($column, $request->$key)
            ->orWhereBetween('rencontre.created_at' , [$dt_min , $dt_max]);

    });
}

$indemnites = $query->leftJoin('structures' , 'structures.id' , '=' ,  'indemnites.club_id')
    ->leftJoin('rencontre' , 'rencontre.id' , '=' , 'indemnites.rencontre_id')
    ->where('total' , '!=' , 0)
    ->selectRaw('sum(total) as total , structures.nom_structure as club , club_id')
    ->groupBy('club_id')->paginate(20);

dd($indemnites);

我做错了什么?希望有人能帮助我

问题似乎来自生成结果的最后一个查询

当我更改为$ indemnites = $ query-&gt; paginate(20);它正在工作,但我需要在原始中赚一笔钱...希望有人可以提供帮助

已解决(我在列名上犯了错误)

 public function index(Request $request){

        $structure = Structure::select('num_structure', 'nom_structure' , 'id')
            ->where('type_structure_id' , '4')
            ->get()
            ->mapWithKeys(function($i) {
                return [$i->id => $i->num_structure.' - '.$i->nom_structure];
            });

        $query = Indemnites::query();

        $filters = [
            'structure' => 'club_id',
            'dt_min' =>'dt_rencontre',
            'dt_max' =>'dt_rencontre',
        ];


        $dt_min = $request->input('dt_min');
        $dt_min = Carbon::parse($dt_min)->format('Y-m-d');
        $dt_max = $request->input('dt_max');
        $dt_max = Carbon::parse($dt_max)->format('Y-m-d');


        foreach ($filters as $key => $column) {


            $query->when($request->has($key), function ($query) use ($column,$key, $request , $dt_min , $dt_max)  {
                $query->where($column, $request->$key)
                    ->orWhereBetween('rencontre.dt_rencontre' , [$dt_min , $dt_max]);

            });
        }

        $indemnites = $query->leftJoin('structures' , 'structures.id' , '=' ,  'indemnites.club_id')
            ->leftJoin('rencontre' , 'rencontre.id' , '=' , 'indemnites.rencontre_id')
            ->where('total' , '!=' , 0)
            ->selectRaw('sum(total) as total , structures.nom_structure as club , club_id , rencontre.dt_rencontre')
            //->orWhereBetween('created_at' , [$dt_min , $dt_max])
            ->groupBy('club_id')->paginate(20);




        return view('indemnites/index' , compact('indemnites' , 'structure'));

    }

1 个答案:

答案 0 :(得分:1)

您的关系中的表格都有created_at

    foreach ($filters as $key => $column) {


        $query->when($request->has($key), function ($query) use ($column,$key, $request , $dt_min , $dt_max)  {
            $query->where($column, $request->$key)
                ->orWhereBetween('tableName.created_at' , [$dt_min , $dt_max]);

        });
    }

可以是这样的

 ->orWhereBetween('structures.created_at' , [$dt_min , $dt_max]);

希望这有帮助