2015-10-16 112 views
1

我弄不明白。我認爲它碰到我的activateAccount控制器功能時會發生。在那個函數裏我調用了account_activated函數,在這個函數中用戶被保存。另外,我得到了用戶對象的第一個實例,所以我沒有發生什麼錯誤。調用未定義的方法Illuminate Database Query Builder :: save() - Laravel 5

這是控制器功能

public function activateAccount(Request $request, User $user, $key) { 
    $registered = $request->session()->get('registered'); 
    if (isset($registered)) { 
     $request->session()->forget('registered'); 
     if ($user->account_activated($key)) { 
      // TODO 
      $referral = Referrals::where('activation_key', $key); 
      if (!empty($referral)) { 
       $referral->validate = 1; 
       $referral->save(); 
      } 


      return redirect('/login')->with('success', "Your account has been activated. You many login!"); 
     } else { 
      return redirect('/not-active'); 
     } 
    } else { 
     return redirect('/'); 
    } 
} 

account_activated function inside the User model 
    public function account_activated($key){ 
     $user = User::where('activate_key', $key)->first(); 
     $user->activate = 1; 
     $user->save(); 
     return true; 
    } 

這是錯誤味精

BadMethodCallException in Builder.php line 2047: 
Call to undefined method Illuminate\Database\Query\Builder::save() 
in Builder.php line 2047 
at Builder->__call('save', array()) 
at Builder->save() 
at call_user_func_array(array(object(Builder), 'save'), array()) in Builder.php line 943 
at Builder->__call('save', array()) in RegisterController.php line 155 
at Builder->save() in RegisterController.php line 155 
at RegisterController->activateAccount(object(Request), object(User), 'OjkTKaLIVAaRhveYRZz9rC1uAuPjUc6fk4BeZhUM') 
at call_user_func_array(array(object(RegisterController), 'activateAccount'), array(object(Request), object(User), 'key' => 'OjkTKaLIVAaRhveYRZz9rC1uAuPjUc6fk4BeZhUM')) in Controller.php line 256 
at Controller->callAction('activateAccount', array(object(Request), object(User), 'key' => 'OjkTKaLIVAaRhveYRZz9rC1uAuPjUc6fk4BeZhUM')) in ControllerDispatcher.php line 164 
at ControllerDispatcher->call(object(RegisterController), object(Route), 'activateAccount') in ControllerDispatcher.php line 112 
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114 
at ControllerDispatcher->callWithinStack(object(RegisterController), object(Route), object(Request), 'activateAccount') in ControllerDispatcher.php line 69 
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\RegisterController', 'activateAccount') in Route.php line 201 
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134 
at Route->run(object(Request)) in Router.php line 704 
at Router->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 
at Pipeline->then(object(Closure)) in Router.php line 706 
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671 
at Router->dispatchToRoute(object(Request)) in Router.php line 631 
at Router->dispatch(object(Request)) in Kernel.php line 236 
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50 
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)) 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)) 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)) 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)) 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)) in CheckForMaintenanceMode.php line 42 
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 103 
at Pipeline->then(object(Closure)) in Kernel.php line 122 
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87 
at Kernel->handle(object(Request)) in index.php line 54 

回答

2

,你得到是不是從你調用函數來的錯誤,但activateAccount函數內本身。

特別是這裏這些行:

$referral = Referrals::where('activation_key', $key); 
      if (!empty($referral)) { 
       $referral->validate = 1; 
       $referral->save(); 
      } 

你需要給你的where子句後取模型。通過它的外觀你已經知道如何做到這一點,但出於完整性的考慮,你只需要在->first()補充:

$referral = Referrals::where('activation_key', $key)->first(); 
       if (!empty($referral)) { 
        $referral->validate = 1; 
        $referral->save(); 
       } 
+1

只是對添加到'介紹人::其中()'返回'查詢\ Builder'對象,因此異常告訴你'save()'在'Query \ Builder'對象中不存在。 'Referrals :: where() - > first()'返回一個Eloquent模型,然後您可以調用'save()'on – andrewtweber

相關問題