Laravel 多條件查詢

chunsezhilv發表於2020-04-22
<?php
public function localSearch($city, $name){
    $sou = ['wmember.headimgurl', 'activity.id', 'activity.tr_id', 'activity.wid',
    'activity.title', 'activity.img', 'activity.price',
    'activity.start', 'activity.forming', 'activity.day_number'];
//按型別搜尋
$homeSearch = Activity::where('tr_id', 1)->where('end', '>=', date("Y-m-d"))
    ->where('activity.city', $city)
    ->Join('wmember', 'activity.wid', '=', 'wmember.wid')
    ->where('activity.type', 'like', '%' . $name . '%')
    ->select($sou)->get();
if ($homeSearch->isEmpty())
    //按標題搜尋
    $homeSearch = Activity::where('tr_id', 1)->where('end', '>=', date("Y-m-d"))
        ->where('activity.city', $city)
        ->Join('wmember', 'activity.wid', '=', 'wmember.wid')
        ->where('activity.title', 'like', '%' . $name . '%')
        ->select($sou)->get();
if ($homeSearch->isEmpty())
    //如果以上條件都不滿足就呼叫首頁進入的搜尋
    $this->homeSearch($city, $name);

foreach ($homeSearch as $k => $v) {
    $v->img = asset($v->img);
    //判斷該活動的報名人數是否成型
    $ord_people = DB::table('order')->where('sub_id', $v->id)->where('status', 2)->sum('people_num');
    if ($ord_people >= $v->forming) {
        $v->become = "已成行";
    } else {
        $v->become = "報名中";
    }
}
if ($homeSearch->first()) {
    return ['status' => '0', 'msg' => "載入成功!", 'search' => $homeSearch];
} else {
    return ['status' => '1', 'msg' => "暫時沒有資料!"];
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章