如何更新包含多个条目的表.拉拉维尔 5.


How to update table with multiple entries. Laravel 5

>更新。我有一个表单将数据添加到两个不同的表(文章和交易)。一篇文章有很多交易。交易有一条。用户在创建和编辑窗体上输入的多个具有不同交易名称的交易。我可以很好地创建一篇包含许多交易的文章,并且我可以使用交易表中的数据填充编辑表单,但是当我使用文章控制器更新我的"交易"表时,它只会使用输入的最后一个交易名称更新每个"交易名称"。 我只需要更新"dealname"列,因为所有其他列将保持不变。如果我删除表单的交易名称/交易部分,我可以很好地更新。

如何正确更新交易表?我知道我必须在文章控制器的更新功能中更改某些内容。

我正在使用Laravel 5。

文章表有:ID,标题,图像,描述,地址。交易表有: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() !!}

文章控制器 - 商店

    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');
    }

文章模型

class Article extends Model
{
    public function deals()
    {
        return $this->hasMany('App'Deal');
    }  
protected $fillable = array('title', 'photo', 'description', 'address'); 
}

交易模式

class Deal extends Model
{
    public function article()
    {
        return $this->belongsTo('App'Article')->withTimestamps();
    }
    protected $fillable = array('dealname', 'article_id', 'dayID'); 
}

我真的不确定是否完全理解您的问题,但是这样的事情在您的情况下会有用吗:

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 天 ID 吗?

编辑:您也可以尝试使用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();
    }
}