dongliao6777 2017-09-18 18:00
浏览 202
已采纳

Laravel 5.4 - 插入重复行

I have been using Laravel for a couple years and I'm stumped on this one. Using Laravel 5.4 with voyager. I have my own controller outside of the BREAD controller

Form:

<form method="POST" action="/admin/invites" accept-charset="UTF-8" class="form-edit-add">
<input name="_token" type="hidden" value="QgLgj5tG4RfD2CxCsqE2Qn5jcWfwQhsk5THT30vO">
<div class="panel-body">
<div class="form-group">
<label for="name">Business</label>
<input class="form-control" placeholder="Business Name" name="business_id" type="text">
</div>
<div class="form-group">
<label for="body">Referral Name</label>
<input class="form-control" placeholder="Referral Name" name="referral_name" type="text">
</div>
</div>
<input class="btn btn-primary width-100 mb-xs" type="submit" value="Save">
</form>

web routes:

Route::resource('/admin/invites',  'InviteController');

Controller:

public function store(Requests\InviteRequest $request)
{
    DB::table('invites')->insert(
        [
            'user_id' => Auth::user()->id, 
            'business_id' => $request->business_id,
            'referral_name' => $request->referral_name,
            'url_token' => str_random(16)
        ]
    );
    return redirect('/admin/invites')->with([
                    'message'    => "Successfully Added New",
                    'alert-type' => 'success',
                ]);
}

When I submit it creates 2 rows in the database. I don't have duplicate routes or controllers. My Request file is empty. I have an ID in the table that is auto increment with primary index.

Any thoughts or troubleshooting tips?

  • 写回答

2条回答 默认 最新

  • dongxunhua2054 2017-09-19 03:42
    关注

    I think the main point you are missing here is that Voyager calls its store() twice. First, through AJAX to validate the fields and then again by normal form submit to store the BREAD to the database.

    Take a look at the default store() implementation in Voyager:

    public function store(Request $request)
    {
            $slug = $this->getSlug($request);
    
            $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
    
            // Check permission
            Voyager::canOrFail('add_'.$dataType->name);
    
            //Validate fields with ajax
            $val = $this->validateBread($request->all(), $dataType->addRows);
    
            if ($val->fails()) {
                return response()->json(['errors' => $val->messages()]);
            }
    
            if (!$request->ajax()) {
                $data = $this->insertUpdateData($request, $slug, $dataType->addRows, new $dataType->model_name());
    
                return redirect()
                    ->route("voyager.{$dataType->slug}.edit", ['id' => $data->id])
                    ->with([
                            'message'    => "Successfully Added New {$dataType->display_name_singular}",
                            'alert-type' => 'success',
                        ]);
            }
    }
    

    Notice the if (!$request->ajax()) condition, the first AJAX call would bypass that but the second call would get into it and store to the database.

    So in short, you have to follow the same structure in your store() method. Perform your validations first. Then when it's time to save, put that code into the if (!$request->ajax()) condition.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名