所以,我已經把自己徹底搞糊涂了,如果有任何幫助,我將不勝感激。 我想做的是過濾掉那些受用戶國家限制的專案(在我的控制器中完成)。
我的第一個查詢得到所有專案(本例中為海報)。我的第二個專案獲得受限制的海報。 這段代碼按計劃運行。
然后我試圖創建一個嵌套回圈,通過創建一個新的陣列,并將通過if子句的專案推送到$unrestrictedPosters陣列中,從而從所有海報中過濾出受限制的海報。我的if子句是我的代碼失敗的地方,因為我無法正確地訪問鍵值,以實作我的if子句。
另外,一旦我回傳$unrestrictedPosters,就會在我的bade檔案中使用和回圈。
控制器代碼如下
class PosterRestrictionController extends Controller
{
public function index()
{
$profile = app('AppHttpControllersDevelopmentController')->getActiveUser(Auth::id() ) 。
$userCountry = $profile->國家。
$unrestrictedPosters = [];
$allPosters = DB::table('posters')
->join('profile', 'posters.user_id', '=', 'profile.user_id')
->加入('users', 'posters.user_id', '=', ' users.id')
->選擇('users.name',
'profile.surname',
'profile.country'。
'posters.id as poster_id'。
'posters.user_id'。
'posters.title'。
'posters.category'。
)
->orderBy('posters.id', 'asc')
->get()。
$restrictedPosters = DB::table('posters')
->join('poster_restrictions', 'poster_restrictions.poster_id', '=', 'posters.id')
->where('posters_restrictions.restricted_country_id', '=', $userCountry)
->選擇(
'posters.id as poster_id'。
'poster_restrictions.poster_viewable',
'poster_restrictions.restricted_country_id')
)
->orderBy('posters.id', 'asc')
->get()。
foreach ($allPosters as $poster) {
foreach ($restrictedPosters as $restrictedPoster) {
if ($restrictedPoster['poster_viewable'] != 0) {
array_push($unrestrictedPosters, $poster) 。
}
}
}
dd($unrestrictedPosters)。
//return $unrestrictedPosters;。
}
}
uj5u.com熱心網友回復:
當你可以在查詢中限制它們時,為什么還要麻煩地回圈呢
class PosterRestrictionController extends Controller
{
public function index()
{
$profile = app('AppHttpControllersDevelopmentController')->getActiveUser(Auth::id() ) 。
$userCountry = $profile->國家。
$unrestrictedPosters = DB::table('posters'/span>)
->join('profile', 'posters.user_id', '=', 'profile.user_id')
->join('users', 'posters.user_id', '=', ' users.id')
-> leftJoin('poster_restrictions', function($join) use($userCountry) {
$join->on('poster_restrictions.poster_id', '='/span>, 'posters.id')
->where('poster_restrictions.restricted_country_id', '=', $userCountry) 。
})
->whereNull('poster_restrictions.restricted_country_id')
->選擇('users.name',
'profile.surname',
'profile.country'。
'posters.id as poster_id'。
'posters.user_id'。
'posters.title'。
'posters.category'。
)
->orderBy('posters.id', 'asc')
->get();
dd($unrestrictedPosters)。
//return $unrestrictedPosters;。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321603.html
標籤:
上一篇:不能做一個自定義的簽名驗證器類[Laravel8SpatieWebhookClient]。
下一篇:決議異質資料陣列的正確方法
