2017-12-22 19 views
1

WebページのコントローラとブレードファイルにはLaravelを使用しています。私のコードは次のようなものです:私が達成したいものをLaravel同じデータを取得してページを設定します

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

index.blade.php

PropertiesController

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

カテゴリを取得することです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')); 
関連する問題