laravel发布302表单时的回复

时间:2019-07-09 11:58:27

标签: php laravel

我使用的是laravel 5.7,当我单击提交表单时,我的状态为302,并且未发送到数据库,因此我将返回表单页面 chromer控制台中的错误状态为302。数据未保存在数据库中。试图在web.php中编辑路由,但效果不佳 我已经在web.php中编辑了路线,但无法解决

表格(society.blade.php)

<div class="container" style="width: 930px">
    <form  method="POST" action="{{isset($routes)?$routes:route('societies.store')}}" class="form-horizontal form-box remove-margin ui-formwizard" >
        <h4 class="form-box-header"><i class="fa fa-magic"></i> Society @if(empty($model->title)) Register @else Update @endif</h4>
        {{ csrf_field() }}
        <input type="hidden" name="_method" value="{{isset($method)?$method:'POST'}}"/>
        <div class="form-box-content">
            <div id="advanced-first" class="step ui-formwizard-content" style="display: block;">

                <div class="col-md-12">
                    <div class="col-sm-6 form-group">
                        @if(!empty($model->title)) <label for="title" >Society Name</label> @endif
                        <input type="text" class="form-control {{ $errors->has('title') ? ' is-invalid' : '' }}"
                               name="title" id="title"
                               value="{{old('title',$model->title)}}" placeholder="Society Name" required="required">
                        @if($errors->has('title'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('title') }}</strong>
                            </div>
                        @endif
                    </div>

                </div>
                <div class="col-md-12">
                    <div class="col-sm-6 form-group">
                        @if(!empty($model->total_land)) <label for="total_land" >Total Land</label> @endif
                        <input type="text" class="form-control {{ $errors->has('total_land') ? ' is-invalid' : '' }}"
                               name="total_land" id="total_land"
                               value="{{old('total_land',$model->total_land)}}" placeholder="Total Land" required="required">
                        @if($errors->has('total_land'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('total_land') }}</strong>
                            </div>
                        @endif
                    </div>
                    <div class="col-sm-6 form-group">
                        @if(!empty($model->sellable_area)) <label for="sellable_area" >Saleable Area</label> @endif
                        <input type="text" class="form-control {{ $errors->has('sellable_area') ? ' is-invalid' : '' }}"
                               name="sellable_area" id="sellable_area"
                               value="{{old('sellable_area',$model->sellable_area)}}" placeholder="Saleable Area" required="required">
                        @if($errors->has('sellable_area'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('sellable_area') }}</strong>
                            </div>
                        @endif
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="col-sm-6 form-group">
                        @if(!empty($model->latitude)) <label for="latitude" >Latitude</label> @endif
                        <input type="text" class="form-control {{ $errors->has('latitude') ? ' is-invalid' : '' }}"
                               name="latitude" id="latitude" value="{{old('latitude',$model->latitude)}}" placeholder="Latitude"
                               required="required">
                        @if($errors->has('latitude'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('latitude') }}</strong>
                            </div>
                        @endif
                    </div>
                    <div class="col-sm-6 form-group">
                        @if(!empty($model->longitude)) <label for="longitude" >Longitude</label> @endif
                        <input type="text" class="form-control {{ $errors->has('longitude') ? ' is-invalid' : '' }}"
                               name="longitude" id="longitude" value="{{old('longitude',$model->longitude)}}" placeholder="Longitude"
                               required="required">
                        @if($errors->has('longitude'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('longitude') }}</strong>
                            </div>
                        @endif
                    </div>

                </div>
                <div class="col-md-12">
                    <div class="col-sm-6 form-group">
                        @if(!empty($model->province)) <label for="province" >Province</label> @endif
                        <input type="text" class="form-control {{ $errors->has('province') ? ' is-invalid' : '' }}"
                               name="province" id="province" value="{{old('province',$model->province)}}" placeholder="Province"
                               maxlength="50" required="required">
                        @if($errors->has('province'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('province') }}</strong>
                            </div>
                        @endif
                    </div>
                    <div class="col-sm-6 form-group">
                        @if(!empty($model->city)) <label for="city" >City</label> @endif
                        <input type="text" class="form-control {{ $errors->has('city') ? ' is-invalid' : '' }}" name="city"
                               id="city" value="{{old('city',$model->city)}}" placeholder="City" maxlength="50"
                               required="required">
                        @if($errors->has('city'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('city') }}</strong>
                            </div>
                        @endif
                    </div>

                </div>
                <div class="col-md-12">
                    <div class="  form-group">
                        @if(!empty($model->address)) <label for="address" >Address</label> @endif
                        <input type="text" class="form-control {{ $errors->has('address') ? ' is-invalid' : '' }}"
                               name="address" id="address" value="{{old('address',$model->address)}}" placeholder="Address"
                               maxlength="225" required="required">
                        @if($errors->has('address'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('address') }}</strong>
                            </div>
                        @endif
                    </div>

                </div>
                <div class="col-md-12">
                    <div class=" col-sm-6 form-group">
                        @if(!empty($model->square_fit_price)) <label for="square_fit_price" >Square ft. Price</label> @endif
                        <input type="text" class="form-control {{ $errors->has('square_fit_price') ? ' is-invalid' : '' }}"
                               name="square_fit_price" id="square_fit_price"
                               value="{{old('square_fit_price',$model->square_fit_price)}}" placeholder="Square Fit Price"
                               required="required">
                        @if($errors->has('square_fit_price'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('square_fit_price') }}</strong>
                            </div>
                        @endif
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="col-sm-6 form-group">
                        <div class="input-group">
                            @if(!empty($model->start_date)) <label for="start_date" >Start Date</label> @endif
                            <input type="date" class="form-control calendar {{ $errors->has('start_date') ? ' is-invalid' : '' }}"
                                   name="start_date" id="start_date"
                                   value="{{old('start_date',$model->start_date)}}"
                                   placeholder="Start Date" required="required">
                        </div>
                        @if($errors->has('start_date'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('start_date') }}</strong>
                            </div>
                        @endif
                    </div>
                    <div class="col-sm-6 form-group">
                        <div class="input-group">
                            @if(!empty($model->ending_date)) <label for="ending_date" >Ending Date</label> @endif
                            <input type="date" class="form-control calendar {{ $errors->has('ending_date') ? ' is-invalid' : '' }}"
                                   name="ending_date" id="ending_date"
                                   value="{{old('ending_date',$model->ending_date)}}"
                                   placeholder="Ending Date" required="required">

                        </div>
                        @if($errors->has('ending_date'))
                            <div class="invalid-feedback">
                                <strong>{{ $errors->first('ending_date') }}</strong>
                            </div>
                        @endif
                    </div>
                </div>

                <div class="col-md-12">
                    <div class="col-sm-4 form-check">
                        <input style="margin-left: 5px" class="form-check-input" type="checkbox"
                               value="1" name="role" checked
                               id="admin">
                        <label class="form-check-label" for="role">
                            Status?
                        </label>
                    </div>
                    <div class="col-sm-4 form-check">
                        <input style="margin-left: 5px" class="form-check-input" type="checkbox"
                               value="1" name="status" checked onclick="return false;"
                               id="status">
                        <label class="form-check-label" for="status">
                            delete Status
                        </label>

                    </div>

                </div>
                <div class="form-group form-actions ">
                    <div class="col-md-10 col-md-offset-2">
                        <div class="form-group text-right ">
                            <input type="submit" class="btn btn-primary" value="Submit"/>
                        </div>

                    </div>


                </div>
            </div>
        </div>



    </form>
</div>

SocietyController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Society;
use App\Http\Requests\Societies\Index;
use App\Http\Requests\Societies\Show;
use App\Http\Requests\Societies\Create;
use App\Http\Requests\Societies\Store;
use App\Http\Requests\Societies\Edit;
use App\Http\Requests\Societies\Update;
use App\Http\Requests\Societies\Destroy;


/**
 * Description of SocietyController
 *
 * @author Tuhin Bepari <digitaldreams40@gmail.com>
 */

class SocietyController extends Controller
{
       /**
     * Display a listing of the resource.
     *
     * @param  Index  $request
     * @return \Illuminate\Http\Response
     */
    public function index(Index $request)
    {
        return view('pages.societies.index', ['records' => Society::paginate(10)]);
    }    /**
     * Display the specified resource.
     *
     * @param  Show  $request
     * @param  Society  $society
     * @return \Illuminate\Http\Response
     */
    public function show(Show $request, Society $society)
    {
        return view('pages.societies.show', [
                'record' =>$society,
        ]);

    }    /**
     * Show the form for creating a new resource.
     *
     * @param  Create  $request
     * @return \Illuminate\Http\Response
     */
    public function create(Create $request)
    {
        return view('pages.societies.create', [
            'model' => new Society,

        ]);
    }    /**
     * Store a newly created resource in storage.
     *
     * @param  Store  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Store $request)
    {
        $model=new Society;
        $model->fill($request->all());

        if ($model->save()) {

            session()->flash('app_message', 'Society saved successfully');
            return redirect()->route('societies.index');
            } else {
                session()->flash('app_message', 'Something is wrong while saving Society');
            }
        return redirect()->back();
    } /**
     * Show the form for editing the specified resource.
     *
     * @param  Edit  $request
     * @param  Society  $society
     * @return \Illuminate\Http\Response
     */
    public function edit(Edit $request, Society $society)
    {

        return view('pages.societies.edit', [
            'model' => $society,

            ]);
    }    /**
     * Update a existing resource in storage.
     *
     * @param  Update  $request
     * @param  Society  $society
     * @return \Illuminate\Http\Response
     */
    public function update(Update $request,Society $society)
    {
        $society->fill($request->all());

        if ($society->save()) {

            session()->flash('app_message', 'Society successfully updated');
            return redirect()->route('societies.index');
            } else {
                session()->flash('app_error', 'Something is wrong while updating Society');
            }
        return redirect()->back();
    }    /**
     * Delete a  resource from  storage.
     *
     * @param  Destroy  $request
     * @param  Society  $society
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy(Destroy $request, Society $society)
    {
        if ($society->delete()) {
                session()->flash('app_message', 'Society successfully deleted');
            } else {
                session()->flash('app_error', 'Error occurred while deleting Society');
            }

        return redirect()->back();
    }
}

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Auth::routes();

Route::group(['middleware' => 'web'], function () {

    Route::group(['middleware' => ['auth']], function () {

        Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');

        Route::get('/home', 'HomeController@index')->name('home');

        Route::get('welcome', 'WelcomeController@index');

        Route::get('/', 'WelcomeController@index');

        Route::resource('buyers','BuyerController');

        Route::resource('users','UserController');

        Route::resource('dealers','DealerController');

        Route::resource('documents','DocumentController');

        Route::resource('payments','PaymentController');

        Route::resource('purchaseorders','PurchaseOrderController');

        Route::resource('sellers','SellerController');

        Route::resource('societies','SocietyController');

    });

});

我想将数据提交到数据库

1 个答案:

答案 0 :(得分:0)

那是因为您这样做

$society->fill($request->all()); if ($society->save())

尝试做出

$society->fill($request->all()); $society->save(); if ($society)

,然后其余的就可以了。