- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有四个模型:
关系定义如下:
问题是我试图通过内置的 Laravel 关系访问 User->Opportunity 关系,但如果没有自定义查询或机会上的附加 user_id 列,我似乎无法做到这一点表以允许直接访问(即使可以从 Store->Client 关系中推断出一个)。如果可以避免的话,我也不喜欢嵌套的 foreach
循环。
我的问题:
在这种情况下,有没有一种方法可以更深入地直接访问用户的商机?实际Model代码及所有相关关系如下:
用户
class User extends Eloquent{
public function clients(){
return $this->hasMany('Client');
}
public function stores(){
return $this->hasManyThrough('Store', 'Client');
}
public function proposals(){
return $this->hasMany('Proposal');
}
public function opportunities(){ //This does the job, but I feel like it could be better
return Opportunity::join('stores', 'stores.id', '=', 'opportunities.store_id')->
join('clients', 'clients.id', '=', 'stores.client_id')->
join('users', 'users.id', '=', 'clients.user_id')->
select('opportunities.*')->
where('users.id', $this->id);
}
public function getOpportunitiesAttribute(){ //This just helps mimic the hasManyThrough shorthand
return $this->opportunities()->get();
}
}
客户端
class Client extends Eloquent{
public function stores(){
return $this->hasMany('Store');
}
public function user(){
return $this->belongsTo('User');
}
public function opportunities(){
return $this->hasManyThrough('Opportunity', 'Store');
}
}
商店
class Store extends Eloquent {
public function client(){
return $this->belongsTo('Client');
}
public function opportunities(){
return $this->hasMany('Opportunity');
}
}
机会
class Opportunity extends Eloquent {
public function store(){
return $this->belongsTo('Store');
}
}
最佳答案
我不认为在 Laravel 中有这样的方法。您必须创建自定义查询。此自定义查询可能非常昂贵,因为将执行多个查询。因此,根据我的说法,最佳解决方案是将用户和机会与外键相关联。
但是,如果您不想使用外键链接 User 和 Opportunity,则可以创建自定义查询来处理此问题。只需在 Opportunity 和 Client 模型之间添加一个“hasManyThrough”关系,例如,
<?php
class Client extends Eloquent{
public function store(){
return $this->hasMany('Store');
}
public function user(){
return $this->belongsTo('User');
}
public function opportunity(){
return $this->hasManyThrough('Opportunity', 'Store');
}
}
然后在用户模型中创建一个静态函数。
<?php
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
public function client(){
return $this->hasMany('Client');
}
public function store(){
return $this->hasManyThrough('Store', 'Client');
}
public static function getOpportunityOfUser($userId)
{
$clients = User::find($userId)->client;
foreach ($clients as $client) {
$opportunities[] = Client::find($client->id)->opportunity;
}
return $opportunities;
}
}
现在您可以一次性访问为用户提供的机会,例如,
Route::get('/', function()
{
return $usersOpportunities = User::getOpportunityOfUser(1);
});
这将返回与 ID 为“1”的用户相关的所有客户的所有机会。
关于php - 遥远的HasManyThrough,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938081/
描述: 我的应用程序结构如下:Property与 Manager 有多对多关系, 和 Unit与 Property 有一对多关系,即一个经理可以管理多个属性,一个属性可以有多个经理帐户,一个属性可以有
我有这张 table , categories: id | name | slug Products: id | name | slug | category_id | brand_id Brands
我正在为一个项目开发小型 CMS,其表格如下: - pages - id … - translations - page_id … - menus - id
我想获取帖子的标签。 这是我的数据透视表 post body tag name post_tag post_id tag_id 据我所知并能够理解hasManyThrough() 是为
在我的 Laravel 应用程序中,我有以下类: class Product extends Model { public function extended() {
如果我们取 Laravels example在 hasManyThrough 上并将其更改如下: countries id - integer name - string users
我正在使用 laravel 创建一个消息传递应用程序。我使用了四种迁移(用户、对话、对话成员和对话回复)。我还定义了模型之间的关系。 用户的详细信息将存储在用户表中。对话 ID 将存储在对话表中。每个
我有以下型号: Product BaseProduct BaseCode Series 它们中的每一个都是一个独立的实体,但它们是相关的。 Product有一个指向 BaseProduct 的外键,
这些 HasManyThrough 与 HasAndBelongsToMany 之间有什么区别?? https://docs.strongloop.com/display/public/LB/HasM
我正在尝试获取属于custom_view 的所有过滤器。例如:$user->custom_views->first->filters 下面的代码给了我一个空集合。我已经尝试了所有方法,但无法理解为什么
我需要有关 Laravel Eloquent 模型关系的帮助。我有三张 table : 项目 item_id slideshowID 幻灯片 slideshow_id 图像 image_id slid
我浪费了一整天的时间,但无法弄清楚 hasManyThrough 关系。 我有这样的关系: # get related users public function users() { retu
我的用户模型中有以下关系 public function events($user_id) { return $this->find($user_id)->hasManyThrough('Ev
Laravel 文档似乎表明 hasManyThrough 声明只能用于“深”两级的关系。更复杂的关系呢?例如,一个User有很多Subject,每个Subject有很多Deck,每个Deck有很多C
我有 3 个表:company users invoice。 公司 hasMany 用户。 一个用户 belongsTo 一个公司并且一个用户 hasMany 发票。 一张发票属于一个用户。 现在
您好,我的实体之间存在以下关系。 User - id - other stuff NeighborhoodFilter - id - userId - neighborhoodId
在 Eloquent 的文档中,据说我可以将所需关系的键传递给 hasManyThrough . 假设我有名为 Country、User、Post 的模型。一个 Country 模型可能有许多 Pos
table groups ( id int auto_increment primary key, ... ); table users ( id int auto_incre
我有这些表: 条目表 --------------------------------- | id | blog_id | title | ---------------------
我有这些表: 条目表 --------------------------------- | id | blog_id | title | ---------------------
我是一名优秀的程序员,十分优秀!