One to Many to One relationship in laravel eloquent


One to Many to One relationship in laravel eloquent



I have company, employee and motorCycle table.


company


employee


motorCycle



One Company has many employee. One employee has One motorCycle



Company.php


<?php

namespace AppModel;

use IlluminateDatabaseEloquentModel;

class Company extends Model
{
protected $table = 'company';
protected $primaryKey = '_id';

public function employee()
{
return $this->hasMany(Employee::class);
}
}
?>



Employee.php


<?php

namespace AppModel;

use IlluminateDatabaseEloquentModel;

class Employee extends Model
{
protected $table = 'employee';
protected $primaryKey = '_id';

public function motorCycle()
{
return $this->hasOne(MotorCycle::class, 'motorCycle', 'id');
}
}
?>



MotorCycle.php


<?php

namespace AppModel;


use IlluminateDatabaseEloquentModel;

class MotorCycle extends Model
{
protected $table = 'motorCycle';
protected $primaryKey = 'id';

public function employee()
{
return $this->belongsTo('MotorCycle::class');
}
}
?>



I would like to fetch result in controller like below


public function show(Company $company)
{
return $company->employee()->offset(0)->limit(20)->motorCycle()->get();

}



I am trying to browse this URL http://127.0.0.1:8000/api/comapanys/1


http://127.0.0.1:8000/api/comapanys/1



My route is like below



Route::apiResource('comapanys', 'ComapanyController');


Route::apiResource('comapanys', 'ComapanyController');




2 Answers
2



what kind of result do you want to display? you want to have a list of employees and his/her motorcycle in a certain company?



may you can return a query like this.


public function show(Company $company)
{
return $company->employee()->with('motorCycle')->offset(0)->limit(20)->get();
}





Thanks @Kevin. Yes, I want to have a list of employees and his/her motorcycle in a certain company.
– abu abu
Jun 30 at 9:06





Thanks @Kevin. Your solution is working. Thanks.
– abu abu
Jun 30 at 9:40





@abuabu no problem dude :)
– Kevin Loquencio
2 days ago




There is to many problem in your code.
First, for example you write class company extends Model but in controller you use Company $company. Also for class employee class employee extends Model but in motoCycle class return $this->belongsTo('AppModelEmployee');. Use naming convention like first letter uppercase for model name .
Second, im not sure but I dont think you can chain eloquent methods like this return $company->employee()->offset(0)->limit(20)->motorCycle()->get();. Offset and limit should be on the end of chain.
Also use (Employee::class) instead of ('AppModelEmployee')


class company extends Model


Company $company


class employee extends Model


return $this->belongsTo('AppModelEmployee');


m not sure but I don


return $company->employee()->offset(0)->limit(20)->motorCycle()->get();


(Employee::class)


('AppModelEmployee')





Thanks @zozodev. I edited code. Thanks.
– abu abu
Jun 30 at 8:50






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Extract Id from Twitch Clip URL

Why are these constructs (using ++) undefined behavior in C?

I'm Still Waiting (Diana Ross song)