【问题标题】:Error in save() after an function in Laravel在 Laravel 中的函数后 save() 出错
【发布时间】:2020-07-24 00:39:50
【问题描述】:

我正在尝试在self::restrictionMinimunBid($ request, $ karateka, $ bid) 函数之后实现save() 方法,但出现以下错误:"Method App\\Http\\Controllers\\BidController::save does not exist."

这是我的代码:

$bid = Bid::where('id_participants', '=', $request->id_participant)
        ->where('id_karatekas', '=',$market->id_karatekas)
        ->where('bid', '=',$request->bid);

        if(!$bid->count()){
         $bid->id_market = $market->id;
         $bid->id_group = $market->id_group;
         $bid->id_karatekas = $market->id_karatekas;                        
         $bid->id_participants = $request->id_participant;                                    
         self::restrictionMinimunBid($request, $karateka, $bid);
         $bid->save();                                    
         $response = array('code' => 200, 'Bid' => $bid, 'msg' => 'Bid created'); 
         }else{
         $response = array('code' => 400, 'error_msg' => "Bid already registered.");
         }
 public function restrictionMinimunBid(Request & $request, $karateka, & $bid)
    {
        $allKaratekas = Karateka::all()
        ->map(function ($allKaratekas) use ($karateka, $request, $bid){
                if($karateka->id == $allKaratekas->id ){
                   if($request->bid > $allKaratekas->value){
                    $bid->bid = $request->bid;
                    $msg ="The bid is more than the value of karateka";
                    var_dump($msg);

                   }
                   else{
                       $error ="The bid is less than the value ofmkarateka";
                       var_dump($error);
                   }
                }
        });
    }

【问题讨论】:

  • 执行var_dump($bid) 并检查它实际包含的内容。看起来它应该是一个集合而不是单个模型。如果您想在没有找到的情况下添加新的出价,请尝试在 if 语句中创建一个新对象:$bid = new Bid(),而不是尝试向空集合添加值。
  • 是的,我在此代码之前使用$bid = new Bid()。还有var_dump($bid),我收到了收藏。
  • 我已经修复了代码,需要更改变量$ bid,我在其中执行``` where () ```。
  • restrictionMinimunBid 上面的所有代码都在方法中吗?你能提供完整的控制器代码吗?

标签: php sql laravel laravel-5 eloquent


【解决方案1】:

你可以替换这个:

$bid = Bid::where('id_participants', '=', $request->id_participant)
    ->where('id_karatekas', '=',$market->id_karatekas)
    ->where('bid', '=',$request->bid);

到这里:

$bid = Bid::where('id_participants', '=', $request->id_participant)
    ->where('id_karatekas', '=',$market->id_karatekas)
    ->where('bid', '=',$request->bid)->first();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-22
    • 2016-03-30
    • 2016-06-29
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2022-08-19
    • 2020-03-01
    相关资源
    最近更新 更多