Using Laravel eloquent relationship (One to Many) returns NULL
Tag : php , By : Pradeep Gowda
Date : March 29 2020, 07:55 AM
wish of those help when Im using Laravel eloquent - relationships (One To Many) the result is NULL. I have setted up data in "work_hour" table, where "workers_id" has value 2 and there is row in "workers" table with id=2 I followed the documentation " Laravel relationships" , You simply need to add the relation to the Model not Controller: class Worker extends Eloquent{
protected $fillable = ['first_name', 'last_name'];
public function workhour(){
return $this->hasMany('Workhour','workers_id');
}
}
|
Eloquent Query always returns null for parent table using oneToOne relationship
Tag : php , By : Anonymous
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You receive null for because your relation definition is wrong. Your profile model should be public function user()
{
return $this->belongsTo('App\User','user_id');
}
@foreach ($profiles as $profile)
{{ $profile->user->email }}
@endforeach
|
Laravel 5 eloquent relationship returns empty or null but the code seems ok
Date : March 29 2020, 07:55 AM
This might help you Your naming convention on the columns in your relation are incorrect (backwards). The convention is ${related_table}_id. To solve this, alter your migration. Othwise, if you don't want to just adjust your migration, then specify the foreign key column in your Autore model as the 2nd argument to your hasMany relationship. public function libri() {
return $this->hasMany('App\Libro', 'id_autore')
}
|
Eloquent hasOne Relationship Returns NULL even when related records exist
Tag : php , By : Singularity
Date : March 29 2020, 07:55 AM
hop of those help? I found the solution to the problem and it was along the same lines of what IzzEps pointed out. The way that I had the relationships originally set up is like this. public function transactionType() {
return $this->hasOne("App\Model\TransactionType", "id");
}
public function transactionStatus() {
return $this->hasOne("App\Model\TransactionStatus", "id");
}
public function transactionType() {
return $this->hasOne("App\Model\TransactionType", "id", "transaction_type_id");
}
public function transactionStatus() {
return $this->hasOne("App\Model\TransactionStatus", "id", "transaction_status_id");
}
|
Laravel Eloquent belongsto relationship returns null
Tag : php , By : Revision17
Date : March 29 2020, 07:55 AM
wish helps you I have two Eloquent models: , Try to point out foregin key and other key in relation, examples: public function post()
{
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
public function user()
{
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}
|