重载页面时,Laravel重复输入

时间:2017-09-07 09:37:47

标签: php laravel

我有一个模型,一个控制器和一个用于将条目引入数据库的表单。问题是刷新页面会导致数据库中的重复条目。如何解析它?

loan.controller:

public function store(Request $request)
   {   $id = Auth::id();
           $loan=New loan;
            $loan->cod_user=$id;
            $loan->nume =$request->name;
            $loan->data=$request->date;
            $loan->durata=$request->period;
            $loan->valoare_rata_luna=$request->month;
            $loan->valoare_totala=$request->amount;
            $loan->save();

            return view('loans');
        }

来自贷款视图的表格:

<div class="container">
            <div class="row">
                  <div class="col-md-4">
                     <div class="blacktext"><h3>My Loans:</h3></div>
                  </div>
                  <div class="col-md-4"></div>
                  <div class="col-md-2"></div>
                  <div class="col-md-2"><button type="button" class="btn btn-success" data-toggle="modal" data-target="#modal1">
                  <span class="glyphicon glyphicon-plus-sign"></span> Add Loan</button></div>

               <div class="modal fade " id="modal1" role="dialog">
                    <div class="modal-dialog modal-lg">
                        <div class="modal-content">

                            <div class="modal-header">
                                <button class="close" data-dismiss="modal">×</button>
                                        <h3>Add Loan</h3>
                            </div>
                            <div class="modal-body text-right">

                               <form action="{{ route('loan.store') }}" method="POST">    
                                    <p class="al-left">
                                    {{csrf_field()}}
                                    <label for="date">Date:</label>
                                    <input type = "date" name="date" placeholder="Date" id="date">
                                    </p>

                                    ...

                                    <p class="al-left">
                                    <label for="amount">Amount:</label>
                                    <input type = "number" name="amount" placeholder="0" id="amount">
                                    </p>

                                    <input type="submit" class="btn btn-sm btn-primary" name="submit" value="Add">

4 个答案:

答案 0 :(得分:1)

只需重定向到路线(提供视图的路线),而不是商店方法末尾的视图:

public function store(Request $request)
   {   $id = Auth::id();
           $loan=New loan;
            $loan->cod_user=$id;
            $loan->nume =$request->name;
            $loan->data=$request->date;
            $loan->durata=$request->period;
            $loan->valoare_rata_luna=$request->month;
            $loan->valoare_totala=$request->amount;
            $loan->save();

            return redirect(route(('loans')); //asumming that you named a route as loans
        }

答案 1 :(得分:0)

刷新时仍然存在POST ..您可以做的是将页面与商店功能分开..就像这样......

<强> ROUTE

Route::get('/loans', 'LoanController@viewLoanForm');
Route::post('/loans', 'LoanController@store');

<强> LoanController

public function viewLoanForm()
{
    return view('loan');
}

public function store(Request $request)
{
    // process your input
    return redirect('/loans');
}

答案 2 :(得分:0)

我认为您应该更改In [551]: (df.set_index('Date').groupby('ID').resample('Y').size().unstack(fill_value=0) .rename(columns=lambda x: x.year)) Out[551]: Date 2014 2015 2016 2017 ID a 0 0 2 1 b 2 0 0 1 方法,例如:

return

答案 3 :(得分:0)

尝试使用return redirect() - back()。它肯定会起作用。