Biz Dev B
Biz Dev B

Reputation: 383

Laravel Getting element from a parameter issue

Here is my Code to get the AuthorData from the AuthorModel

$AuthorData = AuthorModel::where('BlogAuthor', '=', '1')->get();
echo $AuthorData;

While i echo the $AuthorData

I am getting

[{"AutoId":1,"BlogAuthor":1,"AuthorName":"Raj Kumar","AuthorDesc":null,"CreatedAt":null,"CreatedBy":null,"UpdatedAt":null,"UpdatedBy":null,"IsDeletable":null,"Status":1}]

But while i try to get only the AuthorName like echo $AuthorData->BlogAuthor; below i am getting Undefined property: error.

What is the mistake i am doing ?

Upvotes: 1

Views: 37

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152880

get() returns a collection. If you closely look at the output of the whole object you can see that it is being converted to an array [...].

You probably only want the first result since only one should match

$AuthorData = AuthorModel::where('BlogAuthor', '=', '1')->first();
echo $AuthorData->AuthorName;

Or if you want all results you need to loop

$AuthorData = AuthorModel::where('BlogAuthor', '=', '1')->get();
foreach($AuthorData as $Author){
    echo $Author->AuthorName;
}

Upvotes: 1

Related Questions