在Controller.php第107行中的Laravel 5.2 BadMethodCallException:方法[save]不存在
I can't figure out why am I getting this error.
Controller: 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!');
}
}
Model: SectionHeader
<?php
namespace SimpleCms;
use Illuminate\Database\Eloquent\Model;
class SectionHeader extends Model {
protected $table = 'sectionheader';
protected $fillable = [
'title',
'description',
'heading',
'image'
];
}
Routes:
Route::post('/home/store', 'SectionHeaderController@store' );
I don't know what is wrong nor how to fix this.
This error appears once I hit form submit which points to this SectionHeaderController@store
Any idea?
Thank you.
EDIT: I changed per suggestions and I get new error
FatalErrorException in SectionHeaderController.php line 34: Class 'App\SectionHeader' not found
我无法弄清楚为什么会出现此错误。 p>
Controller:SectionHeaderController p>
&lt;?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扩展Controller {
公共函数存储(Request $ request){
$ header = new SectionHeader();
$ this-&gt; validate($ request,[
'title'=&gt;'required',
'image'=&gt;'required | mimes:jpeg,png | max:1024 | 尺寸:max_width = 300,max_height = 100',
'标题'=&gt;'required',
'description'=&gt;'required'
]);
$ header-&gt; title = $ request - &gt; title;
$ header-&gt; heading = $ request-&gt; description;
$ header-&gt; description = $ request-&gt; description;
if($ request-&gt; hasFile( 'image')){
$ file = Input :: file('image');
$ timestamp = str_replace(['',':'],' - ',Carbon :: now() - &gt; toDateTimeString());
\ n $ name = $ timestamp。 ' - '。$ file-&gt; getClientOriginalName();
$ header-&gt; filePath = $ name;
$ file-&gt; move(public_path()。'/ images /',$ name);
}
$ header-&gt; save();
返回$ this-&gt; create() - &gt; with('success','Done!');
}
}
code> pre>
Model:SectionHeader p>
&lt;?php
namespace SimpleCms;
use Illuminate \ Database \ Eloquent \ Model;
class SectionHeader扩展Model {
protected $ table ='sectionheader';
protected $ fillable = [
'title',
'description',
'heading',
'image'
];
}
code> pre>
路线: p>
Route :: post('/ home / store ','SectionHeaderController @ store');
code> pre>
我不知道有什么问题,也不知道如何解决这个问题。
一旦我点击表单提交,就会出现此错误 指向此 SectionHeaderController @ store code>
任何想法? p>
谢谢。 p>
编辑:
我根据建议更改了我收到新错误 p>
SectionHeaderController.php第34行中的FatalErrorException:未找到Class
'App \ SectionHeader p>
blockquote>
div>
your code has very problem ...
i suggest use artisan commands for generate models and controller in future ...
in your model, namespace is not App\SectionHeader
because of that you get this exception : get Class 'App\SectionHeader' not found
change your model namespace to App\SectionHeader
in your controller, you create controller instead model :
$header = new SectionHeaderController():
instead
$header = new SectionHeaders();
and finally in end of store action, i don't know why you do this :
return $this->create()->with('success', 'Done!');
you should redirect to some route and set flash message or render a view with success message ...
Can you change
$header = new SectionHeaderController():
To
$header = new SectionHeaders();