douwo1517 2017-01-18 21:56
浏览 61
已采纳

在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

  • 写回答

2条回答 默认 最新

  • doumi1311 2017-01-18 22:31
    关注

    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 ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退