2017-01-18 54 views
0

我不明白爲什麼我會得到這個錯誤。Laravel 5.2 BadMethodCallException在Controller.php行107:方法[保存]不存在

控制器:SectionHeaderController

<?php 

namespace SimpleCms\Http\Controllers; 

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Input; 
use Carbon\Carbon; 
use App\Http\Requests; 
use App\SectionHeader; 


class SectionHeaderController extends Controller { 

    public function store(Request $request) { 
     $header = new SectionHeader(); 
     $this->validate($request, [ 
      'title' => 'required', 
      'image' => 'required|mimes:jpeg,png|max:1024|dimensions:max_width=300,max_height=100', 
      'heading' => 'required', 
      'description' => 'required' 
     ]); 
     $header->title = $request->title; 
     $header->heading = $request->description; 
     $header->description = $request->description; 

     if($request->hasFile('image')) { 
      $file = Input::file('image'); 
      $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString()); 

      $name = $timestamp. '-' .$file->getClientOriginalName(); 
      $header->filePath = $name; 
      $file->move(public_path().'/images/', $name); 
     } 
     $header->save(); 
     return $this->create()->with('success', 'Done!'); 
    } 
} 

型號:SectionHeader

<?php 

namespace SimpleCms; 

use Illuminate\Database\Eloquent\Model; 

class SectionHeader extends Model { 

    protected $table = 'sectionheader'; 
    protected $fillable = [ 
     'title', 
     'description', 
     'heading', 
     'image' 
    ]; 
} 

路線:

Route::post('/home/store', '[email protected]'); 

我不知道什麼是錯的,也沒有如何解決這個問題。 這個錯誤出現後,我點擊表格提交指向這[email protected] 任何想法?

謝謝。

編輯: 我每建議改變了,我得到新的錯誤

FatalErrorException在SectionHeaderController.php線34:類 '應用\ SectionHeader' 未找到

回答

0

你的代碼具有非常問題...

我建議生成模型並在今後的控制器使用工匠的命令......

模型中的

,命名空間不App\SectionHeader因爲您得到此異常:get Class 'App\SectionHeader' not found

你的模型命名空間更改爲App\SectionHeader

在你的控制器,你CREA TE控制器代替型號:

$header = new SectionHeaderController(): 

代替

$header = new SectionHeaders(); 

終於在商店的行動結束,我不知道你爲什麼這樣做:

return $this->create()->with('success', 'Done!'); 

你應該重定向到一些路由並設置Flash消息或呈現成功消息的視圖...

+0

我明白你在說什麼,我會改變最後一部分不用擔心,但我可以找出爲什麼我得到這個錯誤 – Morpheus

+0

爲什麼你改變你的問題????????????????? ?? –

+0

將您的模型名稱空間更改爲「App \ SectionHeader」...您的名稱空間爲:SimpleCms ... @Morpheus –

1

你可以改變

$ header = new SectionHeaderController():

$頭=新SectionHeaders();

+1

真棒,但我現在得到類'App \ SectionHeader'沒有找到 – Morpheus

+0

我編輯答案應該是SectionHeaders(); – Afsal

+0

對不起,我看到它,但問題是,當我在等待答案時,我刪除了所有內容,並將它命名爲'controller:SectionHeaderController',模型'SectionHeader',我在更改所寫內容後得到該錯誤...因此,基本上它是一樣的.... – Morpheus

相關問題