Property [title] does not exist on this collection instance
When you're using get()
you get a collection. In this case you need to iterate over it to get properties:
@foreach ($collection as $object)
{{ $object->title }}
@endforeach
Or you could just get one of objects by it's index:
{{ $collection[0]->title }}
Or get first object from collection:
{{ $collection->first() }}
When you're using find()
or first()
you get an object, so you can get properties with simple:
{{ $object->title }}
Property [image] does not exist on this collection instance. / Laravel - with() Method
the problem is that the variable $employee (which contains model Post btw)
has relation 1:N to People I assume
meaning you have to loop the people collection returned by the relation
e.g.
@foreach($employees as $employee)
@foreach($employee->people as $person)
{{ $person->image }}
@endforeach
@endforeach
Property [id] does not exist on this collection instance?
Try this solution:
You have a error in this query
$products = Product::where('user_id', $user_id)->get();
because $user_id is an object of the User, not a single value.
Your Code:
public function show($id)
{
//
$user_id = User::find($id);
if (is_null($user_id)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user_id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
New Code:
public function show($id)
{
//
$user = User::find($id);
if (is_null($user)) {
return response()->json([
'message' => 'User Not Found',
'status' => 404
], 404);
}
$products = Product::where('user_id', $user->id)->get();
return response()->json([
'data' => new UserProductResource($products),
'status' => 200
], 200);
}
Laravel: Property [name] does not exist on this collection instance while trying to populate field in a Edit view
Thank you everyone for your time
All of your answers where very helpful, but it turns out i just made a typo.
In the edit routeRoute::get('/product/{article}/edit', [ProductController::class, 'edit']);
I wrote {article}
instead of {product}
(Both the model and controller where named Product) as stated by @ByWaleed. After making that correction everything works correctly.
As a recomendation for future users, if posible use Route::resource()
instead of writing each route individually it makes it less likely to make a mistake as te one i did.
Again thank you everyone.
Property [name] does not exist on this collection instance
Because the areas
is a collection so you should loop over it like this :
@foreach($users as $u)
<tr role="row" class="odd">
<td class="sorting_1">{{$u->name}}</td>
<td></td>
<td>
@foreach($u->areas as $area)
{{$area->name}},
@endforeach
</td>
<td>{{route('show_user', ['id' => $u->id])}}</td>
</tr></tbody>
@endforeach
And if you want to separate them whith comma for example you can do it like that without looping :
@foreach($users as $u)
<tr role="row" class="odd">
<td class="sorting_1">{{$u->name}}</td>
<td></td>
<td>
{{ $u->areas->pluck('name')->implode(', ') }}
</td>
<td>{{route('show_user', ['id' => $u->id])}}</td>
</tr></tbody>
@endforeach
Property [users] does not exist on this collection instance
You had missed the exact foreign key between your users table and usertypes table.
First, you defined the that the foreign key of your user table is 'ut_id' base of what you had in your belongsTo relationship. On this one
/**
* Get the user type that user has.
*/
public function users(){
return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
Second is that, in your user type model, you used a foreign key to user table named 'user_type_id', which is at first you named it as 'ut_id' in your users table. On this one
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'ut_id';
/**
* Get the user associated with the user type.
*/
public function users(){
return $this->hasOne('App\Models\Users', 'user_type_id', $primaryKey);
}
You have to match this foreign keys you used to solve your problem.
Now, to fetch your all user with their types, your query should look like this.
$users = Users::with('users')->get();
assuming that your user table has this relationship
public function users(){
return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
and your user types model has this relationshio
public function users(){
return $this->hasOne('App\Models\Users', 'ut_id', $this->primaryKey);
}
in laravel Property [password] does not exist on this collection instance
using get()
returns a collection and you need to loop through it to get a property. use first()
that will return an object to which the property exist.
$admin = User::with('usermetas')->where('username',$username)->
orWhere('email',$username)->where('admin','0')->first();
Property [group] does not exist on this collection instance
$product->groupproducts
is a collection(array) of products.
You can do:
dd($product->groupproducts->first()->group->name);
This will get first element from collection which is instance of Group
class.
Related Topics
Tell Composer to Use Different PHP Version
How to Find Out If CSV File Fields Are Tab Delimited or Comma Delimited
Laravel Print Array in Blade PHP
Dompdf Remote Image Is Not Displaying in Pdf
What Is the Regular Expression to Validate a Comma Delimited List But Ending With '&' and a Word
How to Get Max and Min Value in a Multidimensional Array
Php Get Number of Week for Month
Laravel, Sync() - How to Sync an Array and Also Pass Additional Pivot Fields
How to Get Username from Facebook Id
How to Remove Back Slashes from Json Output in PHP
What Is the Easiest Way to Check If Time Is Set in Datetime Variable
Add Space After Every 4Th Character
Stop Inserting Data in the Database When Refreshing the Page
Clear the Form Field After Successful Submission of PHP Form