Relationship: belongsTo this model or a different model - laravel

We have the following
Manager
[id]
Companies
[id]
[manager_id] not nullable
Stores
[id]
[company_id]
[manager_id] *nullable*
I am looking for a single Eloquent relationship for the manager of every store.
Any suggestions?

Building on your comment #Sanjay S
class Store extends Model {
public function manager() {
return $this->belongsTo('App\Manager');
}
public function getManager() {
if (is_null($this->manager)) {
return $this->company()->manager;
} else {
return $this->manager;
}
Then when you call $store->getManager() you will get the company manager if store manager_id is null

You question is not clear as your relationship in the stores table is like manager_id [nullable] and you are asking for a single Eloquent relationship for the manager of every store. But from my guesses your Eloquent models should be look like this
class Manager extends Model {
public function stores() {
return $this->hasMany(App\Store::class);
}
public function company() {
return $this->hasOne(App\Company::class);
}
}
Company class
class Company extends Model {
public function manager() {
return $this->belongsTo(App\Manager::class);
}
}
Store class
class Store extends Model {
public function manager() {
return $this->belongsTo(App\Manager::class);
}
}
Hope this solution works for you.

Related

One to Many Relationship Over Pivot Table

I have existing data in the database, and I want to create a one-to-many relationship. How can I limit or simulate a one-to-many relationship when I'm using a pivot table?
I want to get the data from the pivot table, so I thought it could be done like this.
class Asset extends Model {
public function publisher() {
return $this->belongsTo(Publisher::class, 'asset_publisher');
}
}
-
class Publisher extends Model {
public function assets() {
return $this->hasMany(Asset::class, 'asset_publisher');
}
}
A possible way to simulate the one-to-many is creating a model for the pivot table "AssetPublisher" so you will now have
class Publisher extends Model {
public function assets() {
return $this->belongsToMany(Asset::class, 'asset_publisher');
}
}
class AssetPublisher extends Model {
public function publisher() {
return $this->belongsTo(Publisher::class);
}
}
class Asset extends Model {
public function assetPublisher() {
return $this->hasOne(AssetPublisher::class)->with('publisher');
}
public function getPublisherAttribute(){
return $this->assetPublisher->publisher;
}
}
You can now call $asset->publisher to give you the Publisher instance and $publisher->assets()->get() to get all assets instances

Laravel Eloquent for hasMany relation cannot find records

I can not find why it does not work.
In my Course model I have defined relation:
class Course extends Model {
public function courseDates() {
return $this->hasMany(CourseDate::class, 'course_id');
}
}
And in my CourseDate model this:
class CourseDate extends Model {
public function course() {
return $this->belongsTo(Course::class);
}
}
When I try to access CourseDates from Course I will always get null, but when I access Course from CourseDate, it works and I see all data:
var_dump(CourseDate::where('id', 1)->first()->course->name); => output: "Course 1"
var_dump(Course::where('id', 1)->first()->courseDate); => output: null
And what's strange when I try it with another course (like ID 2) then it works. The data is absolutely the same in the database. Any Ideas?
You shouldent add class like CourseDate::class instead you should add it like below
class Course extends Model {
public function courseDates() {
return $this->hasMany('App\CourseDate', 'course_id');
}
}
And in your CourseDate model
class CourseDate extends Model {
public function course() {
return $this->belongsTo('App\Course');
}
}
And your relationship method for Course model is courseDates so you should use it like below
var_dump(Course::where('id', 1)->first()->courseDates);
Documentation : https://laravel.com/docs/5.7/eloquent-relationships

Laravel eloquent relationship with 2 foreign keys

There are 2 tables and 2 models. Table "games" has 2 foreign keys "team_a_id" and "team_b_id"
I try to define relationship but can't understand how build this relationship. One game can have only one team A and only one team B. But one team can have many games.
At this moment I have
class Game extends Model
{
public function teamA()
{
return $this->hasOne('App\Models\Team', 'team_a_id');
}
public function teamB()
{
return $this->hasOne('App\Models\Team', 'team_b_id');
}
}
and
class Team extends Model
{
public function gameTeamA()
{
return $this->belongsTo('App\Models\GameSerie', 'team_a_id');
}
public function gameTeamB()
{
return $this->belongsTo('App\Models\GameSerie', 'team_b_id');
}
}
I have to define relationship in order to find all games where team was team_a or team_b.
e.g.
$allGames = $team->games($id);
Also i am not sure that i defined relationships right
It's the other way around. The model with the foreign key has the BelongsTo relationship:
class Game extends Model
{
public function teamA()
{
return $this->belongsTo('App\Models\Team', 'team_a_id');
}
public function teamB()
{
return $this->belongsTo('App\Models\Team', 'team_b_id');
}
}
class Team extends Model
{
public function gameTeamA()
{
return $this->hasOne('App\Models\GameSerie', 'team_a_id');
}
public function gameTeamB()
{
return $this->hasOne('App\Models\GameSerie', 'team_b_id');
}
}

Laravel polymorphic relationships return null

I'm trying to join an Order and 2 different OrderDetailType tables.
OrderDetailType1
- id
- order_id
OrderDetailType2
- id
- order_id
Order
- id
- detail_type 'type1' or 'type2'
And I have followed Polymorphic Relations example in official Laravel site and adapted to my code like:
class OrderDetailType1 extends Model {
public function order() {
return $this->morphOne('App\Order', 'type_detail');
}
}
class OrderDetailType2 extends Model {
public function order() {
return $this->morphOne('App\Order', 'type_detail');
}
}
class Order extends Model {
public function type_detail() {
return $this->morphTo();
}
}
And I have put Relation::morphMap() into boot() function in AppServiceProvider class already.
use Illuminate\Database\Eloquent\Relations\Relation;
class AppServiceProvider extends ServiceProvider {
public function boot() {
Relation::morphMap([
'type1' => 'App\OrderDetailType1',
'type2' => 'App\OrderDetailType2'
]);
}
}
The example is different from my code. The difference is both foreign key and the attribute that specifying which table to be joined should be in Order. So I cannot use morphTo() and morphOne() like the example to solve this.
I am confuse which classes should contain morphTo() and morphOne(). And is there any overload to specify which table have foreign key and type?
I use Laravel 5.4. Thank You in Advance.
The _id column has to be in the Order table:
OrderDetailType1
- id
OrderDetailType2
- id
Order
- id
- detail_type
- detail_id
class OrderDetailType1 extends Model {
public function order() {
return $this->morphOne('App\Order', 'detail');
}
}
class OrderDetailType2 extends Model {
public function order() {
return $this->morphOne('App\Order', 'detail');
}
}
class Order extends Model {
public function detail() {
return $this->morphTo();
}
}

Laravel Eloquent Relationship Through Another Table

I have the following database tables:
Seasons
id
number
Teams
id
name
Standings
id
season_id
team_id
The question is, how could I get all of the teams in a season through the standings table. At the moment I am getting all of the teams this way:
$teams = [];
$standings = $season->standings;
foreach($standings as $standing){
$teams[] = $standing->team;
}
Is there a way I could do this using Eloquent relationships? I have tried HasManyThrough with no success. These are what my models look like currently:
class Season extends Eloquent{
public function standings(){
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent{
public function team(){
return $this->belongsTo('Team');
}
}
class Team extends Eloquent{
public function standings(){
return $this->belongsToMany('Standing');
}
}
Your relationships look a little off. Here is all the relationships you should need though only the belongsToMany ones are required for this specific scenario of finding all the teams in a season.
class Season extends Eloquent {
public function teams()
{
return $this->belongsToMany('Team', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Team extends Eloquent {
public function seasons()
{
return $this->belongsToMany('Season', 'Standings');
}
public function standings()
{
return $this->hasMany('Standing');
}
}
class Standing extends Eloquent {
public function team()
{
return $this->belongsTo('Team');
}
public function season()
{
return $this->belongsTo('Season');
}
}
You would use the belongsToMany relationship rather than a hasManyThrough to query all the teams in a season. That would look something like...
Season::with('teams')->find($season_id);
foreach($season->teams as $team) {
echo $team->name;
}

Resources