如何使用多个条目更新 table。 Laravel 5
How to update table with multiple entries. Laravel 5
已更新。我有一种表格可以将数据添加到两个不同的 tables(文章和交易)。一篇文章有很多交易。一个交易有一个文章。有多个 交易,用户在创建和编辑表单中输入了不同的 交易名称。我可以很好地创建包含许多交易的文章,并且可以在更新 'deals' [时使用交易 table、but 中的数据填充编辑表单=57=] 使用 Articles Controller,它只是用输入的最后一个交易名称更新每个 'dealname'。我只需要更新 'dealname' 列,因为所有其他列都将保持不变。如果我删除表单的 dealname/deals 部分,我可以正常更新。
如何正确更新交易 table?我知道我必须更改我的文章控制器的更新功能。
我正在使用 Laravel 5。
文章 Table 有:id、标题、图片、描述、地址。交易 table 具有: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');
}
文章模型
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 个 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();
}
}
已更新。我有一种表格可以将数据添加到两个不同的 tables(文章和交易)。一篇文章有很多交易。一个交易有一个文章。有多个 交易,用户在创建和编辑表单中输入了不同的 交易名称。我可以很好地创建包含许多交易的文章,并且可以在更新 'deals' [时使用交易 table、but 中的数据填充编辑表单=57=] 使用 Articles Controller,它只是用输入的最后一个交易名称更新每个 'dealname'。我只需要更新 'dealname' 列,因为所有其他列都将保持不变。如果我删除表单的 dealname/deals 部分,我可以正常更新。
如何正确更新交易 table?我知道我必须更改我的文章控制器的更新功能。
我正在使用 Laravel 5。
文章 Table 有:id、标题、图片、描述、地址。交易 table 具有: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');
}
文章模型
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 个 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();
}
}