[英]Error when updating database after deploying to server. Laravel 5
I have one form that adds data to two different tables (Articles & Deals). 我有一种将数据添加到两个不同表(文章和交易)的表格。 An Article has many deals.
文章有很多交易。 A deal has one Article.
交易只有一条。 There are multiple deals with different dealnames that the user inputs on the create and edit form.
用户在创建和编辑表单上输入了多个具有不同交易名称的交易。 I can update the 'deals' part of my database fine in my local/vagrant dev environment but I get a ' Creating default object from empty value ' error when I try on my live site.
我可以在本地/无用的开发环境中很好地更新数据库的“交易”部分, 但是在我的实时网站上尝试时,出现“ 从空值创建默认对象 ”错误。 It says the problem is in the
update
function of my Articles Controller
. 它说问题出在我的
Articles Controller
的update
功能中。
I'm using Laravel 5. 我正在使用Laravel 5。
The Articles Table has: id(primary)
, title
, image
, description
, address
. 文章表具有:
id(primary)
, title
, image
, description
, address
。
The Deals table has: id(primary)
, dealname
, article_id (index)
, dayID
. Deals表具有:
id(primary)
, dealname
, article_id (index)
, dayID
。
The only difference I can see between my dev and live environment is that the index (article_id) on the 'Deals' table doesn't have a key icon next to it in PHPMyAdmin. 我能看到的开发环境和实时环境之间的唯一区别是,“ Deals”表上的索引(article_id)在PHPMyAdmin中旁边没有键图标。 But the foreign key r/ship is set correctly.
但是外键r / ship设置正确。
You can see all the code here: https://github.com/lakemck/gethappy 您可以在这里查看所有代码: https : //github.com/lakemck/gethappy
Articles Controller- Update 文章控制器-更新
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++) {
//Error is supposedly on the next line.
$article->deals->where('dayID',($i + 1))->first()->dealname = $request->input('dealname')[$i];
$article->deals->where('dayID',($i + 1))->first()->save();
}
return redirect('/');
}
Form 形成
{!! 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() !!}
Article Model 文章模型
class Article extends Model
{
public function deals()
{
return $this->hasMany('App\Deal');
}
protected $fillable = array('title', 'photo', 'description', 'address');
}
DEAL MODEL 交易模型
class Deal extends Model
{
public function article()
{
return $this->belongsTo('App\Article')->withTimestamps();
}
protected $fillable = array('dealname', 'article_id', 'dayID');
}
In the end I had to change the code in my ArticlesController update function to this: 最后,我不得不将我的ArticlesController更新函数中的代码更改为此:
for($i = 0; $i < sizeof($request->input('dealname')); $i++) {
$deal = $article->deals()->where('dayID', $i + 1)->first();
$deal->dealname = $request->input('dealname')[$i];
$deal->save();
}
return redirect('/');
}
Note the brackets on the deals(). 请注意Deals()上的括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.