Laravel BelongstoMany 关系保存额外的列与关系中的两个模型
Laravel BelongtoMany relation save extra column with the two model in relation
我尝试构建表单来创建发票然后生成它 pdf
我的数据库结构
表:
-- 发票 包含 日期 | invoice_num | client_id
--文章包含个名字|说明 |价格
-- article_invoice 包含 invoice_id | article_id | qte*(数量)*
使用 BelongsToMany 关系保存 article_id & invoice_id
我的发票控制器
public function store(Request $request){
$qte = $request['qte'];
$request = $request['fields'];
$facture = Facture::create(['date'=>'2018-08-22', 'facture_num'=>'4','client_id'=>'6']);
$facture->article()->sync($request);
$facture->article()->update($qte);
}
我的观点
<div class="controls">
{!! Form::open(array('route' =>'factures.store' ,'method'=>'post','enctype'=>'multipart/form-data')) !!}
<div class="entry input-group col-md-6">
<select class="form-control" name="fields[]" type="text" placeholder="Type something">
@foreach($articles as $article)
<option value="{{$article->id}}">{{ $article->designation}}</option>
@endforeach
</select>
<input type="number" name="qte[]">
<span class="input-group-btn">
<button class="btn btn-success btn-add" type="button">
<span class="glyphicon glyphicon-plus"></span>
</button>
</span>
</div>
<br>
</div>
</div>
<button type="submit" class="btn btn-primary btn-sm">
<i class="fa fa-dot-circle-o"></i> Submit
</button>
{!! Form::close() !!}
**同步后 article_invoice 我想保存 qte 有什么帮助吗? **
您可以传递额外的列,例如 -> $facture->article()->sync([$request,$extra_columns]);
解法:
控制器:
$data = Input::get('qte'); // get input qte ( extra column )
$fields = Input::get('fields'); // get input ( Ids Of articles )
$syncData = array();
for ($i=0;$i<count($fields);$i++){
$syncData[$fields[$i]] = array('qte' => $data[$i]);
}
$facture->article()->sync($syncData);
$syncData 必须是这样的:
array:3 [▼
7 => array:1 [▼ // " 7 " is article Id
"qte" => "4" ] //qte is the extra column.
8 => array:1 [▼
"qte" => "10" ]
10 => array:1 [▼
"qte" => "250" ] ]
我尝试构建表单来创建发票然后生成它 pdf
我的数据库结构
表:
-- 发票 包含 日期 | invoice_num | client_id
--文章包含个名字|说明 |价格
-- article_invoice 包含 invoice_id | article_id | qte*(数量)*
使用 BelongsToMany 关系保存 article_id & invoice_id
我的发票控制器
public function store(Request $request){
$qte = $request['qte'];
$request = $request['fields'];
$facture = Facture::create(['date'=>'2018-08-22', 'facture_num'=>'4','client_id'=>'6']);
$facture->article()->sync($request);
$facture->article()->update($qte);
}
我的观点
<div class="controls">
{!! Form::open(array('route' =>'factures.store' ,'method'=>'post','enctype'=>'multipart/form-data')) !!}
<div class="entry input-group col-md-6">
<select class="form-control" name="fields[]" type="text" placeholder="Type something">
@foreach($articles as $article)
<option value="{{$article->id}}">{{ $article->designation}}</option>
@endforeach
</select>
<input type="number" name="qte[]">
<span class="input-group-btn">
<button class="btn btn-success btn-add" type="button">
<span class="glyphicon glyphicon-plus"></span>
</button>
</span>
</div>
<br>
</div>
</div>
<button type="submit" class="btn btn-primary btn-sm">
<i class="fa fa-dot-circle-o"></i> Submit
</button>
{!! Form::close() !!}
**同步后 article_invoice 我想保存 qte 有什么帮助吗? **
您可以传递额外的列,例如 -> $facture->article()->sync([$request,$extra_columns]);
解法:
控制器:
$data = Input::get('qte'); // get input qte ( extra column )
$fields = Input::get('fields'); // get input ( Ids Of articles )
$syncData = array();
for ($i=0;$i<count($fields);$i++){
$syncData[$fields[$i]] = array('qte' => $data[$i]);
}
$facture->article()->sync($syncData);
$syncData 必须是这样的:
array:3 [▼ 7 => array:1 [▼ // " 7 " is article Id "qte" => "4" ] //qte is the extra column. 8 => array:1 [▼ "qte" => "10" ] 10 => array:1 [▼ "qte" => "250" ] ]