2017-12-22 22 views
1

웹 페이지의 컨트롤러 및 블레이드 파일에 Laravel을 사용하고 있습니다. 뭔가처럼 내 코드는 다음과 같습니다Laravel 동일한 데이터를 가져오고 페이지 매기기

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

내가 달성하고자하는 카테고리를 얻는 것입니다

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에서 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()),

를하고있는 중이 야가 아닌 객체의 속성을 얻으려고 노력 error

나에게주는
<span class="lat"><?php echo e($property->title); ?></span>,

+0

어디에서 사용 했습니까()? 내가 볼 수 있니? –

답변

0

관계가 m 이런 경우에만()와 함께 사용해야합니다. 이것은 컨트롤러가 있어야하는 방법입니다.

$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'));