首页 > 解决方案 > 如何从 laravel 中的对象获取数据

问题描述

View Code:

 <div class="content">
        <div class="title m-b-md">
            Pizza List - {{$id}}
        </div> 
        {{$p->type}}  // This is not working.Here I only want to print type of pizza from the 
                         db.ERROR 
                            =Property [type] does not exist on this collection instance.
        {{$p}}  // But This Does shows Data in JSON.
    </div>


Controller Code:

public function show($id){

        $p=Pizza::where('p_name','prashant')->get();

        return view('details',['id'=>$id,'p'=>$p]);
    }

在这里,我想通过在 db 中具有列名称 p_name 的人来获取披萨订单的类型,所以我给出了一些静态名称。我只是一个初学者。谁能告诉我怎么做才能只打印披萨类型

标签: laravel

解决方案


如果你得到一个比萨值,那么使用这个

$p=Pizza::where('p_name','prashant')->first();


<div class="content">
    <div class="title m-b-md">
        Pizza List - {{$id}}
    </div> 
    {{$p->type}} 
</div>

如果你想要多个披萨值,那么使用这个

$p=Pizza::where('p_name','prashant')->get();

<div class="content">
    <div class="title m-b-md">
        Pizza List - {{$id}}
    </div> 
     @foreach($p as $pizza)
         {{$pizza->type}} 
     @endforeach
</div>

推荐阅读