我应该在 Laravel 的多个模型中使用什么雄辩的关系 [英] What eloquent relation should I use in my multiple model in Laravel

查看:48
本文介绍了我应该在 Laravel 的多个模型中使用什么雄辩的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过订单 ID 查看某个订单库.我可以显示与订单相关的公司,但后来我意识到我还需要显示订购该公司的联系人我需要在视图中显示.

挖掘后我了解到我可以使用 hasManyThrough 但不知道如何实际显示数据.我什至不确定 hasManyThrough 是否可以满足我的需求.

这是简化的表关系;

公司联系方式订单id company_id company_id

联系人表格屏幕

公司表

订单表

公司模式

公共函数orders(){返回 $this->hasManyThrough(订单::类,联系人::班级,'company_id','company_id','ID','ID');}

订单模型

公共职能公司(){返回 $this->hasOne('App\Companies', 'id','company_id');}公共函数 contact(){返回 $this->hasOne('App\Contacts', 'id','companies_id');}

我正在尝试显示这样的数据

<strong>收件人:</strong><h4>{{ $orders->companys->comp_name }}</h4>//还行吧<p>{{ $orders->companys->comp_address }}</p>//还行吧<p>{{ $orders->contact['cont_name'] }}</p>//这应该是那个公司的联系人

关于如何实现这一目标的任何建议?在此先感谢您!

解决方案

在 Cascade 1-to-Many-to-Many 样式中,您需要 hasManyThrough.就像艺术家有很多专辑有很多歌曲一样,所以艺术家通过专辑拥有很多歌曲(艺术家->专辑->歌曲).

就你而言:

1- 公司有一个或多个联系人,假设您将使用业务逻辑代码将其限制为 1

2- 一个公司有很多订单,一个订单属于一个公司

3- 联系人属于公司

所以你可以使用代码:

更新 -----

当您在评论中指定公司可以拥有多个联系人时,您可以定义与联系人的附加 hasMany 关系.

<小时>

公司模式

hasOne('App\Contacts', 'companies_id');}公共函数contacts(){返回 $this->hasMany('App\Contacts', 'companies_id');}公共函数命令(){返回 $this->hasMany('App\Orders', 'company_id');}

接触模型

belongsTo('App\Companies', 'companies_id');}

订单模型

belongsTo('App\Companies', 'company_id');}

最后在 Blade 视图中

<strong>收件人:</strong><h4>{{ $orders->company->comp_name }}</h4>//还行吧<p>{{ $orders->company->comp_address }}</p>//还行吧<p>{{ $orders->company->contact->cont_name }}</p>//这应该没问题

I am working with viewing a certain orders base from orders id. I have no problem displaying the company connected to the orders but later I realize i need also to display the contact person from that company who ordered I need to display that in view.

Upon digging I learned that i could use hasManyThrough but don't know how to actually display the data. I am not even sure if hasManyThrough can give what I need.

Here's the simplified table relation;

Companies                Contacts                 Orders
   id                  companies_id             company_id

contacts table screen

Companies table

orders table

Companies Model

public function orders()
{
    return $this->hasManyThrough(
        Orders::class, 
        Contacts::class, 
        'companies_id', 
        'company_id', 
        'id', 
        'id' 
    );
}    

Orders Model

public function companies()
{
   return $this->hasOne('App\Companies', 'id','company_id');
}

public function contact()
{
   return $this->hasOne('App\Contacts', 'id','companies_id');
}

I am trying to display the data like this

<div class="col-xs-6">
    <strong>TO:</strong>
    <h4>{{ $orders->companies->comp_name }}</h4> //this is ok
    <p>{{ $orders->companies->comp_address }}</p> //this is ok
    <p>{{ $orders->contact['cont_name'] }}</p> //this should be the contact person from that company
</div>

any suggestion on how can I achieve this? thank you so much in advance!

解决方案

You need hasManyThrough on a Cascade 1-to-Many-to-Many style. Like an Artist has Many Albums that has Many Songs, so an Artist has Many Songs through Albums (Artist->Albums->Songs).

In your case :

1- Company has One or multiple Contacts, let's say you will limit it to 1 with your Business Logic codes

2- A Company has Many Orders and an Order belongs To a Company

3- A Contact belongs to a Company

So you can use the Codes :

UPDATE -----

As you specify in Comment that Company can have Multiple Contacts, then you can define an additional hasMany relation to Contact.


In Companies Model

<?php
// Companies model

public function contact()
{
   return $this->hasOne('App\Contacts', 'companies_id');
}

public function contacts()
{
   return $this->hasMany('App\Contacts', 'companies_id');
}

public function orders()
{
   return $this->hasMany('App\Orders', 'company_id');
}

In Contact Model

<?php
// Contacts model
// Keep it singular since it's only one Company
public function company()
{
   return $this->belongsTo('App\Companies', 'companies_id');
}

In Order Model

<?php
// Orders model
// Keep it singular since it's only one Company
public function company()
{
   return $this->belongsTo('App\Companies', 'company_id');
}

And finally in the Blade view

<div class="col-xs-6">
    <strong>TO:</strong>
    <h4>{{ $orders->company->comp_name }}</h4> //this is ok
    <p>{{ $orders->company->comp_address }}</p> //this is ok
    <p>{{ $orders->company->contact->cont_name }}</p> //this should be ok
</div>

这篇关于我应该在 Laravel 的多个模型中使用什么雄辩的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆