【问题标题】:Why Laravel query returns empty array?为什么 Laravel 查询返回空数组?
【发布时间】:2021-09-02 22:34:09
【问题描述】:

我正在使用 Laravel 查询生成器从 db 中检索结果,但它向我显示空数组,但每当我使用原始查询时,它都会向我显示结果。 有什么解决办法吗?

RAW 查询(显示结果)

$work_query = 'SELECT * FROM work_availability WHERE EmployeeID = ' . $id . ' AND Position LIKE "%' . $r->position . '%" AND Type = "' . $r->first_time_type . '"';

Laravel 查询构建器(返回空数组)

$work_first_time = DB::table('work_availability')
        ->where('EmployeeID', $r->id)
        ->where('Position', 'LIKE', " % $r->position % ")
        ->where('Type', '"'.$r->first_time_type.'"')
        ->get()->toArray();

【问题讨论】:

    标签: mysql sql laravel


    【解决方案1】:

    试试这个

    1.错误在->where('Type', '"'.$r->first_time_type.'"')。 laravel 中不需要引号 2.->where('Position', 'LIKE', " % $r->position % ")出错

    这里有两种使用方式

    ->where('Position', 'like', "%{$r->position }%")
    

    ->where('Position', 'like', "%".$r->position."%")
    

    所以最终代码将是

    $work_first_time = DB::table('work_availability')
            ->where('EmployeeID', $r->id)
            ->where('Position', 'like', " %{$r->position}% ")
            ->where('Type', $r->first_time_type)
            ->get()->toArray();
    

    【讨论】:

    • 仍然给出空数组
    • 你能显示你在 dd($r->all()); 中得到了什么吗?并尝试使用较小的。
    • [ "id" => "1047" "position" => "Care Assistant, Live In" "first_time_days" => "Friday" "second_time_days" => "Saturday" "daysit_days" => null "sleepin_days" => null "first_time_type" => "06:45-15:00" "second_time_type" => "15:00-21:45" "other_type" => "Other" "daysit_type" => "DaySit " "sleepin_type" => "SleepIn" "week_type" => "Weeks" "waking_type" => "WakingNight" "other_data" => "nonef" "week_data" => "10" "AnnualLeave" => "Yes" "年度休假日期" => "19-10-2020,21-10-2020,23-10-2020" ]
    • dd($r->all()); 的结果那个没问题
    • 试试这个 ->where('Position', 'like', "%".$r->position."%")
    猜你喜欢
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 2019-09-17
    • 2016-03-21
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多