简体   繁体   English

Laravel我可以将数据透视表用作模型吗

[英]Laravel can I use pivot table as a model

I have three tables staff , customer , service . 我有三张桌子的工作人员客户服务 人员

I have created pivot for customer and service as customer_service (has extra fields). 我已经为customer_service和customer_service (具有额外的字段)创建了枢轴。

Now I want to link the staff to customer_service. 现在,我想将工作人员链接到customer_service。 So i tried to use customer_service as a model ' Custserv ' and tried to relate with staff . 因此,我尝试将customer_service用作“ Custserv模型,并尝试与员工联系 It didnt workout. 它没有锻炼。

Because I don't want staff linking directly to customer and service 因为我不希望员工直接链接到客户和服务

I had this following relationship working 我有以下这种关系在工作

/*Model - Service*/
public function customer(){
    return $this->belongsToMany('customer')->withPivot(
        'start_date',
        'stop_date',
        'rem_date',
        'due_date',
        'status'
        );
}

/*Model - customer*/
public function services(){
    return $this->belongsToMany('Service')->withPivot(
        'start_date',
        'stop_date',
        'rem_date',
        'due_date',
        'status'
        );
}

////These following relations didnt workout
/*Model - custserv*/ //uses the pivot table customer_service//
public function staff(){
    return $this->belongsToMany('Staff');
}

/*Model - Staff*/
public function custservs(){
    return $this->belongsToMany('Custserv');
}

/*schema for pivot table 'staff' and 'Custserv' */
Schema::create('customer_service_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_service_id')->unsigned()->index();
        $table->foreign('customer_service_id')->references('id')->on('customer_service')->onDelete('cascade');
        $table->integer('staff_id')->unsigned()->index();
        $table->foreign('staff_id')->references('id')->on('staff')->onDelete('cascade');
        $table->timestamps();
    });

Then I tried ... 然后我尝试了...

$staff = User::find(1)->custservs;
return $staff;

It gave error 它给了错误

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'auditCrm_db.custserv_user' doesn't exist (SQL: select `customer_service`.*, `custserv_user`.`user_id` as `pivot_user_id`, `custserv_user`.`custserv_id` as `pivot_custserv_id` from `customer_service` inner join `custserv_user` on `customer_service`.`id` = `custserv_user`.`custserv_id` where `custserv_user`.`user_id` = 1) 

If my relationshiop is correct how to get and set values between Staff and Custserv? 如果我的关系正确,如何在Staff和Custserv之间获取并设置值?

You may have figured it out, but I think you are doing it overly complicated. 您可能已经弄清楚了,但是我认为您这样做过于复杂。 When using a many-to-many relationship Laravel provides the pivot property. 当使用许多一对多的关系,Laravel提供了pivot财产。 You already have withPivot in your relationship. 您的关系中已经有了withPivot

Now you can access it like that: 现在您可以像这样访问它:

$staff = User::find(1)->services()->first()->pivot; // or you could loop over services
return $staff;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM