【问题标题】:How to update table with multiple entries. Laravel 5如何更新具有多个条目的表。拉拉维尔 5
【发布时间】:2016-06-17 15:07:33
【问题描述】:

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

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

我正在使用 Laravel 5。

文章表有:id、title、image、description、address。 Deals 表有:id、dealname、article_id、dayID。

Articles Controller - 更新

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

    }

文章模型

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

【问题讨论】:

  • 以前没见过fill(),好像也没有记录。此外,要将输入作为数组获取,您可以使用$request-&gt;all();$request-&gt;input() 是否包含任何数据? . $article-&gt;update($request-&gt;all()) 不行吗?
  • 谢谢。在我添加交易内容之前,fill() 用于更新文章。文章和交易是不同的表,所以我认为我需要以不同的方式更新它们?下面是一个 fill()stackoverflow.com/questions/26034176/… 的例子
  • update() 基本上是fill()-&gt;save() github.com/laravel/framework/blob/5.2/src/Illuminate/Database/… 的别名
  • 这与问题没有太大关系,但您不应该在 belongsTo 关系上使用-&gt;withTimestamps() 方法,它是用于belongsToMany 关系...但是,我可以不太了解这个问题:/
  • 谢谢@El_Matella 我已经解决了。我也澄清了这个问题。

标签: php laravel-5 one-to-many


【解决方案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 个 dayId 吗?

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

【讨论】:

  • 哇。这完全有效!我有一周中的所有日子(7dayids)。我认为有一种方法可以通过 foreach 做到这一点吗?
  • 知道为什么当我部署到服务器时这不起作用吗?我收到“从空值创建默认对象”错误。 stackoverflow.com/questions/35833903/…
猜你喜欢
  • 2017-08-07
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 2017-03-12
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多