2016-02-29 49 views
0

有問題我無法在網上找到任何地方。錯誤通知我save()方法不存在?不能弄明白這個問題。發送郵件表單以在控制器中創建/保存新任務時會發生此問題。Controller.php中的BadMethodCallException行103:方法[保存]不存在

跟蹤堆棧和錯誤的方法如下 - >

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('/'); 

    } 

} 

路線 - >

<?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', '[email protected]'); 



    Route::post('/task/{task}/activate', '[email protected]'); 



    Route::post('/task/{task}/deactivate', '[email protected]'); 


    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('/'); 
    }); 

}); 

很想得到一些幫助。

+0

您可以發佈任務模型? – Laerte

回答

0

你所面對的問題是,你命名你的控制器任務和你正在做的

$task = new Task; 

在這種情況下,代碼是指控制器的任務,但不是模型的任務。所以它在Task控制器類中搜索save方法,而不是模型。你可以把它改成

$task = new \App\Task; 
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; 
相關問題