Controller.php第103行中的BadMethodCallException:方法[save]不存在

时间:2016-02-29 21:25:53

标签: php laravel laravel-5.2

遇到问题我无法在网络上找到任何地方。错误通知我save()方法没有退出?无法弄清楚问题所在。发送帖子表单以在控制器中创建/保存新任务时会发生此问题。

跟踪堆栈和错误方法如下 - >

BadMethodCallException in Controller.php line 103: Method [save] does not exist.

    in Controller.php line 103
    at Controller->__call('save', array()) in Task.php line 56
    at Task->saveTask(object(Request))
    at call_user_func_array(array(object(Task), 'saveTask'), array(object(Request))) in Controller.php line 76
    at Controller->callAction('saveTask', array(object(Request))) in ControllerDispatcher.php line 146
    at ControllerDispatcher->call(object(Task), object(Route), 'saveTask') in ControllerDispatcher.php line 94
    at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
    at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
    at ControllerDispatcher->callWithinStack(object(Task), object(Route), object(Request), 'saveTask') in ControllerDispatcher.php line 54
    at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\Task', 'saveTask') in Route.php line 174
    at Route->runController(object(Request)) in Route.php line 140
    at Route->run(object(Request)) in Router.php line 724
    at Router->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
    at VerifyCsrfToken->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
    at ShareErrorsFromSession->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
    at StartSession->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
    at EncryptCookies->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
    at Pipeline->then(object(Closure)) in Router.php line 726
    at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
    at Router->dispatchToRoute(object(Request)) in Router.php line 675
    at Router->dispatch(object(Request)) in Kernel.php line 246
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
    at CheckForMaintenanceMode->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
    at Pipeline->then(object(Closure)) in Kernel.php line 132
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
    at Kernel->handle(object(Request)) in index.php line 53
    at require_once('D:\wamp64\organizer\public\index.php') in server.php line 21

Task.php控制器saveTask方法 - >

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Session as Ticket;
use Session;

class Task extends Controller
{
    //

    public function activateTask(\App\Task $task)
    {
        if($task->active == 1)
        {
            return redirect('/');
        }
        else
        {
            $task->active = 1;
            $task->save();
            if(Session('active') != null)
            {
                $oldtask = Ticket::find(Session('active'));
                $oldtask->Task->active = 0;
                $oldtask->Task->save();
                $oldtask->touch();
                $oldtask->save();
            }
            $session = new Ticket;
            $session->task()->associate($task);
            $session->save();
            Session(['active' => $session->id]);
            return redirect('/');
        }
    }

    public function saveTask(Request $request) {

        $validator = Validator::make($request->all(), [
            'name' => 'required|max:255',
        ]);

        if($validator->fails()) {
            return redirect('/')->withInput()->withErrors($validator);
        }

        $task = new Task;
        $task->name = $request->input('name');
        $task->deadline = $request->input('deadline');
        $task->save();
        return redirect('/');
    }

    public function deactivateTask(\App\Task $task)
    {
        $task->active = 0;
        $task->save();
        $session = Ticket::find(Session('active'));
        $session->touch();

        $started = strtotime($session->created_at);
        $ended = strtotime($session->updated_at);
        $hours = abs(($ended - $started))/(60*60);
        $session->hours = $hours;

        $session->save();
        Session(['active' => null]);
        return redirect('/');

    }

}

路线 - &gt;

<?php

use App\Task;
use App\Session;

use Illuminate\Http\Request;

Route::group(['middleware' => ['web']], function () {
    //

    Route::get('/', function() {
        $task = Task::orderBy('created_at', 'desc')->get();

        return view('layouts/tasks', [
            'tasks' => $task
        ]);

    });


    Route::post('/task', 'Task@saveTask');



    Route::post('/task/{task}/activate', 'Task@activateTask');



    Route::post('/task/{task}/deactivate', 'Task@deactivateTask');


    Route::delete('/task/{task}', function (Task $task){
        if($session = Session::find(Session('active')))
        {
            if($session->task->id == $task->id)
            {
                Session(['active', null]);
            }
        }

        $task->delete();
        return redirect('/');
    });

});

希望得到一些帮助。

2 个答案:

答案 0 :(得分:2)

您有两个名为Task的类,这是可以的,因为它们位于不同的名称空间中。

首先,您拥有Task模型,即\App\Task。其次,您拥有Task控制器,即\App\Http\Controllers\Task

在您的任务控制器中,在saveTask方法中,您有以下一行:

$task = new Task;

因为您没有指定完全限定的类名,所以它将在当前命名空间中查找要使用的类。由于您位于\App\Http\Controllers命名空间,因此它正在创建一个新的\App\Http\Controllers\Task实例。

您需要将行更改为:

$task = new \App\Task;

答案 1 :(得分:0)

您遇到的问题是您将控制器命名为“任务”并且您正在执行

 $task = new Task;

在这种情况下,代码指的是控制器任务,但不是模型任务。所以它在Task控制器类中搜索save方法而不是模型。你可以把它改成

$task = new \App\Task;