2016-06-20 65 views
0

這就是我想要沒有任何成功的事情:Laravel 5,無法通過請求方法插入不存在

在welcome.blade我有一些板和所產生的子卡(隨機一個foreach用戶)在哪裏你可以點擊子板和去這樣/ subboardOne。我得到這個在我的routes.php文件

Route::get('/{subboaName}', '[email protected]'); 
Route::post('/{subboaName}', '[email protected]'); 

,那麼你可以通過形式寄出對這段扣板線程,但因爲我真的不知道怎麼laravel知道他在哪裏,形式是這樣的:

<form class="form col-md-12 center-block" role="form" method="POST" action="/{{$subboardcoll->id}}"> 

這個$ subboardcoll-> ID來自控制器,它通過指數函數發送收集:

public function index($subboard) 
{ 
    $subboardcoll = Subboard::where('subboaName', $subboard)->first(); 
    $threads = Thread::where('subboaId', $subboardcoll->id) 
      ->orderBy('created_at', 'desc') 
      ->get(); 
    return view('threads.thread', compact('threads', 'subboardcoll')); 
} 

然後我試圖把我的形式和存儲線程autoinserti納克subboardId但laravel不承認扣板方法:

public function store(Request $request) 
    { 
     $this->validate($request, [ 
      'comentario' => 'required|max:2000', 
      //'g-recaptcha-response' => 'required|recaptcha', 
      //'imagen' => 'required', 
     ]); 

     $request->subboards()->threads()->create([ 
      'thrName' => $request->nombre, 
      'thrComment' => $request->comentario, 
      'thrImg' => $request->imagen, 
      'thrSubject' => $request->tema, 
     ]); 

     return redirect()->back(); 

    } 

,給我這個erorr:

BadMethodCallException in Macroable.php line 81: Method subboards does not exist. 

你們helpme能知道爲什麼嗎?還有更好的形式來做我想要做的?上laravel IM新手,感謝

編輯:

Thread.php

public function subboard() 
{ 
    return $this->belongsTo(Subboard::class, 'subboaId'); 
} 

Subboard.php

public function thread() 
{ 
    return $this->hasMany(Thread::class); 
} 

回答

0

方法扣板不請求對象存在。考慮這樣做

public function store($id, Request $request) 
{ 
    $this->validate($request, [ 
     'comentario' => 'required|max:2000', 
     //'g-recaptcha-response' => 'required|recaptcha', 
     //'imagen' => 'required', 
    ]); 

    Subboard::find($id)->threads()->create([ 
     'thrName' => $request->nombre, 
     'thrComment' => $request->comentario, 
     'thrImg' => $request->imagen, 
     'thrSubject' => $request->tema, 
    ]); 

    //Alternative query statement 
    Subboard::where('id', $id)->first()->threads()->create([..... 

    return redirect()->back(); 

} 
+0

這樣做,傳遞一個ID,通過形式,但在執行時,它給我的錯誤:傳遞給照亮\數據庫\語法:: columnize( 參數1)必須是類型的數組,字符串在第121行調用E:\ laragon \ www \ DeepElite \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Query \ Grammars \ Grammar.php並定義爲 – Sociopath

+0

。使用find時,只傳遞$ id。我編輯了我的答案以反映正確的用法。 –

+0

看起來像threads()這是一個未定義的方法,不知道爲什麼,因爲在我的模型上它都很好,編輯我的問題 – Sociopath