I have the following tables
users userdatas workdays
--------- ----------- --------
id id id
user_id
workday_id
I would like to get a user's workdays (and later get all the users that have a specified workday, but one step at a time). I attempted several variations of this however it's not working. I believe the issue could be that workdays doesn't reference userdatas.
In the User model:
public function workday() {
return $this->hasOneThrough('App\Workday', 'App\Userdata');
}
I've also defined the following:
In Userdata
public function workday() {
return $this->hasOne('App\Workday');
}
In Workday
public function userdatas() {
return $this->belongsToMany('App\Userdata');
}
Does the reverse through relationship have to be defined in Workday? Any tips to get this to work?
Thank you!
You're following wrong relationship. As per your Database table design it should be Many to many relationship(belongsToMany).
In User Model pass the relationship as below.
public function workdays() {
return $this->belongsToMany('App\Workday', 'userdatas','user_id','workday_id');
}
Related
In a project, let's say that we have Customers, and each customer can have one Voucher. The voucher, though, may be for a different thing for different customers - maybe for a Hote, a Car or a Flight.
We have a table of flight voucher codes, a table of hotel voucher codes and a table of car voucher codes.
When a customer is allocated a voucher, therefore, we allocated them the next code for the relevant thing that they're getting a voucher for. But rather than have multiple tables (customer_car_voucher, customer_hotel_voucher, and so on) I would rather have a Voucher table which is, in turn, linked to the relevant voucher type.
What I want to be able to do is just go $customer->voucher->code to get the relevant code, whether that be a flight, a hotel or a car. Other vouchers may be added at a later date, you see, for different things.
I think I can do this using morphable relationships - the voucher morphsTo car, hotel and flight, so within the the voucher table there is a "voucherable_type" and a "voucherable_id". But damned if I can get it to work.
Any help, please? Am I going about it wrong?
you arte right. and for a hint use:
public function customer()
{
return $this->belongsTo(Customer::class):
}
public function voucherable()
{
return $this->morphTo();
}
in voucher model.
and for each flight,car,hotel include:
public function voucher(){
return $this->morphOne(Voucher::class,'voucherable');
}
you can see Laravel morph relationship too for more help.
In Laravel's Eloquent ORM is used for morphable relationships.
First, create two Models AirVoucher and Voucher.
First, the AirVoucher model uses the following relationship.
public function voucher()
{
return $this->morphOne(Voucher::class, 'voucherable');
}
Second, the Voucher model uses the following relationship.
public function voucherable()
{
return $this->morphTo();
}
You can use the following Laravel official relationship document for more help.
Laravel Morph Relationships.
you must use laravel Polymorphic Relationships.
in Voucher model set this model as polymorph model(function name = modelname+"able"):
public function voucherable() \Illuminate\Database\Eloquent\Relations\MorphTo
{
return $this->morphTo();
}
then in Car model (or hotel/fight) set realation(function name= polymorph name):
if each car has one voucher, use morphOne:
public function files(): \Illuminate\Database\Eloquent\Relations\MorphOne
{
return $this->morphOne(Voucher::class, 'voucherable');
}
if each car has many voucher, use morphMany:
public function files(): \Illuminate\Database\Eloquent\Relations\MorphMany
{
return $this->morphMany(Voucher::class, 'voucherable');
}
Retrieving The Relationship
$car = Car::find(1);
$vocher = $car->voucher;
laravel docs
Given below are the tables I have:
keywords
--------
id
keyword
Keyword table has many footprints
footprints
----------
id
keyword_id
footprint
Each serp is associated with a footprint
serps
-----
id
footprint_id
phrase
I need to get the keyword each serp is associated with. By reading about Laravel's Has One through relationship, I get the feeling that it can be done with has one trough relationship.
I want to get the keyword using:
$serp->keyword;
On Serp Model, I wrote the following code which is not working. I will highly appreciate your help to the write right relationship code.
class Serp extends Model {
//
public function keyword(){
return $this->hasOneThrough(
'\App\Keyword',
'App\Footprint') ;
}
}
In your case, you can use has one through to find Serp based on keyword
class Keyword extends Model {
public function serp(){
return $this->hasOneThrough(
'\App\Serp',
'App\Footprint') ;
}
}
But if you want to use $serp->keyword(), you should use inverse relationship of hasOneThrough.
Check this trait out, it might help you
In Laravel, i´m trying to show relation elements between Auth::user (Users) and Departments. In User table i have id, name, and department_id. In Departments table I have id and name.
In user model i create
public function department()
{
return $this->belongsTo('App\Models\Department');
}
Then, in blade template I try
Auth::user()->department
But return null and doesn´t show linked departments. Null is incorrect, all users have departments. Soo, ¿Any help? ¿What´s wrong in relation?
You can try this User::with('departmento')->find(Auth::id());
This method uses less queries - the others go after the user table twice:
auth()->user()->load(['department']);
You should add 'department_id' as a second parameter ($foreignKey) when calling belongsTo method, because it will searching for departmento_id by default.
public function departamento()
{
return $this->belongsTo('App\Models\Departamento', 'department_id');
}
Or just rename User::departamento() method to User::department()
public function department()
{
return $this->belongsTo('App\Models\Departamento');
}
Relation works with Model. Auth uses the session it not uses relation
Use User Model instead
optional(User::find(Auth::id())->departmento)->department_name
I have an order table with a primary key id and another table trips. I trip can have many orders. I have defined the relationship in this way
public function orders_currency() {
return $this->hasMany('App\Order', 'trip_id', 'id')->select('id', 'converted_currency_code');
}
Although I know that, I have to select id of the order table in order to get the result, that's why I am mentioning to select the id of the order table.
Despite this, I am getting the orders_currency index empty. Which angle am I missing in this case?
Any clue will be appreciated.
Did you set up the model correctly?
You'd do something like this in your model script I suppose
class Trips extends Model
{
public function orders() {
return $this->hasMany('Order');
}
I got the solution by doing this way
public function orders_currency() {
return $this->hasOne('App\Order', 'trip_id', 'id')
->select('trip_id','converted_currency_code');
}
I had to select referencing key in this case.
I have two tables departments and course, and I have defined a relationship between them but I received this error message:
Call to undefined relationship in laravel
Can anyone tell me please where's the problem?
Model Code:
public function course()
{
return $this->hasMany('App\Course','departments_id','id');
}
Model Department:
public function department()
{
return $this->belongsTo('App\Departments','departments_id');
}
So problem is so clear. Either in your Course model you have to rename the function department to departments, or to make a new one with the correct name.
First of all little correction in schema $table->integer('department_id'); should be $table->unsignedInteger('department_id');
And the problem is - in your migration column name is department_id but in your relation you have written departments_id there is a extra S!
just correct the column name from model.
in my case, i used:
::with('boards')
i just changed it to :
::with(['boards'])
with brackets
in my department Model:
public function boards()
{
return $this->hasMany(Board::class);
}