mercredi 21 octobre 2015

Laravel 5 Blade template does not return object

With the concept of ->hasMany(App\RoomType) and ->belongsTo(App\Location) I have construct the necessary relation without problem (I love laravel).

Part of Location model:
...
  ->integer('id')->unsigned();
  ->string('name');
...
  RoomType(){return $this->hasMany('App\RoomType');)


Part of RoomType model:
...
  ->integer('location_id')->unsigned()->index();
  ->json('images')->unllable();  //  <--- older mysql will threat text, but not an issue
...
   protected $casts = ['images' => 'array'];
   Location(){return $this->belongsToMany('App\Location')};

After seeded db, database contains:

In Location table

ID  NAME
1   Loc001
2   Loc002
3   Loc003

In RoomType table

LOCATION_ID  IMAGES
1            'image1.jpg'
2            '{"0":"room-2.jpg","1":"room-3.jpg"}'
3            null

In the controller

. . . (simplified with relevant relationship for easy reading )
$Loc = App\Location::find(2);
$Info['location'] = $Loc;
$Info['roomType'] = $Loc->RoomType();  

return view('index', compact('Info'));

So far so good!

However at the Blade template.

I fail at the inner loop

@foreeach ($Info['location'] as $index => $loc)  //this is an object, no issue
    {!! $loc !!}
    @foreeach ($Info['roomType'] as $rt)
        ???
        $rt->images  /// this return exact string instead of object ??  
    @endforeach
@endforeach

When I dd($Info['roomType']->images), this return string instead of object.

I have no issue on the outer loop (location).

I do try pass from the view, with json_decode() or toArray(), I still get string at Blade side.

The casts is effective. Marvelous Laravel, but at controller side.

Of cause there are little logic to check if single or array of images.

How could I do foreach loop for RoomType images (if it's an array)

Appreciate your advice.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire