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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?