簡體   English   中英

如何從 php 中的數組中獲取名稱屬性值?

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

這是來自數據庫的一組數據。 我想顯示數組中的 name 屬性,這也是一個 laravel livewire 項目我該怎么做? 我可以使用它 foreach 循環嗎?

*這是數組

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 [▶]
   }
 ]
} 

**這是我項目的代碼**

   public $reply;

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

    }

我正在嘗試獲取我的 laravel 項目的這個,它給了我錯誤消息

ErrorException 為 foreach() 提供的參數無效(查看:

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


這是數據庫數據我該如何使用這個{{$reply}} 在此處輸入圖像描述

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

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

您可以使用箭頭 -> 后跟屬性名稱來訪問任何 model 的每個屬性。 在您的情況下,您想訪問“名稱”屬性,因此您只需鍵入$myComment->name 請參閱: https://laravel.com/docs/8.x/eloquent#retrieving-models

如果你使用 Laravel 刀片引擎,你可以在視圖的{{... }}中使用相同的語法。

要遍歷實體列表,您必須在訪問視圖時在 controller 中傳遞包含此列表的變量。 請參閱: https://laravel.com/docs/8.x/views#passing-data-to-views

之后,您可以使用您在傳遞的數組中為其指定的名稱訪問該變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM