【问题标题】:Laravel get and paginate same dataLaravel 获取和分页相同的数据
【发布时间】:2018-06-04 18:48:30
【问题描述】:

我将 Laravel 用于控制器和刀片文件用于网页。我的代码是这样的:

PropertiesController

$properties = Property::where('status', 1);
$properties = $properties->orderBy('properties.created_at', 'DESC')->paginate(8);
return view('properties.index')->with('properties', $properties);

index.blade.php

@foreach ($properties as $property)
<div class="geo">
  <span class="lat">{{ $property->title }}</span>,
  <span class="lng">{{ $property->description }}</span>
</div>

我想要实现的是获得 w.r.t 的类别。与属性一起计数,为此,我正在做

$properties = Property::where('status', 1);

$categories = array();
if (is_null($req->c)) {
    $search = $properties;
    foreach (Category::all() as $category) {
     array_push(
       $categories,
          array(
            'id' => $category->id,
            'name' => $category->category,
            'counts' => count($search->where('properties.category', $category->id)->get()),
          )
       );
    }
}

$properties = $properties->orderBy('properties.created_at', 'DESC')->paginate(8);

return view('properties.index')->with('properties', $properties)->with('categories', $categories);

$search = $properties;
'counts' =&gt; count($search-&gt;where('properties.category', $category-&gt;id)-&gt;get()),

有了这个它给了我

试图获取非对象的属性
&lt;span class="lat"&gt;&lt;?php echo e($property-&gt;title); ?&gt;&lt;/span&gt;,

【问题讨论】:

  • 你在哪里使用 for() ?我可以看看吗?

标签: php laravel pagination eloquent blade


【解决方案1】:

如果在模型中建立关系,则只能以这种方式使用 with ()。 控制器应该是这样的。

$properties = Property::where('status', 1)->with('category')->orderBy('properties.created_at', 'DESC')->paginate(8);
return view('properties.index', compact('propierties'));

这将为您提供指定类别旁边的属性列表。

但是,如果您需要列出类别并在每个类别中具有属性,则必须这样做。

$categories = Category::with('properties')->paginate(8);
return view('properties.index', compact('categories'));

【讨论】:

    【解决方案2】:

    我认为您希望将数据传递到刀片视图并获取每个类别的分类数据计数...为此,您可以使用重复的函数分别计算数据。例如:

    public function properties() {
        $properties = Property::where('status', 1);
    
        $categories = array();
        foreach (Category::all() as $category) {
            $count = $this->count($category->id);
            array_push(
                $categories,
                array(
                    'id' => $category->id,
                    'name' => $category->category,
                    'counts' => $count,
                )
            );
        }
    
        $properties = $properties->orderBy('properties.created_at', 'DESC')->paginate(8);
    
        return view('properties.index')->with('properties', $properties)->with('categories', $categories);
    }
    
    
    public function count($id) {
        $count = count(Property::where('category_id', $id)); // or any variable you are using to connect categories table with
        return $count;
    }
    

    $count = $this-&gt;count($category-&gt;id);

    这就是成功的那一行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 2021-03-09
      • 2021-02-09
      • 2021-11-24
      • 2018-07-31
      • 2021-09-02
      • 1970-01-01
      相关资源
      最近更新 更多