2017-01-18 56 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

相关问题