简体   繁体   English

如何从 php 中的数组中获取名称属性值?

[英]How can I get the name property value From the array in php?

This is an array of data from the database.这是来自数据库的一组数据。 I want to show the name property from the array and This is also a laravel livewire project how can I do this?我想显示数组中的 name 属性,这也是一个 laravel livewire 项目我该怎么做? can I use it foreach loop?我可以使用它 foreach 循环吗?

* This is the Array *这是数组

Illuminate\Database\Eloquent\Collection {#1431 ▼
 #items: array:5 [▼
   0 => App\Models\Reply {#1430 ▶}
   1 => App\Models\Reply {#1429 ▼
     #filleble: array:6 [▶]
     #connection: "mysql"
     #table: "replies"
     #primaryKey: "id"
     #keyType: "int"
     +incrementing: true
     #with: []
     #withCount: []
     #perPage: 15
     +exists: true
     +wasRecentlyCreated: false
     #attributes: array:9 [▶]
     #original: array:9 [▼
       "id" => 4
       "comments_id" => 1
       "name" => "Md. Munirujjaman"
       "email" => "mdmunirujjaman045@gmail.com"
       "reply" => "dkjfdkj"
       "notification" => 0
       "status" => 0
       "created_at" => "2021-01-12 03:01:47"
       "updated_at" => "2021-01-12 03:01:47"
     ]
     #changes: []
     #casts: []
     #classCastCache: []
     #dates: []
     #dateFormat: null
     #appends: []
     #dispatchesEvents: []
     #observables: []
     #relations: []
     #touches: []
     +timestamps: true
     #hidden: []
     #visible: []
     #fillable: []
     #guarded: array:1 [▶]
   }
 ]
} 

**This is the Code of my project ** **这是我项目的代码**

   public $reply;

    public function getModalId($item)
    {
        $this->reply     = Reply::where('comments_id', $item)->orderby('id', 'desc')->get();
        dd($this->reply);

    }

I am trying to get this of my laravel project and it give me the error message我正在尝试获取我的 laravel 项目的这个,它给了我错误消息

ErrorException Invalid argument supplied for foreach() (View: ErrorException 为 foreach() 提供的参数无效(查看:

 @foreach($reply as $cmtReply)
   {{$cmtReply->name}}
  @endforeach


This is database data how can I use this {{$reply}}这是数据库数据我该如何使用这个{{$reply}} 在此处输入图像描述

return View:make('viewname')->with('reply', $this->reply);

@foreach ($reply as $CommentReply)
  Hello, {{$CommentReply->name}}
@endforeach

You can access each property of any model by using the arrow -> followed by the name of the property.您可以使用箭头 -> 后跟属性名称来访问任何 model 的每个属性。 In your case, you want to access the "name" property, so you just type $myComment->name .在您的情况下,您想访问“名称”属性,因此您只需键入$myComment->name See: https://laravel.com/docs/8.x/eloquent#retrieving-models请参阅: https://laravel.com/docs/8.x/eloquent#retrieving-models

If you are using Laravels blade engine, you can use the same syntax within the {{... }} in your view.如果你使用 Laravel 刀片引擎,你可以在视图的{{... }}中使用相同的语法。

To loop through a list of entities, you have to pass the variable which contains this list in your controller, when accessing the view.要遍历实体列表,您必须在访问视图时在 controller 中传递包含此列表的变量。 See: https://laravel.com/docs/8.x/views#passing-data-to-views请参阅: https://laravel.com/docs/8.x/views#passing-data-to-views

After that, you can access the variable with the name you gave to it in the passed array.之后,您可以使用您在传递的数组中为其指定的名称访问该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM