尝试嵌套 foreach 循环以删除项目并在 laravel 控制器中创建新数组
trying to nest foreach loop to remove items and create new array in laravel controller
所以,我已经彻底弄糊涂了,非常感谢任何帮助。
我想做的是过滤掉受用户所在国家/地区限制的项目(在我的控制器中完成)。
我的第一个查询获取所有项目(在本例中为海报)。我的第二个项目得到了限制海报。此代码按计划工作。
然后,我尝试创建一个嵌套循环,通过创建一个新数组并将传递我的 if 子句的项目推入我的 $unrestrictedPosters 数组来从所有海报中过滤出受限制的海报。我的 if 子句是我的代码失败的地方,因为我无法正确访问键值来实现我的 if 子句。
顺便说一句,一旦我 return $unrestrictedPosters 将在我的 bade 文件中使用和循环。
控制器代码如下
class PosterRestrictionController extends Controller
{
public function index()
{
$profile = app('App\Http\Controllers\DevelopmentController')->getActiveUser(Auth::id());
$userCountry = $profile->country;
$unrestrictedPosters = [];
$allPosters = DB::table('posters')
->join('profiles', 'posters.user_id', '=', 'profiles.user_id')
->join('users', 'posters.user_id', '=', 'users.id')
->select('users.name',
'profiles.surname',
'profiles.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('poster_restrictions.restricted_country_id', '=', $userCountry)
->select(
'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;
}
}
当您可以在查询中限制它们时,为什么还要麻烦循环它
class PosterRestrictionController extends Controller
{
public function index()
{
$profile = app('App\Http\Controllers\DevelopmentController')->getActiveUser(Auth::id());
$userCountry = $profile->country;
$unrestrictedPosters = DB::table('posters')
->join('profiles', 'posters.user_id', '=', 'profiles.user_id')
->join('users', 'posters.user_id', '=', 'users.id')
->leftJoin('poster_restrictions', function($join) use($userCountry) {
$join->on('poster_restrictions.poster_id', '=', 'posters.id')
->where('poster_restrictions.restricted_country_id', '=', $userCountry);
})
->whereNull('poster_restrictions.restricted_country_id')
->select('users.name',
'profiles.surname',
'profiles.country',
'posters.id as poster_id',
'posters.user_id',
'posters.title',
'posters.category',
)
->orderBy('posters.id', 'asc')
->get();
dd($unrestrictedPosters);
//return $unrestrictedPosters;
}
}
所以,我已经彻底弄糊涂了,非常感谢任何帮助。 我想做的是过滤掉受用户所在国家/地区限制的项目(在我的控制器中完成)。
我的第一个查询获取所有项目(在本例中为海报)。我的第二个项目得到了限制海报。此代码按计划工作。
然后,我尝试创建一个嵌套循环,通过创建一个新数组并将传递我的 if 子句的项目推入我的 $unrestrictedPosters 数组来从所有海报中过滤出受限制的海报。我的 if 子句是我的代码失败的地方,因为我无法正确访问键值来实现我的 if 子句。
顺便说一句,一旦我 return $unrestrictedPosters 将在我的 bade 文件中使用和循环。
控制器代码如下
class PosterRestrictionController extends Controller
{
public function index()
{
$profile = app('App\Http\Controllers\DevelopmentController')->getActiveUser(Auth::id());
$userCountry = $profile->country;
$unrestrictedPosters = [];
$allPosters = DB::table('posters')
->join('profiles', 'posters.user_id', '=', 'profiles.user_id')
->join('users', 'posters.user_id', '=', 'users.id')
->select('users.name',
'profiles.surname',
'profiles.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('poster_restrictions.restricted_country_id', '=', $userCountry)
->select(
'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;
}
}
当您可以在查询中限制它们时,为什么还要麻烦循环它
class PosterRestrictionController extends Controller
{
public function index()
{
$profile = app('App\Http\Controllers\DevelopmentController')->getActiveUser(Auth::id());
$userCountry = $profile->country;
$unrestrictedPosters = DB::table('posters')
->join('profiles', 'posters.user_id', '=', 'profiles.user_id')
->join('users', 'posters.user_id', '=', 'users.id')
->leftJoin('poster_restrictions', function($join) use($userCountry) {
$join->on('poster_restrictions.poster_id', '=', 'posters.id')
->where('poster_restrictions.restricted_country_id', '=', $userCountry);
})
->whereNull('poster_restrictions.restricted_country_id')
->select('users.name',
'profiles.surname',
'profiles.country',
'posters.id as poster_id',
'posters.user_id',
'posters.title',
'posters.category',
)
->orderBy('posters.id', 'asc')
->get();
dd($unrestrictedPosters);
//return $unrestrictedPosters;
}
}