使用Laravel添加数据透视表记录和相关的子记录

时间:2014-07-31 02:25:45

标签: php laravel orm eloquent

我需要帮助在Laravel中添加数据透视记录和相关的子记录。如果可能的话,我想用ORM来做。这里的概念是潜在的捐赠者参加筹款活动和捐款,捐款将由一个或多个名称组成。我的表就是这样(简化):

donor:
  id
  name

fund_raiser:
  id
  date

donation:
  id
  donor_id
  fund_raiser_id
  monetary_type

designation:
  id
  donation_id
  category
  amount

我可以手动插入数据透视记录,如下所示:

// assume both donor and rundraiser records already exist
$donor = Donor::find($donorID);
$fundraiser = FundRaiser::find($fundraiserID);

// create the pivot record
$donation = Donation::create(['donor_id' => $donor->id, 'fundraiser_id' => $fundraiser->id, 'date' => 'somedate']);

// now add a designation
$designation = new Designation(['Category' => 'General', 'Amount' => '100']);
$donation->designations()->save($designation);

或者我可以用更多ORM方式创建数据透视记录:

$fundraiser->donors()->save($donor)

但现在我不知道如何插入名称,因为我不知道刚刚添加的数据透视记录的ID。

2 个答案:

答案 0 :(得分:0)

关系定义需要withPivot才能获得数据透视id,然后您可以在关系的上下文中访问数据透视模型,即id

// Donor model
public function fundRaisers()
{
  return $this->belongsToMany('FundRaiser', 'donation')->withPivot('monetary_type', 'id');
}

FundRaiser模型上该关系的另一部分也是如此。

然后你可以这样做:

// let's grab single donor
$donor = Donor::first();

// then load fund raisers and get one of them
$fundRaiser = $donor->fundRaisers->find($someId);

// now we're in the context of the relation, so we can access pivot model
$donationId = $fundRaiser->pivot->id;

// use it to save designation
$designation = new Designation([ SOME VALUES]);
$designation->donation_id = $donationId;
$designation->save();

现在,为了访问数据透视表上的相关Designation模型,您需要使用Pivot关系自定义数据透视模型(扩展hasMany):

use Illuminate\Database\Eloquent\Relations\Pivot;

class DonationPivot extends Pivot {

  protected $table = 'donations';

  public function designations()
  {
    return $this->hasMany('Designation');
  }
}

现在你可以使用它了:

$fundRaiser->pivot->designations;

请注意,您无法致电

$designation->donation;

使用该设置,因为Pivot需要在构造时传递依赖关系,但是您可以这样做:

$donor = Donor::join('donations', 'donors.id', '=', 'donations.donor_id')->where('donations.id', '=', $designation->donation_id')->first();

要获得pivot id,您可以在当前连接的PDO上使用lastInsertId:

$fundraiser->donors()->save($donor);
$fundraiser->getConnection()->getPdo()->lastInsertId();

答案 1 :(得分:0)

我发现我可以手动插入数据透视记录,如下所示:

// assume both donor and rundraiser records already exist
$donor = Donor::find($donorID);
$fundraiser = FundRaiser::find($fundraiserID);

// create the pivot record, plugging in the necessary id values manually
$donation = Donation::create(['donor_id' => $donor->id, 'fundraiser_id' => $fundraiser->id, 'date' => 'somedate']);

// now add a designation
$designation = new Designation(['Category' => 'General', 'Amount' => '100']);
$donation->designations()->save($designation);

还有另一种方法来创建我所知道的数据透视记录,它更像是ORM样式:

// assume both donor and rundraiser records already exist
$donor = Donor::find($donorID);
$fundraiser = FundRaiser::find($fundraiserID);

// create the pivot record
$fundraiser->donors()->save($donor)

但是这个方法的问题是我没有获得数据透视记录,所以我无法添加相关记录(指定)。任何能告诉我如何完成第二种方法的人都会获得勾选标记。