第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Laravel Eloquent 的條件 wherehas

Laravel Eloquent 的條件 wherehas

PHP
尚方寶劍之說 2021-12-03 15:29:22
我area在兩個(gè)表中有一個(gè)相似的 ( ) 值one,two并且這兩個(gè)表與主表有關(guān)系master。一次,該master表將僅在一個(gè)關(guān)系中包含數(shù)據(jù),而另一個(gè)將是null。使用這種架構(gòu),我有一個(gè)搜索頁面,用戶可以在其中搜索與這些表相關(guān)的任何值,并且搜索字段放置AND有條件。在這里,如果用戶輸入一些值,area我需要檢查任何一個(gè)表 (one或two) 中存在的面積值,而不會(huì)破壞AND條件。嘗試了下面的代碼,但它違反了AND規(guī)則并考慮了OR. 有什么建議可以解決嗎?$result =  Master::where(function ($query) use ($request) {    if ($request->filter == true) {        $query->where('user_id', Auth::user()->id);    }    // other conditions here    if (!empty($request->area_from) && !empty($request->area_to)) {        $query->whereHas('one', function ($query) use ($request) {            $query->whereBetween('area', [$request->area_from, $request->area_to]);        });        $query->orWhereHas('two', function ($query) use ($request) {            $query->whereBetween('area', [$request->area_from, $request->area_to]);        });    }    // other conditions here})->with(['one', 'two'])->paginate($request->item);
查看完整描述

3 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

您將所有 where 語句括在括號(hào)中。我認(rèn)為您想要做的是將查詢的第一部分從 where 子句中拉出,以便您可以輕松地將該whereHas部分括在括號(hào)中。


// Initialise the model

$query = new Master;


// Start building the query

if ($request->filter == true) {

    $query->where('user_id', Auth::user()->id);

}


if (!empty($request->area_from) && !empty($request->area_to)) {

    // Wrap these in brackets so we don't interfare with the previous where

    $query->where(function($query2) use ($request) {

        $query2->whereHas('one', function ($query3) use ($request) {

            $query3->whereBetween('area', [$request->area_from, $request->area_to]);

        });

        $query2->orWhereHas('two', function ($query3) use ($request) {

            $query3->whereBetween('area', [$request->area_from, $request->area_to]);

        });

    }

}


$query->with(['one', 'two'])->paginate($request->item);


查看完整回答
反對(duì) 回復(fù) 2021-12-03
?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

您可以參考此鏈接創(chuàng)建合并關(guān)系


public function mergedOneAndTwo($value)

{

    // There two calls return collections

    // as defined in relations.

    $onedata= $this->one;

    $twodata= $this->two;


    // Merge collections and return single collection.

    return $onedata->merge($twodata);

}



并使用 whereHas('mergedOneAndTwo')


查看完整回答
反對(duì) 回復(fù) 2021-12-03
?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊

使用更近的位置并在內(nèi)部設(shè)置條件可能會(huì)正常工作


$master = Master::with('one')->with('two');

$result = $master->where(function($subQuery)

{   

    $subQuery->whereHas('one', function ( $query ) {

        $query->whereBetween('area', [$request->area_from, $request->area_to] ); //assuming  $request->area_from, $request->area_to is range of value

    })

    ->orWhereHas('two', function ( $query ) {

        $query->whereBetween('area', [$request->area_from, $request->area_to] );

    });

});


查看完整回答
反對(duì) 回復(fù) 2021-12-03
  • 3 回答
  • 0 關(guān)注
  • 339 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)