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 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译