I'm working with Laravel 8 to develop my project and in this project which is a Movie Storage, I have a One to One Relationship between movies table and imdbs table.
So I have already added the relationships into Models like this:
Movie.php:
public function imdb()
{
return $this->hasOne(Imdb::class);
}
And Imdb.php:
protected $fillable = ['rate','movie_id'];
public function movie()
{
return $this->belongsTo(Movie::class);
}
And the Migration of imdbs table goes here:
public function up()
{
Schema::create('imdbs', function (Blueprint $table) {
$table->id();
$table->string('rate');
$table->integer('movie_id')->unsigned();
$table->timestamps();
});
}
As you can see I have set movie_id which defines the id column of movies table.
Now in order to retrieve data on Blade, I did this:
#foreach($movies as $movie)
<tr>
<th scope="row">{{ $movie->id }}</th>
<td>{{ $movie->name }}</td>
<td>{{ $movie->year }}</td>
<td>{{ $movie->imdb->rate }}</td>
</tr>
#endforeach
And the $movies variable comes from this Method:
public function index()
{
$movies = Movie::with('imdb')->get();
return view('admin.movies.index')->with('movies', $movies);
}
But the problem is, I get this error message:
ErrorException Trying to get property 'rate' of non-object
However the rate already has some data in it:
I don't know how to fix this, because everything looks fine and correct.
Your movie may not have an imdb object with the movie's id associated.
To fix, provide a check on the view to determine if it exists:
For example, change:
<td>{{ $movie->imdb->rate }}</td>
to
#if($movie->imdb !== null)
<td>{{ $movie->imdb->rate }}</td>
#else
<td> N/A </td>
#endif
Related
I have
ErrorException
Attempt to read property "match_name" on null (View: C:\xampp\htdocs\Cbangla\resources\views\admin\manage\score\index.blade.php)
Error
I want to fetch all data from my score tables This is my scores table database view
To fetch, all data from the scores table This is what I have in my ScoreController.php
public function index()
{
$data=Score::all();
$team=Team::all();
$match=Matchh::all();
$player=Player::all();
return view('admin.manage.score.index',compact('data','team','match','player'));
}
This is my Score.php model
protected $fillable = ['score_name','score_slug','team_id','match_id','player_id'];
public function team(){
return $this->belongsTo(Team::class);
}
public function matchh(){
return $this->belongsTo(Matchh::class);
}
public function playre(){
return $this->belongsTo(Player::class);
}
This is my index.blade.php
#foreach ($data as $key => $row)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $row->score_name }}</td>
<td>{{ $row->score_slug }}</td>
<td>{{ $row->matchh->match_name }}</td>
<td>{{ $row->team->team_name }}</td>
<td>{{ $row->player->player_name }}</td>
</tr>
#endforeach
As I see, there is problem when score has not match or match not found.
First of all, you have to specify relation column, because your model is named Matchh and laravel is looking for matchh_id column.
Solution:
# Score.php model
public function matchh(){
return $this->belongsTo(Matchh::class,'match_id');
}
Tip 1:
You can use optional function, when you're not sure if model has relation or not
#foreach ($data as $key => $row)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ $row->score_name }}</td>
<td>{{ $row->score_slug }}</td>
<td>{{ optional($row->matchh)->match_name }}</td>
<td>{{ optional($row->team)->team_name }}</td>
<td>{{ optional($row->player)->player_name }}</td>
</tr>
#endforeach
Tip 2:
You can use whereHas function to be sure that Score has Matchh
public function index()
{
$data=Score::query()->whereHas('matchh')->get();
$team=Team::all();
$match=Matchh::all();
$player=Player::all();
return view('admin.manage.score.index',compact('data','team','match','player'));
}
Please add match_id to matchh relationship case the name of your relationship is different than the match id you stored in database
public function matchh(){
return $this->belongsTo(Matchh::class,'match_id');
}
You must provide key in relation because your method name is matchh and in that case relation except matchh_id but in you case it must be match_id.
So:
public function matchh(): BelongsTo
{
return $this->belongsTo(Matchh::class, 'match_id');
}
I have two table SALARIES and POINTAGES And between them a relationship hasMany belongsTo, I want to display for each POINTAGE a SALARIES corresponds but it gives me this error:
Undefined property: stdClass::$salarie
consulter.blade.php
#foreach($pointages as $pointage)
<tr>
<td>{{ $pointage->datep }}</td>
<td>{{ $pointage->chantier }}</td>
<td>{{ $pointage->ouvrage }}</td>
<td>{{ $pointage->nbrj }}</td>
<td>{{ $pointage->solde }}</td>
<td>{{ $pointage->salarie->nom }}</td>
</tr>
#endforeach
Pointage.php
protected $fillable = [
'salarie_id', 'datep', 'solde', 'nbrj' , 'ouvrage' , 'chantier' , 'prime' ,
];
public function salarie(){
return $this->belongsTo('App\Salarie');
}
Salarie.php
public function pointages(){
return $this->hasMany('App\Pointage');
}
pointages migration:
public function up(){
Schema::table('pointages', function (Blueprint $table) {
$table->integer('salarie_id')->unsigned()->after('id');
$table->foreign('salarie_id')->references('id')->on('salaries');
});
}
SalarieController.php
public function consulter(){
$salaries = Salarie::with('pointages')->get();
$pointages = DB::table('pointages')->get();
return view('salarie.consulter', compact('salaries','pointages'));
}
If you want to use a relationship, like public function salarie() on your Pointage model, you need to actually query it. Don't use DB::table(), use Pointage:
$pointages = Pointages::get();
Also, to avoid additional queries when looping $pointages, eager load your relationship:
$pointages = Pointages::with(["salarie"])->get();
In summary, DB::table() does not return a Pointage model, so you can't use ->salarie. Use your model.
Edit: There is a chance that a Pointage doesn't have a Salarie associated, so you need to handle that. Either enforce the relationship with a ->has() query, or check while looping:
$pointages = Pointages::with(["salarie"])->has("salarie")->get();
Or, in your view:
#if($pointages->salarie)
<td>{{ $pointage->salarie->nom }}</td>
#else
<td>No Salarie...</td>
#endif
So i have created 2 models 'Team' & 'Match', 1 controller 'MatchController' and a view 'matches/index.blade.php'.
The Team model has an ID & name.
The Match model has an ID, homeTeam_id & awayTeam_id.
The MatchController has an index method.
The view shows all the matches in de database correctly, but with the homeTeam_id, what I would like is show the name for the teams, from the Team model.
How do i do that? This is what i have now in my view:
#foreach ($matches as $key => $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->date }}</td>
<td>{{ $value->homeTeam_id }}</td>
<td>{{ $value->awayTeam_id }}</td>
</tr>
#endforeach
You can create two relationships between Team and Match models:
public function homeTeam()
{
return $this->belongsTo('App\Team', 'homeTeam_id', 'id');
}
public function awayTeam()
{
return $this->belongsTo('App\Team', 'awayTeam_id', 'id');
}
And then load the data:
$matches = Match::with('homeTeam', 'awayTeam')->get();
To display team name do this:
{{ $value->homeTeam->name }}
Here is my situation;
I have an agreements table which will list the different types of agreements there are. It's a lookup table basically.
I have a client_agreements table which list which clients have signed up to which type of agreement. I have an agreement_id within this table (foreign key id).
As I am using Laravel, here is my Controller method to view all agreements for a specific client;
public function index($client_id)
{
$client_agreements = Agreement::find($client_id);
return View::make('client_agreements.index')
->with('client_agreements', $client_agreements);
}
Here is my Agreement Model;
class Agreement extends Eloquent {
protected $table = 'agreements';
public function client_agreements(){
return $this->hasMany('ClientAgreement', 'agreement_id');
}
}
So I want to output in the view from the agreements table;
agreement_type
level
and from the client_agreements table;
start_date
expire_date
My View code (which I'm sure is wrong but don't know why) is essentially;
#foreach($client_agreements as $client_agreement)
<tr>
<td>{{ $client_agreement->agreement_type }}</td>
<td>{{ $client_agreement->level }}</td>
<td>{{ $client_agreement->start_date }}</td>
<td>{{ $client_agreement->expire_date }}</td>
</tr>
#endforeach
What am I doing wrong?
You can do it like this:
$client = Client::with('agreements')->find($client_id);
$client_agreements = $client->agreements;
return View::make('client_agreements.index')
->with('client_agreements', $client_agreements);
And in your view:
#foreach($client_agreements as $client_agreement)
<tr>
<td>{{ $client_agreement->agreement_type }}</td>
<td>{{ $client_agreement->level }}</td>
<td>{{ $client_agreement->pivot->start_date }}</td>
<td>{{ $client_agreement->pivot->expire_date }}</td>
</tr>
#endforeach
This would require the following setup:
Client model
class Client extends Eloquent {
public function agreements(){
return $this->belongsToMany('Agreement', 'client_agreements')->withPivot('id', 'start_date', 'expire_date');
}
}
Agreement model
class Agreement extends Eloquent {
public function clients(){
return $this->belongsToMany('Client', 'client_agreements')->withPivot('id', 'start_date', 'expire_date');
}
}
For more information read the Eloquent docs (especially the section about relationships)
Good evening peeps, I'm playing a little bit with Laravel, and I have this situation. I have 2 models and 3 tables to display vital signs from a pacient.
Table pacient:
id
names
birthdate
Table vital_sign:
id
description
unit
table pacient_vital_sign
id
pacient_id
vital_sign_id
first_value
second_value
date_time_taken
And the 2 models:
class Pacient extends Eloquent{
public static $timestamps = false;
public function vital_signs(){
return $this->has_many_and_belongs_to('VitalSign', 'pacient_vital_sign', 'pacient_id', 'vital_sign_id');
}
}
class VitalSign extends Eloquent{
public static $timestamps = false;
public static $table = 'vital_signs';
public function pacients(){
return $this->has_many_and_belongs_to('Pacient', 'pacient_vital_sign', 'vital_sign_id', 'pacient_id');
}
}
And for my view I'm sending the pacient whose vital signs I want to display:
$pacient = Pacient::where('active', '=', 1)->first();
So, based on the documentation I could do this to show the pivotal table data:
#foreach($pacient->vital_signs as $sv)
<tr>
<td>{{ $sv->pivot->date_time_taken }}</td>
<td>{{ $sv->description }}</td>
<td>{{ $sv->pivot->first_value }} {{ $sv->pivot->second_value }}</td>
</tr>
#endforeach
But this way, neither the date_time_taken value nor first_value or second_value is showed. Description is displayed.
Now, if I do something like this
#foreach($pacient->vital_signs()->pivot()->get() as $sv)
<tr>
<td>{{ $sv->date_time_taken }}</td>
<td>{{ $sv->description }}</td>
<td>{{ $sv->first_value }} {{ $sv->second_value }}</td>
</tr>
#endforeach
date_time_taken, first_value and secon_value are displayed but I dont know how to reverse the relationship in order to display the value for the related table column, in this case, description from vital_sign table
So I both cases, I'm missing something. Any ideas? What I'm missings?
Thanks
On the Docs:
By default only certain fields from the pivot table will be returned
(the two id fields, and the timestamps). If your pivot table contains
additional columns, you can fetch them too by using the with() method
So in my case, on both models:
return $this->has_many_and_belongs_to('VitalSign', 'pacient_vital_sign', 'pacient_id', 'vital_sign_id')->with('col1', 'col2', 'coln');
and this will work:
#foreach($pacient->vital_signs as $sv)
<tr>
<td>{{ $sv->pivot->date_time_taken }}</td>
<td>{{ $sv->description }}</td>
<td>{{ $sv->pivot->first_value }} {{ $sv->pivot->second_value }}</td>
</tr>
#endforeach
I found this to be useful, but a quick addendum for Laravel 4:
return $this->belongsToMany('Model','pivot_table','this_id','foreign_id')->withPivot('col1','col2');