已更新。我有一種形式將數據添加到兩個不同的表(文章&交易)。一篇文章有很多交易。一筆交易有一篇文章。有多個的交易與dealname不同,用戶在創建和編輯表單上輸入。我可以創建一個有很多交易的文章,我可以使用Deals表中的數據填充編輯表格,但當我使用文章控制器更新我的'交易'表時,它只更新每個'交易名'與最後一個dealname是輸入的。我只需要更新'dealname'列,因爲所有其他列將保持不變。如果我刪除表單的dealname/deals部分,我可以更新。如何用多個條目更新表格。 Laravel 5
如何正確更新交易表?我知道我必須改變我的文章控制器的更新功能。
我使用Laravel 5.
在文章表有:ID,標題,圖片,說明,地址。 Deals表格包含:id,dealname,article_id,dayID。
文章控制器 - 更新
public function update(ArticleRequest $request, $id)
{
$article = Article::find($id);
if($request->hasFile('image')){
// photo saving stuff.
}
$article->fill($request->input())->save();
//Get IDs of deals to be updated.
$dealID = Deal::all()->lists('dealname', 'id');
$dealID = $dealID->toArray();
$dealID = array_keys($dealID);
$deals = $request->input('dealname');
foreach($deals as $deal) {
Deal::whereIn('id', $dealID)->update(['dealname' => $deal]);
}
return redirect('/');
}
表
{!! Form::model($article, ['route' => ['articleUpdate_path', $article->id], 'files' => true, 'method' => 'PATCH']) !!}
{!! Form::label('title','TITLE') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
{!! $errors->first('title','<p class="error">:message</p>')!!}
{!! Form::label('image','PHOTO') !!}
{!! Form::file('image', null, ['class' => 'form-control']) !!}
{!! Form::label('description','DESCRIPTION') !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
@foreach ($article->deals as $deal)
@if($deal->dayID == '1')
{!! Form::label('dealname','Monday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '1']) !!}
@endif
@if($deal->dayID == '2')
{!! Form::label('dealname','Tuesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '2']) !!}
@endif
@if($deal->dayID == '3')
{!! Form::label('dealname','Wednesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '3']) !!}
@endif
@endforeach
{!! Form::label('address','ADDRESS') !!}
{!! Form::text('address', null, ['class' => 'form-control']) !!}
{!! Form::close() !!}
文章控制器-store
public function store(ArticleRequest $request)
{
$image_name = $request->file('image')->getClientOriginalName();
$request->file('image')->move(base_path().'/public/images', $image_name);
$article = ($request->except(['image']));
$article['image'] = $image_name;
$article = Article::create($article);
// GET INPUT
$deals = $request->input('dealname');
// GET ID OF ARTICLE
$articleID = $article->id;
// N is the day id that increments
$n = 1;
foreach($deals as $deal) {
Deal::create(['dealname' => $deal, 'article_id' => $articleID, 'dayID' => $n++]);
}
return redirect()->route('articles_path');
}
Article模型
class Article extends Model
{
public function deals()
{
return $this->hasMany('App\Deal');
}
protected $fillable = array('title', 'photo', 'description', 'address');
}
DEAL模型
class Deal extends Model
{
public function article()
{
return $this->belongsTo('App\Article')->withTimestamps();
}
protected $fillable = array('dealname', 'article_id', 'dayID');
}
以前沒有看過'fill()',它似乎沒有被記錄。另外,要以數組的形式獲取輸入,可以使用'$ request-> all();'。 '$ request-> input()'是否包含任何數據? 。 '$ article-> update($ request-> all())'不起作用嗎? – Jeemusu
謝謝。 fill()在添加交易內容之前用於更新文章。文章和交易是不同的表,所以我想我需要不同地更新它們?這裏有一個fill()的例子http://stackoverflow.com/questions/26034176/call-toa-a-member-function-fill-on-a-non-object –
'update()'基本上是' fill() - > save()'https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model。php#L1416 – Jeemusu