2016-03-06 26 views
0

我有一種形式將數據添加到兩個不同的表(文章&交易)。一篇文章有​​很多交易。一筆交易有一篇文章。用戶在創建和編輯表單上輸入的交易名稱有多種交易。我可以在我的本地/流動開發環境中更新我的數據庫的「交易」部分,但是當我嘗試在我的現場網站上時,我收到''從空值錯誤'創建默認對象'錯誤。 它說這個問題出在我的Articles Controllerupdate函數中。部署到服務器後更新數據庫時出錯。 Laravel 5

我使用Laravel 5.

在文章表有:id(primary)titleimagedescriptionaddress

The Deals表有:id(primary),dealname,article_id (index),dayID

我可以在我的開發環境和實時環境之間看到的唯一區別是,「Deals」表上的索引(article_id)在PHPMyAdmin中沒有旁邊的關鍵圖標。但是外鍵r/ship設置正確。

這裏你可以看到所有代碼:https://github.com/lakemck/gethappy

文章控制器 - 更新

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

DEAL模型

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

回答

0

最後,我在我的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('/'); 
    } 

注意對交易的括號()。

相關問題