部署到服务器后更新数据库时出错. Laravel 5 [英] Error when updating database after deploying to server. Laravel 5

查看:85
本文介绍了部署到服务器后更新数据库时出错. Laravel 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种将数据添加到两个不同表中的表格(文章和交易).一篇文章有​​很多交易.一笔交易只有一条.用户在创建和编辑表单上输入了多个具有不同交易名称的交易.在本地/无用的开发环境中,我可以很好地更新数据库的交易"部分,但是在我的实时站点上尝试时,出现"从空值创建默认对象"错误. 它说问题出在我的Articles Controllerupdate函数中.

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.

我正在使用Laravel 5.

I'm using Laravel 5.

文章表具有:id(primary)titleimagedescriptionaddress.

The Articles Table has: id(primary), title, image, description, address.

交易"表具有:id(primary)dealnamearticle_id (index)dayID.

The Deals table has: id(primary), dealname, article_id (index), dayID.

在开发环境和实时环境之间,我可以看到的唯一区别是,"Deals"表上的索引(article_id)在PHPMyAdmin中旁边没有键图标.但是外键r/ship设置正确.

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. But the foreign key r/ship is set correctly.

您可以在此处查看所有代码: https://github.com/lakemck/gethappy

You can see all the code here: https://github.com/lakemck/gethappy

Articles Controller-更新

 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::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() !!}

商品模型

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

推荐答案

最后,我不得不将ArticlesController更新函数中的代码更改为此:

In the end I had to change the code in my ArticlesController update function to this:

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

请注意Deals()上的括号.

Note the brackets on the deals().

这篇关于部署到服务器后更新数据库时出错. Laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆