【问题标题】:Extracting relational data in laravel在laravel中提取关系数据
【发布时间】:2017-10-01 08:01:58
【问题描述】:

我正在尝试在laravel 5.4 中构建一个小型应用程序,其中有一个contacts 表、companies 表、interactions 表和users 表,每个表都有各自的模型。 User 正在创建一个 Interaction,其中有 client_contactcontactuser 参与。每个 client_contact 和 contact 共享相同的模型,并且属于在其列中具有类型的公司,例如 Investor、Research、Corporate 等。现在我正在尝试生成一个报告,其中用户与 Investors 进行交互。首先让我向您展示我的模型:

用户模型:

class User extends Authenticatable
{
    use HasApiTokens, Notifiable, SoftDeletes;


    public function interactions()
    {
        return $this->hasMany('App\Interaction');
    }

    public function clients()
    {
        //Each user has been assigned a particular client 
        return $this->belongsToMany('App\Company', 'company_user', 'user_id', 'company_id');
    }

}

联系人模型:

class Contact extends Model
{
    use SoftDeletes, DataViewer;

    public function company()
    {
        return $this
            ->belongsToMany('App\Company', 'company_contact','contact_id', 'company_id')->withTimestamps();
    }

    public function interactions()
    {
        return $this->belongsToMany('App\Interaction', 'contact_interaction','contact_id', 'interaction_id');
    }

    public function clientInteractions()
    {
        return $this->belongsToMany('App\Interaction', 'contact_client_interaction','contact_id', 'interaction_id');
    }
}

公司模式:

class Company extends Model
{
    use SoftDeletes, DataViewer;

    public function contacts()
    {
        return $this->belongsToMany('App\Contact', 'company_contact', 'company_id','contact_id');
    }

    public function users()
    {
        return $this->belongsToMany('App\User', 'company_user', 'company_id', 'user_id')->withTimestamps();
    }

}

交互模型:

class Interaction extends Model
{
    use SoftDeletes;

    public function stellarParticipants()
    {
        return $this->belongsToMany('App\User')->withTimestamps();
    }

    public function clientsAssociation()
    {
        return $this->belongsToMany('App\Contact', 'contact_client_interaction',  'interaction_id', 'contact_id')->withPivot('company_id')->withTimestamps();
    }

    public function contactsAssociation()
    {
        return $this->belongsToMany('App\Contact', 'contact_interaction',  'interaction_id', 'contact_id')->withPivot('company_id')->withTimestamps();
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }

}

现在在我的 ReportController: 我正在做这样的事情:

 public function getAnalystInvestor(Request $request)
{
    $user = User::find($request->id);
    $interactions = Interaction::whereHas('contactsAssociation', function ($query3) {
        $query3->whereHas('company', function ($query4) {
            $query4->where('type', 'like', '%'.'Investor'.'%');
        });
    })->whereHas('user', function ($query1) use($user) {
        $query1->where('name', '=', $user->name);
    })->orWhereHas('stellarParticipants', function ($query2) use($user) {
        $query2->where('name', '=', $user->name);
    })
    ->orderBy('created_at', 'desc')
    ->take(50)
    ->with(['contactsAssociation', 'clientsAssociation', 'stellarParticipants'])
    ->get();

    return response()->json(['interactions' => $interactions], 200);

}

但我无法获得预期的结果,仍然显示了一些不是投资者的数据。我也试过了:

$interactions = Interaction::whereHas('user', function ($query1) use($user) {
        $query1->where('name', '=', $user->name);
    })->orWhereHas('stellarParticipants', function ($query2) use($user) {
        $query2->where('name', '=', $user->name);
    })->whereHas('contactsAssociation', function ($query3) {
        $query3->whereHas('company', function ($query4) {
            $query4->where('type', 'like', '%'.'Investor'.'%');
        });
    })

首先分配用户过滤器但未按需要显示的位置。帮我解决这个问题。

【问题讨论】:

  • 只需创建一个 SQL 视图并为其创建模型。您在使用 ORM 创建的查询数量上遇到了性能问题

标签: php laravel laravel-5.4 laravel-eloquent


【解决方案1】:

您应该能够链接您的查询,类似于:

$cfs = \App\Service::with('items', 'required_services', 'supported_services')
        ->where(function ($query) {
            $query->where('stm', 11)
                ->orWhere('stm', 17)
                ->orWhere('stm', 31);
        })
        ->where('lifecycle_id', '!=', 12)
        ->where('type', 'Customer Facing')
        ->orderBy('service_full_name')
        ->get();

我不太了解您正在编写的报告,但希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2017-03-30
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多