php - laravel 5.2 Displaying related data in the view -
in controller use eager loading load properties along related files , addresses:
public function search_results(request $request) { $properties = property::with('residential', 'file', 'addresses')->get(); return view('result', ['results' => $properties]); }
and pass straight onto view. in view.blade.php how access related data.
here example of view not seem work regards related data.
@forelse ($results $result) <div class="row"> <div class="col-md-7"> @foreach($result->file() $file) <a href="#"> <img class="img-responsive" alt="" src="{{ url::to('/') }}/images/properties/{{$file->filename}}"> </a> @endforeach </div> <div class="col-md-5"> <h3>{{ $result->title }}</h3> <h4>subheading</h4> <p>{{ $result->description }}</p> <a class="btn btn-primary" href="#">view property <span class="glyphicon glyphicon-chevron-right"></span> </a> </div> </div>
this property model:
class property extends model { protected $table = 'properties'; /** * attributes mass assignable. * * @var array */ protected $fillable = [ 'user_id', 'is_for_sale','available','title', 'description','price','date_available' ]; public function user() { return $this->belongsto('app\user'); } public function residential() { return $this->hasone('app\residential'); } /** * addresses belong properties. */ public function addresses() { return $this->belongstomany('app\address'); } public function file() { return $this->hasmany('app\file'); } }
the file model
class file extends model { protected $table = 'files'; /** * attributes mass assignable. * * @var array */ protected $fillable = [ 'property_id', 'file_type_id','filename','system_filename', 'is-deleted' ]; public function property() { return $this->belongsto('app\property'); } public function file_type() { return $this->belongsto('app\file_types'); } }
what correct way handle type of related data in view?
for
@foreach($result->file() $file)
change
@foreach($result->file $file)
.
the $result->file()
loading same relations search_results()
method in controller.
Comments
Post a Comment