I can't access my MODEL field in my database
This is the code that I execute:
#foreach($clients as client)
{{$client->dependents->fname}}
{{$client->dependents->mname}}
{{$client->dependents->lname}}
#endforeach
This code returns an error : Property [fname] does not exist on this collection instance.
When I do this code:
#foreach($clients as client)
{{$client->dependents}}
#endforeach
This code returns a successful array of data.
This is my Client Model:
class Client extends Model
{
protected $guarded = [];
public function dependents() {
return $this->hasMany(Dependent::class);
}
}
And this is my Dependent Model:
class Code extends Model
{
protected $guarded = [];
public function client()
{
return $this->hasOne(Client::class);
}
}
How can I retrieve each field using the eloquent model method?
In order to access the fields on dependents model, you need to make a foreach, because the client model hasMany dependents and when you try to access simply $client->dependents->fname doesn't know which one to access.
So, you need to do the following:
#foreach($clients as $client)
#foreach($client->dependents as $dependent)
{{$dependent->fname}}
{{$dependent->mname}}
{{$dependent->lname}}
#endforeach
#endforeach
It is HasMany relation AND It returns multiple departments so that fname is undefined. Because it has an object of departments, not the department and you are missing $ from #foreach($clients as client).
#foreach($clients as $client)
#foreach($clients->dependents as $department)
{{$dependent->fname}}
{{$dependent->mname}}
{{$dependent->lname}}
#endforeach
#endforeach
Try this, It will work for you. Enjoy!
Related
I'm trying to get the client name , address ect.. based on their relationship, but when I am trying to get the name for exaple i get the error Trying to get property of 'name' of a non-object.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Client;
class Evaluation extends Model
{
protected $table = 'evaluations';
protected $fillable = [
'expert',
];
public function client() {
return $this->belongsTo(Client::class);
}
}
Controller
public function index()
{
$evaluations = Evaluation::with('client')->get();
$users= auth()->user();
return view('evaluation.index', ['evaluations' => $evaluations]);
}
view
#if (count($evaluations) < 1)
<h1>Nici o evaluare nu a fost gasita.</h1>
#else
#foreach ($evaluations as $evaluation)
<tr>
<td class="pl-1">{{$evaluation->id}}</td>
<td class="pl-1">{{$evaluation->expert}}</td>
<td class="pl-1">{{$evaluation->gram1}}</td>
<td class="pl-1">{{$evaluation->client_id}}</td>
#php
dd($evaluation->client_id->nume)
#endphp
<td> Printeaza</td>
</tr>
#endforeach
#endif
An I also have to mention that in my table is nume instead of name it's no english project.
SOLVED, Relationship and all there was good, all I Needed to do was to create an input hidded for taking the id and add the Id on protected $fillable on client model not on evaluation model. It's work and in the view you have to call it like {{$evaluation->client->nume}}, "->client->" = is the relationship that you called. And I SOLVED even the Foreign key problem, in my create_evaluations_table the client_id column was bigInteger and in my create_clients_table the id of client that I wanted to associate was integer. All you need to do if you will have this error is to make them identically like if the id in the table that you want to associate is integer you need to save the integer there also.
I have 2 tables, tn_project has id name cv_id_project and tn_activity has id name id, i want to get the name of the project which is cv_project_name rather its id.
Basically when i click add activity i have select option to choose which project that i use, i manage to get the id of selected project but i want its name(i can select up to 3 project in a single activity)
Model
<?php namespace Activity;
use Illuminate\Database\Eloquent\Model;
class Activity extends Model {
protected $table = 'tn_activity';
public $timestamps = false;
public function projectModel(){
return $this->hasMany('Activity\Project','cv_id_project','id');
}
}
VIEW
#foreach($query as $result)
<td>{{$result->projectModel->cv_project_name}}</td>
#endforeach
Controller
public function index(){
return view('landing-page')
->with('title','Landing Page')
->with('query',Activity::with('projectModel')->get())
->with('projects',Project::all());
}
usually it's work but i don't know what makes it error
and then this error occured
Undefined property: Illuminate\Database\Eloquent\Collection::$cv_project_name (View: C:\xampp\htdocs\PKL\netxcel-2-backup2\resources\views\landing-page.blade.php)
Your Activity projectModel is a collection, because it has hasMany relationship. If it's really true, you should get the project name like this.
#foreach ($result->projectModel as $project)
<td>{{$project->cv_project_name}}</td>
#endforeach
However, if it shouldn't have hasMany relationship than you should change the hasMany part
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 define scope in model like this
class Station extends Model {
protected $primaryKey = 'st_id';
public function scopeByDid($query)
{
return $query->groupBy("st_did");
}
}
I can call byDid from controller but I cannot get it through blade template like this
#foreach ($river->stations->byDid as $didType)
....
#endforeach
how do I get it. Appreciate your response. Thanks
If you're getting a relationship as an attribute (without () at the end) it means the relationship will have already been retrieved before the scope.
To get your code to work you will just need to change your foreach to:
#foreach($river->stations()->byDid()->get() as $didType)
Hope this helps!
Edit: typo
I have two Models: Project and Task. Both are relational to each other:
Project.php
class Project extends Eloquent {
public function tasks()
{
return $this->hasMany('Task');
}
Task.php
class Task extends Eloquent {
protected $guarded = [];
public function project()
{
return $this->belongsTo('Project');
}
Through my ProjectsController I pass the necessary Variables to my Projects View, like this:
ProjectsController:
public function index()
{
$projects = Project::with('tasks')->get();
return View::make('projects.index')
->with('projects', $projects);
}
And in my View I loop through every Project to show everything on a table:
Projects List
<table>
<tr>
<th>Id</th>
<th>Titel</th>
<th>Description</th>
<th>Tasks</th>
</tr>
#foreach($projects as $project)
<tr>
<td>{{$project->id}}</td>
<td>{{$project->title}}</td>
<td>{{$project->description}}</td>
<td>{{$project->task}}</td>
</tr>
#endforeach
</table>
As you can see, the last td-Tag, should access data from the tasks table.
I know that the above View does not work. But I want to know in general, how I would go about it, if I want to output the number of tasks, each project has.
Or anything else, that explains, how I can access different tables, through relational Models, in this particular situation.
since you defined the project model with $this->hasMany('Task'); and fetched models with their tasks, you can simply do
{{ $project->task()->count() }}
be sure to call the tasks as if it were a function, instead of a property.