在laravel中提取关系数据

时间:2017-09-21 03:15:01

标签: php laravel laravel-5.4 laravel-eloquent

我正在尝试在laravel 5.4中构建一个小型应用程序,其中我有一个contacts表,companies表,interactions表和users每个表都有各自的型号。 Interaction正在创建Userclient_contactcontactuser参与其中。每个client_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'.'%');
        });
    })

首先分配用户过滤器,但不按需要显示。帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

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

$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();

我真的不了解您正在开发的报告,但希望这会对您有所帮助。