I'm getting an error undefined variable when I'm trying to get username ($user->name) from User model using id which is foreign key ($feedback->user_id) of Feedback model.
#php
use App\Feedback;
use App\User;
$feedbacks = Feedback::all();
#endphp
<!DOCTYPE html>
<html>
<body>
ADMIN DASHBOARD | LOGOUT<br><br>
<h3>Feedbacks</h3>
<table border="1">
<tr><th>ID</th><th>Left By</th><th>Feedback</th></tr>
#foreach ($feedbacks as $feedback)
<tr>
<td>{{ $feedback->id}}</td>
<td>{{ $user->name }}</td>
<td>{{ $feedback->feedback }}</td>
</tr>
$uid=$feedback->user_id;
$user= User::find($uid);
#endforeach
</table>
</body>
</html>
I would suggest creating a relation between feedback and user (If you don't have one already). Your relation would be put within the Feedback model and look like this:
// Feedback.php
public function user() {
return $this->belongsTo('App\User');
}
This will relate the feedback to the user using the user_id on the feedback table.
After this, when calling feedback within your controller, you can then eager load the user relation with each feedback. This can be done within the following:
$feedbacks = Feedback::with('user')->get();
Finally, within your template, you will able to call the user through each feedback by doing the following:
{{ $feedback->user->name }}
Note: This example assumes name is a field on your user table.
Related
I have 2 tables in my db, 1 for Collabs and 1 for Projects
I want when I view a project, to display the collabs based on that project (if i press view on a project for example project with id = 10, to display all Collabs for the project that is id 10).
For Collabs table I have id_project that is wanted to be in relationship with id from Project table, so when I insert a new collab in my Collabs table it takes the id from the project I inserted.
For now, this is how I display the collabs, and i display them all for all projects but I don't want that.
#forelse ($istoric as $istProj)
<div class="mb-3">
<table class='table'>
<tr class="table-row-heads">
<th>Id Proiect</th>
<th>Tip actiune </th>
<th>Colaborator </th>
<th>Suma </th>
<th>Data </th>
</tr>
<tr class="table-row-data">
<td>{{ $istProj->id_proiect }}</td>
<td>{{ $istProj->action_type }}</td>
<td>{{ $istProj->colaborator_id }}</td>
<td>{{ $istProj->suma }}</td>
<td>{{ $istProj->data }}</td>
</tr>
</table>
</div>
#empty
<div class="card-body">
<h2>Nu au fost gasite inregistrari</h2>
</div>
#endforelse
You should really consider reading and watching more videos on how relationships and eloquent works, I hope this below is a good reference for you to get started, please read carefully, and sorry I couldn't translate back to romanian, and to avoid any mistakes, I kept my code in english.
Caloboratori = Colaborators
Istoric Proiecte = Project History
id || auto_increment
project_id || bigInteger()
colaborator_id || bigInteger()
Proiecte = Project
id || auto_increment
Project Model
/* To load the history, we will be using hasMany relationship, because for each
project, we have lots of history, please read more on one-to-many relationships here
https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
Istoric Proiecte = Project History
id || auto_increment
project_id || bigInteger()
colaborator_id || bigInteger()
*/
public function histories() {
return $this->hasMany(ProjectHistory::class);
}
Project History Model
//We will reverse the one-to-many relationship, with belongsTo here. | example: project_id
public function project() {
return $this->belongsTo(Project::class);
}
//We will reverse the one-to-many relationship, with belongsTo here. | example: colaborator_id
public function colaborator() {
return $this->belongsTo(Colaborator::class);
}
Projects Controller:
// Show a list of all projects
public function index() {
//Get all projects
$projects = Project::all();
//Load all of the project relationships that we will be using
$projects->load('histories.colaborator');
return view('projects.index', compact('projects'));
}
// Show a single project
public function show(Project $project) {
//Load all of the project relationships that we will be using
$project->load('histories.colaborator');
//Assign the loaded project history
$histories = $project->histories;
return view('projects.show', compact('project', 'histories'));
}
projects.index Blade: in this blade, you can forloop thru all of your projects model, and assign them as $project, since we loaded the relationships earlier from the controller.
You can easily access the relationships using $project->histories then assign each history model to $history.
Then you can go one step inside of the history relationship and call the inner relationship of colaborator with $history->colaborator
#foreach ($projects as $project)
<p>Project id: {{ $project->id }}
<p>Project name: {{ $project->name }}
<h1>Project History list</h1>
#foreach ($project->histories as $history)
<ul>
<li>ID: {{$history->id}}</li>
<li>Name: {{$history->name}}</li>
<li>Colaborator Name: {{$history->colaborator->name}}</li>
</ul>
#endforeach
#endforeach
projects.show Blade: in this blade, we have a single project, and you can forloop thru all of your history models, since we loaded the relationships from the controller.
We assigned the histories collection as $histories then assign each history model to $history
Then you can go one step inside the history relationship and call the inner relationship of colaborator with $history->colaborator
<p>Project name: {{ $project->name }}
<h1>Project History list</h1>
#foreach ($histories as $history)
<ul>
<li>ID: {{$history->id}}</li>
<li>Name: {{$history->name}}</li>
<li>Colaborator Name: {{$history->colaborator->name}}</li>
</ul>
#endforeach
If you use model Collab, and within you have project relation , than you can use
Collab::query()
->with('project', function ($q) use ($id) {
return $q->where('id', $id);
})
->get();
Or you can use query builder as well
DB::table('collabs')
->select('collabs.*')
->join('projects', 'projects.id', '=', 'collabs.project_id')
->where('projects.id', $id)
->get();
Just adjust it according what you really need.
I have 2 relationships that point to the same User model: operador() and profesional().
class Cita extends Model
{
public function paciente(){
return $this->belongsTo('\App\Models\Paciente');
}
public function profesional(){
return $this->belongsTo('\App\Models\User');
}
public function operador(){
return $this->belongsTo('\App\Models\User');
}
}
In the view I call them like this:
#foreach ($comisiones as $comision)
<tr>
<td>{{ $comision->paciente->name }}</td>
<td>{{ $comision->profesional->name }}</td>
<td>{{ $comision->operador->name }}</td>
<td>{{ number_format($comision->total, 0, '.', '.') }}</td>
<td>{{ $comision->estado }}</td>
</tr>
#endforeach
The program crashes on me when it tries to call $commision->operador->name. If I leave it as a comment it works without problems. But it gives me an error when I have the 2 relations at the same time.
Can I have 2 relationships pointing to the same model? And if not, what alternative do I have? Thanks
For the fact that you are using belongTo relationship, that means User is the parent model and Professional and Operador are the child model.
Hence, it is expected that the table for Professional has a column called user_id, thesame thing for the Operador table, it should have user_id column.
With this the relationship will work just fine.
Yes, you can have 2 relationships pointing to the same model.
When you write :
public function operador(){
return $this->belongsTo('\App\Models\User');
}
Laravel expects that The Migration (The table citas) has a column named operador_id. So Yes you can have multiple relationships to the same model.
I've looked at other questions that have answered this same error and I understand I have a property that cannot be defined but not sure how to make it defined. First time using Many-to-Many relationship so I'm assuming this is the issue.
Project Table
id
company
stage
status
date_started
date_finished
timestamps
Employee Table
id
name
department
timestamps
Employee_Project Table
id
employee_id
project_id
In both models, I have belongsToMany and for the show function in the ProjectController, I have:
$projects = Project::find($id);
In the view, I want to show company name ($projects->company) and the employees on that project with their respective departments. These do not work:
$projects->employees->name
$projects->employees->department
How do I access these properties?
--Update--
Jeff's answer works but I set up a table like
<thead><th>PM</th><th>AM</th></thead>
<tbody><td>#if($employee->department == 'Project Manager'){{ $employee->name }}</td>
<td>#else($employee->department == 'Account Manager'){{ $employee->name }}</td></tbody>#endif
and this does not work to show the correct employees in their respective sections. Any ideas how to fix this?
Your problem is that $project->employees is a collection, not an individual instance of an employee. You will need to iterate over the collection in order to access each individual employee's name:
foreach($project->employees as $employee) {
echo $employee->name;
echo $employee->department;
}
Update
It looks like you may need to restructure as below, I think you probably want the entire if construct to be within the same table cell, though I could be wrong:
<tbody>
#foreach($project->employees as $employee)
<tr>
<td>
#if($employee->department == 'Project Manager')
{{ $employee->name }}
#elseif($employee->department == 'Account Manager')
{{ $employee->department }}
#else
<!-- What about non project/account managers? -->
#endif
</td>
</tr>
#endforeach
</tbody>
I could be totally wrong, and maybe you want account managers and project managers in different columns:
<tr>
<td>
#if($employee->department == 'Project Manager')
{{ $employee->name }}
#endif
</td>
<td>
#if($employee->department == 'Account Manager')
{{ $employee->name }}
#endif
</td>
</tr>
Be sure to take a look at the documentation on Blade if statements.
Update 2
If you want to order how the related entities are coming back, you can do something like this (assuming you want the employees ordered by their department:
$project->employees()->orderBy('department', 'DESC')->get();
Notice you're no longer accessing the property, you're calling the relation method employees() so that you can modify the query prior to it being executed.
Here's a link to the documentation on ordering queries, and there is also another StackOverflow question on it.
So I figured out how to show the correct person to show up under their respective departments in the table and I wanted to share.
Thanks to #jeff-lambert I added the foreach loop but I had added to the entire row like in his first update. This did not work because the employees were not showing up in their respective departments. For example, account managers were showing up under the project manager . To resolve this, I had to add a foreach loop to each .
<td>
#foreach($projects->employees as $employee)
#if($employee->department == 'Project Manager')
{{ $employee->name }}
#endif
#endforeach
</td>
Hope this helps explain it.
I'm trying to echo out the name of the user in my article and I'm getting the
ErrorException: Trying to get property of non-object
My code:
Models
1. News
class News extends Model
{
public function postedBy()
{
return $this->belongsTo('App\User');
}
protected $table = 'news';
protected $fillable = ['newsContent', 'newsTitle', 'postedBy'];
}
2. User
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
Schema
table users
table news
Controller
public function showArticle($slug)
{
$article = News::where('slug', $slug)->firstOrFail();
return view('article', compact('article'));
}
Blade
{{ $article->postedBy->name }}
When I try to remove the name in the blade {{ $article->postedBy }} it outputs the id, but when I try to add the ->name there it says Trying to get property of non-object but I have a field namein my table and aUser` model. Am I missing something?
Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).
I got it working by using Jimmy Zoto's answer and adding a second parameter to my belongsTo. Here it is:
First, as suggested by Jimmy Zoto, my code in blade
from
$article->poster->name
to
$article->poster['name']
Next is to add a second parameter in my belongsTo,
from
return $this->belongsTo('App\User');
to
return $this->belongsTo('App\User', 'user_id');
in which user_id is my foreign key in the news table.
If you working with or loops (for, foreach, etc.) or relationships (one to many, many to many, etc.), this may mean that one of the queries is returning a null variable or a null relationship member.
For example: In a table, you may want to list users with their roles.
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->role->name }}</td>
</tr>
#endforeach
</table>
In the above case, you may receive this error if there is even one User who does not have a Role. You should replace {{ $user->role->name }} with {{ !empty($user->role) ? $user->role->name:'' }}, like this:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ !empty($user->role) ? $user->role->name:'' }}</td>
</tr>
#endforeach
</table>
Edit:
You can use Laravel's the optional method to avoid errors (more information). For example:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ optional($user->role)->name }}</td>
</tr>
#endforeach
</table>
If you are using PHP 8, you can use the null safe operator:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user?->name }}</td>
<td>{{ $user?->role?->name }}</td>
</tr>
#endforeach
</table>
I implemented a hasOne relation in my parent class, defined both the foreign and local key, it returned an object but the columns of the child must be accessed as an array.
i.e. $parent->child['column']
Kind of confusing.
REASON WHY THIS HAPPENS (EXPLANATION)
suppose we have 2 tables users and subscription.
1 user has 1 subscription
IN USER MODEL, we have
public function subscription()
{
return $this->hasOne('App\Subscription','user_id');
}
we can access subscription details as follows
$users = User:all();
foreach($users as $user){
echo $user->subscription;
}
if any of the user does not have a subscription, which can be a case.
we cannot use arrow function further after subscription like below
$user->subscription->abc [this will not work]
$user->subscription['abc'] [this will work]
but if the user has a subscription
$user->subscription->abc [this will work]
NOTE: try putting a if condition like this
if($user->subscription){
return $user->subscription->abc;
}
It happen that after some time we need to run
'php artisan passport:install --force
again to generate a key this solved my problem ,
I had also this problem. Add code like below in the related controller (e.g. UserController)
$users = User::all();
return view('mytemplate.home.homeContent')->with('users',$users);
Laravel optional() Helper is comes to solve this problem.
Try this helper so that if any key have not value then it not return error
foreach ($sample_arr as $key => $value) {
$sample_data[] = array(
'client_phone' =>optional($users)->phone
);
}
print_r($sample_data);
Worked for me:
{{ !empty($user->role) ? $user->role->name:'' }}
In my case the problem was in wrong column's naming:
In model Product I've tried to access category relationship instance to get it's name, but both column name and relationship had the same name:
category
instead of:
category_id - for column name
category - for relationship
Setting up key name in relationship definition like
public function category():hasOne
{
return $this->hasOne(Category::class,'category');
}
didn't help because as soon as Laravel found property named category gave up on looking for relationship etc.
Solution was to either:
change property name (in model and database) or
change relationship name (Eg. productCategory )
It wasn't an error in my case. However, this happened to me when I was trying to open users.index, because while testing I've deleted some data from the 'STUDENTS' table and in the 'USERS' table, a foreign key ('student_id') represents the 'STUDENTS' table. So, now when the system tries to access the 'USERS' table in which foreign key ('student_id') is null since the value got deleted from the 'STUDENTS' table.
After checking for hours when I realise this, I insert the same data again in the 'STUDENTS' table and this resolved the issue.
I have 3 table as mentioned below.
Table 1(user):
id username password Name Age
Table 2(tasks):
id task_name description
Table 3(logs)
id user_id task_id date hours
Table Relationships:
user has_many logs
task has_many logs
logs belongs_to user
logs belongs_to task
what i am trying to achieve is to get the logs with the user Name, task Name, date and hours.
Controller:
return View::make('log.index')
->with('logs',log::all());
Blade template
#foreach($logs as $log)
<tr>
<td>{{$log->id}}</td>
<td>{{$log->users()->name}}</td>
<td>{{$log->tasks()->name}}</td>
<tr>
#endforeach
but unable to fetch users Name and Tasks name from the respective table. any help is appreciated.
A better way is to define inverse hasMany relation in your Model, as documented here
So in your logs model, probably you need to define:
class Log extends Eloquent {
protected $table = "logs";
public function user(){
return $this->belongsTo('User');
}
public function task(){
return $this->belongsTo('Task');
}
}
Then in your view you can either use :
$log->user()->first()->name
or even better, by using Dynamic Properties:
$log->user->name
$log->users() and $log->tasks() returns a query object. Below, each call returns the result which is the same as calling $log->users()->get() and $log->tasks()->get(). Because the relationships are many to many, you'll need to iterate over $log->users and $log->tasks to retrieve each record.
#foreach($logs as $log)
<tr>
<td>{{$log->id}}</td>
<td>
#foreach($log->users as $user)
{{$user->name}},
#endforeach
</td>
<td>
#foreach($log->tasks as $task)
{{$task->name}},
#endforeach
</td>
<tr>
#endforeach
If you want a specific user/task attached to a log you'll have to build a query.
#foreach($logs as $log)
<tr>
<td>{{$log->id}}</td>
<td>{{$log->users()->where('id', '=', $userID)->first()->name}} </td>
<td>{{$log->tasks()->where('id', '=', $taskID)->first()->name}} </td>
<tr>
#endforeach