Laravel 刀片模板中的雄辩问题

Laravel 刀片模板中的雄辩问题,laravel,Laravel,我的控制器中有一个函数,如下所示 public function singleCategory($id){ if($id == 'all') { $category = Category::get(); } else { $category = Category::find($id); } return view('campaigns_by_category', compact('category')); } 在刀片模板中,我使

我的控制器中有一个函数,如下所示

 public function singleCategory($id){
    if($id == 'all') {
        $category = Category::get();
    } else {
        $category = Category::find($id);
    }
    return view('campaigns_by_category', compact('category'));
}
在刀片模板中,我使用以下代码

 @if(request()->segment(2) == 'all')
                @php
                    foreach($category as $cat) {
                        $campaigns = $cat->users()->orderBy('id', 'desc')->paginate(20);
                    }
                @endphp
            @else
                @php
                    $campaigns = $category->users()->orderBy('id', 'desc')->paginate(20); 
                @endphp
            @endif
            {{ count($campaigns) }}  // why I am getting 0 here ?

Laravel关系的工作方式如下: 在
类别
模式中定义类别和用户之间的关系

class Category extends Model
{
   function users() {
      return $this->hasMany('App\User');
   }
}
我希望你能理解。 如果不比读拉威尔雄辩的关系

因为当你做
->get()
->all()
$category
时,你会成为
类别的集合,而不是
类别

@if(request()->segment(2) == 'all')
   @php
        foreach($category as $cat) {
          $campaigns = $cat->users()->orderBy('id', 'desc')->paginate(20);
        }
    @endphp
@else
    @php
          $campaigns = $category->users()->orderBy('id', 'desc')->paginate(20); 
    @endphp
@endif 

@if($campaigns->count() > 0)
    // some code here
@endif

编辑


正如您所问,但为什么我在下面的结果中得到0? 下面的部分将覆盖
$campetings
,因为您处于foreach中

foreach($category as $cat) {
    $campaigns = $cat->users()->orderBy('id', 'desc')->paginate(20);
}
a可以给你一些不同的建议吗


 public function singleCategory($id){

    // Eager loading the users before will increase the performance, by performing one single query instead of repeting the query in a loop
    $query = Category::with(['users' => function($query) {
        $query->orderBy('id', 'desc')->paginate(20);
    }])


    if($id == 'all') {
        $categories = $query->get();
    } else {
        // findOrFail() will throw a 404 when the ID doesn't not exists
        // [$id] is an array to return a Collection of Category
        $categories = $query->findOrFail([$id]);
    }
    return view('campaigns_by_category', compact('categories'));
}

然后在你的刀片中,你不需要知道你是否有一个或多个类别

@foreach($categories as $category)

 {{-- "In my knowledge we can't alias with eager loading, but you can still make a relationshion campaigns instead of users" --}}
    {{ $category->users->count() }}

    {{-- "forelse is like a foreach but perform a `else` when users are empty" --}}
    @forelse($category->users as $campaign)
        {{ $campaign }}
    @empty
        No campaigns for {{ $category }}
    @endforelse

@foreach

您是否定义了用户和类别之间的关系?您能否在
类别
模式中定义用户关系?谢谢@Amit Senjaliya。我的模型中有以下代码
public function users(){return$this->hasMany(User::class)->wherective_status(1)->whereUser_type('User');}
。谢谢。为什么我在下面的结果中得到0<代码>@if(request()->segment(2)='all')@php-foreach($category as$cat){$campetings=$cat->users()->orderBy('id','desc')->paginate(20);}@endphp@else@php$campetings=$category->users()->orderBy('id','desc desc')->paginate(20)@endphp@endif{{count($campaiments)}
@abuab什么是
whereActive_status()
whereUser_type()
?这些是数据库字段。然后应该是这样的:
$this->hasMany(User::class)->where(“Active_status”,“1”)->where(“User_type”,“User”)谢谢@cbaconier。你的解决方案是有效的。但为什么我得到0作为下面的结果<代码>@if(request()->segment(2)='all')@php-foreach($cat类){$campetings=$cat->users()->orderBy('id','desc')->paginate(20);}@endphp@else@php$campetings=$category->users()->orderBy('id','desc')->paginate(20)@endphp@endif{{count($campaiments)}
我在下面的一行中遇到了错误<代码>$categories=$query::get();//[2019-09-23 12:25:24]laravel.ERROR:Non-static method illumb\Database\elount\Builder::get()不应静态调用;}@endphp@else@php$campetings=$category->users()->orderBy('id','desc')->paginate(20)@endphp@endif{{count($campaiments)}
我想从
foreach
循环中获取值。@abuabu我修复了
::
,这是一个我在现实生活中没有测试过的小错误,对此我深表歉意。关于你的竞选活动,我把它搬到了foreach里面。既然你有很多活动用户,那么你会算哪一个?