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);
}
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);
}
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 [posts] does not exist on this collection instance laravel
You first have to get your user:$user = Auth::user();
The users friends (better their ids) can be retrieved like this:
$friend_ids = $user->friends()->pluck('id')
Afterwards you will need to add the id of the current user to the id array:
$all_ids = array_push($friend_ids, $user->id)
Lastly you can retrieve all posts:
$posts = Posts::whereIn('user_id', $all_ids)->get();
Related Topics
What Is the Size Limit of a Post Request
How to Check If a Url Exists Via PHP
Finding Cartesian Product With PHP Associative Arrays
What in Layman'S Terms Is a Recursive Function Using PHP
Getting the Screen Resolution Using PHP
MySQL Connection Not Working: 2002 No Such File or Directory
How to Debug in Woocommerce 3+
Truncate a String to First N Characters of a String and Add Three Dots If Any Characters Are Removed
How to Change From-address When Using Gmail Smtp Server
Suppress Error With @ Operator in PHP
Remote File Size Without Downloading File
Is There a Code Obfuscator For PHP
How to Use Store and Use Session Variables Across Pages
Best Way to Insert Many Values in MySQLi
Why Can't I Access Datetime-≫Date in PHP'S Datetime Class
Send JavaScript Variable to PHP Variable