Laravel 获取并分页相同的数据
Laravel get and paginate same data
我使用 Laravel 作为控制器,使用 blade 文件作为网页。我的代码是这样的:
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' => count($search->where('properties.category', $category->id)->get()),
有了这个它给了我
正在尝试获取 属性 个 non-object
<span class="lat"><?php echo e($property->title); ?></span>,
如果关系是在模型中建立的,你应该只以这种方式使用 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'));
我认为您想将数据传递给 blade 查看并获取每个类别的分类数据计数...为此,您可以使用重复函数分别对数据进行计数。例如:
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->count($category->id);
这是成功的行。
我使用 Laravel 作为控制器,使用 blade 文件作为网页。我的代码是这样的:
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' => count($search->where('properties.category', $category->id)->get()),
有了这个它给了我
正在尝试获取 属性 个 non-object
<span class="lat"><?php echo e($property->title); ?></span>,
如果关系是在模型中建立的,你应该只以这种方式使用 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'));
我认为您想将数据传递给 blade 查看并获取每个类别的分类数据计数...为此,您可以使用重复函数分别对数据进行计数。例如:
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->count($category->id);
这是成功的行。