I want to show uploaded pictures of users at my Blade, so I added this:
#foreach($users as $user)
#if($user->photo_id)
<td class="center">
<img src="{{ $user->photos->path }}" width="80">
</td>
#else
<td class="center">
</td>
#endif
#endforeach
But it returns this error:
Exception Property [path] does not exist on this collection instance.
I have already added these relationships to the Models as well:
Photo.php:
public function user()
{
return $this->belongsTo(User::class);
}
User.php:
public function photos()
{
return $this->hasMany(Photo::class);
}
So what is going wrong here ? How can I solve this issue ?
Also when I say: {{ dd($user->photos) }}, it retuns:
Illuminate\Database\Eloquent\Collection {#1231 ▼
#items: []
}
And here is also table photos:
In your User model, you defined one to many relationship. So laravel will return an array of collection.
If you path will not be in array, I suggest you to change the relation to one-to-one.
User.php
public function photos()
{
return $this->hasOne(Photo::class);
}
As far as I understand, you defined your one to many relationship contrariwise.
You're getting multiple photos for a user. So, it is normal to get a collection of photos instead of an instance of photo.
In the result, you don't have path property on photos collection, probably you can handle this error by getting first instance $user->photos->first()->path, but you need to have a clear definition for your model relationship.
Related
post.php
class post extends Model
{
use HasFactory;
public function publisher()
{
return $this->belongsTo('App\Models\User');
}
protected $fillable = ['title','image','price','Describtion','image1','image2','image3','image4','publisher'];
}
Fetchpost.php
class Fetchpost extends Component
{
use WithPagination;
public function render()
{
return view('livewire.fetchpost', ['posts' => post::orderBy('created_at', 'desc')->paginate(10)],);
}
}
fetchpost.blade.php
<div>
#foreach($posts as post)
<img class="rounded" src="{{ $post->publisher->profile_photo_path }}">
<h1>{{ $post->publisher }}</h1>
<h3>{{ $post->title }}</h1>
<p>{{ $post->Description }}</p>
#endforeach
</div>
so what i'm trying to achieve is to show post with publisher profile info like name and profile photo (like facebook), but i am getting error
Trying to get property 'profile_photo_path' of non-object
the problem here is, you are not passing a foreign key explicitly in relationship. so using naming convention for relationship definition, laravel is looking for a publisher_id (relationship method name _ primary key of the parent table) column in your post table. but there's none as your foreign key is publisher only and laravel can't a build a relationship. thus you are getting your error trying to get property of non object. it's a good practice to reference the foreign key in relationship. or make the foreign key using laravel convention.
public function publisher()
{
return $this->belongsTo('App\Models\User', 'publisher');
}
As mentioned in the title I get the error "Property [id] does not exist on this collection instance." Only when I run the code online here are my relevant codes.
1-EmployeeController (browser tells me that the error is here the second line)
public function show(Employee $employee)
{
$employee = Employee::find ($employee);
$edocument = EDocument::where ('employee_id',$employee->id)->first();
return view ('employee.show')->withEmployee($employee)->withEdocument($edocument);
}
2-show.blade.php
<div class="jumbotron">
<h1>{{$employee->name}} ({{$employee->position}})</h1>
#if (isset($edocument))
Go To Employee Database Page
#else
<p class="lead bg-danger">Employee documents are not uploaded</p>
#endif
Create Employee Contract
if anyone can explain to me this error in more details that would be great also. thanks
ps.. this is my first laravel project (;
You use route model binding in your controller method to get the Employee model. But you also run a find, which would fail since you're passing the model instead of the id. Do as one of the codes shown below and don't mix them.
Do this if you want to use route model binding.
public function show(Employee $employee)
{
$edocument = EDocument::where ('employee_id', $employee->id)->first();
return view ('employee.show')->with(compact('employee', 'edocument'));
}
Do this if you want to pass the employee id and fetch the model in controller.
public function show($employee)
{
$employee = Employee::find($employee);
$edocument = EDocument::where ('employee_id', $employee->id)->first();
return view ('employee.show')->with(compact('employee', 'edocument'));
}
Maybe this can help you. Why don't you pass the information in the controller using -
return view('employee.show', ['employee' => $employee, 'edocument'=>$edocument]);
It worked for me. (Do not have to change anything in the show.blade .php)
Hi i need fix returning view. I made relation and its returning array. How i can change this and i am not sure i made good function to relationship. I just trying lerning it i saw many tutorials and i know in Laravel is so kind magic tips to returning.
My function must showing events what user was joining. I think i make it but its returning array and when i try do something like that
#foreach($zapisevents as $zapisevent)
<table class="table">
<th>{{$zapisevent->eventsave->name}}</th>
</table>
#endforeach
i got error:
Property [name] does not exist on this collection instance. (View: /home/mariusz/Pulpit/www/szpital/resources/views/profil/profil.blade.php)
but when i use <th>{{$zapisevent->eventsave}}</th> its returning array.
There is function for joining to event
public function index()
{
$userid = Auth::user();
$zapisevents = User::with('eventsave')->where('id',(Auth::user()->id))->get();
return view('profil.profil', ['userid' => $userid], ['zapisevents' => $zapisevents]);
}
Model User:
public function eventsave()
{
return $this->belongsToMany(HomeModel::class,'save_events','users_id','events_id')->withTimestamps();
}
Model HomeModel <<<
public function usersave()
{
return $this->belongsToMany(User::class,'save_events','events_id','users_id');
}
Its returning:
[{"id":5,"name":"asdasdsa","title":"Wydzial 1","start":"2017-04-04
03:00:00","end":"2017-04-04 07:59:00","created_at":"2017-04-01
18:50:40","updated_at":"2017-04-01
18:50:40","pivot":{"users_id":3,"events_id":5,"created_at":"2017-04-01
18:50:58","updated_at":"2017-04-01
18:50:58"}},{"id":7,"name":"kkkkkkkkkkkkkkkkkkkkkkkk","title":"Wydzial
4","start":"2017-04-01 00:00:00","end":"2017-04-01
23:59:59","created_at":"2017-04-01 19:54:24","updated_at":"2017-04-01
19:54:24","pivot":{"users_id":3,"events_id":7,"created_at":"2017-04-01
19:55:41","updated_at":"2017-04-01 19:55:41"}}]
the
#foreach($zapisevents as $zapisevent)
<table class="table">
<th>{{$zapisevent->eventsave->name}}</th>
</table>
#endforeach
Should ne
#foreach($zapisevents as $zapisevent)
<table class="table">
#foreach($zapisevent->eventsave as $eventSave)
<th>{{$eventsave->name}}</th>
#endForeach
</table>
#endforeach
in you code the name property is being called in a collection of HomeModel but it needs to be called in a model itself
When using arrays, you need to access their properties via their index like this:
$zapisevent->eventsave['name']
as opposed to:
$zapisevent->eventsave->name
I'm using Laravel 5.2. In my application, if a user is an admin, he can see all groups. Otherwise, he can only see his groups.
Model
public function groups() {
if ($this->isAdmin()) {
return \App\Group::get();
}
return $this->belongsToMany('App\Group');
}
View
#foreach($user->groups as $group)
{{ $group->name }}
#endforeach
Result
The code above works if the user is not an admin, but I get this error if the user is an admin
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
I tried this instead: $user->groups() as $group, it works when the user is an admin, but show nothing when it's not an admin.
Question
I know when I call a relation as a property ($user->groups), it returns a collection of objects. Instead, if I call it as a function ($user->groups()), I get a QueryBuilder instance.
What can I do to use the same syntax as in my view ?
Note: I cannot add all groups in the database to the admin, as admins must have no group.
The way is not using directly relationship for this but using extra method to handle this.
First in your model create simple relationship:
public function groups()
{
return $this->belongsToMany('App\Group');
}
and then create extra method:
public function availableGroups()
{
if ($this->isAdmin()) {
return \App\Group::get();
}
return $this->groups;
}
Now in view you can use:
#foreach($user->availableGroups() as $group)
{{ $group->name }}
#endforeach
I have a problem with one to many relationship in Laravel. I cannot retrieve my data from database. My codes are following.
Doctor.php
public function cities()
{
return $this->belongsTo('App\City');
}
City.php
public function doctor()
{
return $this->hasMany('App\Doctor');
}
view
#foreach($doctors as $doctor)
<tr>
<td data-title="ID">{{ $doctor->id }}</td>
<td data-title="Name">{{ $doctor->name }}</td>
<td data-title="Hospital">{{ $doctor->hospital }}</td>
<td data-title="Designation">{{ $doctor->designation }}</td>
<td data-title="City">{{ $doctor->cities->name }}</td> <!-- problem in this line -->
</tr>
#endforeach
When I try to view the data an error has been shown:
ErrorException in 3d7f581c490d492093e6e73f8ebd29525504e56b.php line 46:
Trying to get property of non-object (View: D:\xampp\htdocs\tourisms\root\resources\views\doctors\index.blade.php)
On the belongsTo side of the relationship, when the foreign key name is not specified, it is built using the name of the relationship. In this case, since your relationship is named cities, Laravel will look for the field doctors.cities_id.
If your foreign key is not cities_id, then you need to either change the name of your relationship method, or specify the name of the foreign key:
public function cities() {
return $this->belongsTo('App\City', 'city_id');
}
// or, better:
public function city() {
return $this->belongsTo('App\City');
}
Also, as mentioned by #Christophvh, the naming of your relationships is a little off, but not programmatically incorrect. It makes more sense and is more readable when belongsTo/hasOne relationships are named singular, and belongsToMany/hasMany relationships are named plural.
You are trying to loop a Collection and not an array.
2 solutions:
1)
add ->get(); when you are calling your relationship in your controller
2)
use
#foreach($doctors->all() as $doctor)
instead of
#foreach($doctors as $doctor)
Also a quick tip:
Your naming is not really telling what you are doing. You should swap them around like below. 'A doctor belongs to a city' & 'A city has many doctors'
Doctor.php
public function city()
{
return $this->belongsTo('App\City');
}
City.php
public function doctors()
{
return $this->hasMany('App\Doctor');
}