2016-03-04 80 views
0

已更新。我有一種形式將數據添加到兩個不同的表(文章&交易)。一篇文章有​​很多交易。一筆交易有一篇文章。有多個的交易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'); 
} 
+0

以前沒有看過'fill()',它似乎沒有被記錄。另外,要以數組的形式獲取輸入,可以使用'$ request-> all();'。 '$ request-> input()'是否包含任何數據? 。 '$ article-> update($ request-> all())'不起作用嗎? – Jeemusu

+0

謝謝。 fill()在添加交易內容之前用於更新文章。文章和交易是不同的表,所以我想我需要不同地更新它們?這裏有一個fill()的例子http://stackoverflow.com/questions/26034176/call-toa-a-member-function-fill-on-a-non-object –

+0

'update()'基本上是' fill() - > save()'https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model。php#L1416 – Jeemusu

回答

1

我真的不知道要充分了解你的問題,但將這樣的事情可能是你的情況非常有用:

public function update(ArticleRequest $request, $id) { 
    $article = Article::findOrFail($id); 
    if($request->hasFile('image')){ 
     // photo saving stuff. 
    } 
    $article->update($request->all()); 
    $article->deals->where('dayID',1)->first()->dealname = $request->input('dealname')[0]; 
    $article->deals->where('dayID',1)->first()->save(); 
    $article->deals->where('dayID',2)->first()->dealname = $request->input('dealname')[1]; 
    $article->deals->where('dayID',2)->first()->save(); 
    $article->deals->where('dayID',3)->first()->dealname = $request->input('dealname')[2]; 
    $article->deals->where('dayID',3)->first()->save(); 
} 

他們只是你在表單中使用的那3天嗎?

編輯: 您也可以嘗試使用for循環。這是未經測試的代碼,所以你可能想要優化它:)

public function update(ArticleRequest $request, $id) { 
    $article = Article::findOrFail($id); 
    if($request->hasFile('image')){ 
     // photo saving stuff. 
    } 
    $article->update($request->all()); 
    for($i = 0; $i < sizeof($request->input('dealname')); $i++) { 
     $article->deals->where('dayID',($i + 1))->first()->dealname = $request->input('dealname')[$i]; 
     $article->deals->where('dayID',($i + 1))->first()->save(); 
    } 
} 
+0

哇。完全有效!我有本週的所有日子(7dayids)。我認爲有一種方法可以用foreach來做到這一點嗎? –

+0

嗯,是的,當然,讓我編輯我的答案 – Hammerbot

+0

這很好用 –