laravel迭代刀片中雄辩模型的结果

时间:2017-02-05 06:58:04

标签: php laravel blade

我得到的结果就像是跟随一对多关系的模型

{
   "id":1,
   "name":"asdjash",
   "position":"jhkjh",
   "about":"hkjhkj",
   "message":"hkjhkj",
   "image":"demo.jpg",
   "status":"Active",
   "created_at":"2017-02-07 00:00:00",
   "updated_at":"2017-02-09 00:00:00",
   "personality_slider_images":[
      {
         "id":1,
         "image_name":"one.jpg",
         "personality_id":"1",
         "created_at":"2017-02-09 00:00:00",
         "updated_at":"2017-02-11 00:00:00"
      },
      {
         "id":2,
         "image_name":"two.jpg",
         "personality_id":"1",
         "created_at":"2017-02-10 00:00:00",
         "updated_at":"2017-02-26 00:00:00"
      }
   ]
}

我想在页面上显示所有细节

如下

 <div class="well">
       <p><h4>Name : </h4> 
       {{ $personality->name }}
       </p>
       <p><h4>Position : </h4> 
       {{ $personality->position }}
       </p>
       <p><h4>About : </h4> 
       {{ $personality->about }}
       </p>
       <p><h4>Image : </h4> 
       <img src="{{ URL::to('/contents/personality/' . $personality->image) }}" alt="{{ $personality->image }}" width="150" height="180"  style="display: block; ">                      
       </p>
       <p><h4>Message : </h4> 
       {{ $personality->message }}
       </p>
       <p><h4>SliderImage : </h4> 
       {{ $personality }}

       </p>
       <p><h4>Status : </h4> 
       {{ $personality->status }}
       </p>
 </div>

我试过这个

@foreach($personality->personality_slider_images as $p)
  {{ $p->image_name }}
@endforeach

但它给出了如下错误

  

为foreach()提供的参数无效

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

我看到你有json数组,

  1. 使用json_decode

    对其进行解码

    $decode = json_decode($encode);

  2. 现在遍历你的循环,

    @foreach($ personality-&gt; personality_slider_images as $ p)

    {{$ p-&gt; image_name}}

    @endforeach

  3. 它会起作用。

    试一试。

答案 1 :(得分:-2)

您应该为实际数组添加一个,而不是为该数组中的变量添加。

假设你的数组名称是$ personalities,它应该是这样的。

@foreach ($personalities as $personality)
Insert the HTML from your sample above in here. 
@endforeach

要检索图像对象,您必须执行以下操作

$personality->personality_slider_name['image_name]