将数据表单存储到数据库laravel中的问题

时间:2019-03-11 08:47:56

标签: javascript php html5 laravel-5.2

我正在尝试将一些数据存储到数据库中。我有NewMember表单,此表单将有数据存储在3个表的用户,餐厅和地址中。它显示错误MethodNotAllowedHttpException in RouteCollection.php line 218:。如果我使用默认值的插入查询,它将成功插入。
这是NewMember表单

@extends('layouts.app')
@section('content')
<style>
   body {
   background-color: #fff0e6;
   }
</style>
<div class="container">
   <div class="row">
      <div class="col-md-8 col-md-offset-2">
         <div class="panel panel-default">
            <div class="panel-heading">
               <h1><u><font color="990000" ;>Register new restaurant</font></u></h1>
            </div>
            <div class="panel-body">
               <form class="form-horizontal" role="form" method="POST" action="/restaurantmenus/">
                  <input type="hidden" name="_token" value="{{ csrf_token() }}">
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Restaurant name</label>
                     <div class="col-md-6">
                        <input id="name" type="text" class="form-control" name="name"
                           value="{{ old('name') }}">
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="address" class="col-md-4 control-label">Address</label>
                     <div class="col-md-6">
                        <label for="name" class="col-md-4 control-label">City</label>
                        <select name="city" style="width: 150px" id="city" class="form-control">
                           <option value="Damascus">Damascus</option>
                        </select>
                        <br>
                        <label for="name" class="col-md-4 control-label">Region</label>
                        <select name="region" style="width: 150px" id="region" class="form-control">
                           <option value="111">111</option>
                           <option value="222">222</option>
                        </select>
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Telephone</label>
                     <div class="col-md-6">
                        <input id="Telephone" type="text" class="form-control" name="Telephone"
                           value="{{ old('Telephone') }}">
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">About your restaurant</label>
                     <div class="col-md-6">
                        <textarea id="description" rows="4" cols="50" name="description"
                           value="{{ old('description') }}"
                           placeholder="Enter text here..." class="form-control"></textarea>
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Email</label>
                     <div class="col-md-6">
                        <input id="email" type="email" class="form-control" name="email"
                           value="{{ old('email') }}">
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Username</label>
                     <div class="col-md-6">
                        <input id="username" type="text" class="form-control" name="username"
                           value="{{ old('username') }}">
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Password</label>
                     <div class="col-md-6">
                        <input id="password" type="password" class="form-control" name="password"
                           value="{{ old('password') }}">
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Confirm password</label>
                     <div class="col-md-6">
                        <input id="password2" type="password" class="form-control" name="password2"
                           value="{{ old('password2') }}">
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Kitchen</label>
                     <div class="col-md-6">
                        <select name="kitchen" id="kitchen" class="form-control">
                           <option value="111">111</option>
                           <option value="222">222</option>
                        </select>
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Working time</label>
                     <div class="col-md-6">
                        <label for="name" class="col-md-4 control-label">Open</label>
                        <input id="open" style="width: 150px" type="time" class="form-control" name="open"
                           value="{{ old('open') }}">
                        <br>
                        <label for="name" class="col-md-4 control-label">Close</label>
                        <input id="close" style="width: 150px" type="time" class="form-control" name="close"
                           value="{{ old('close') }}">
                     </div>
                  </div>
                  <div class="form-group">
                     <label for="name" class="col-md-4 control-label">Delivery</label>
                     <label for="name">Yes</label>
                     <input id="delivery" type="checkbox" style="float: left;" name="delivery"
                        value="{{ old('delivery') }}">
                     <input id="price" style="width: 150px" style="float: left;" placeholder="Delivery price"
                        type="text" name="price" value="{{ old('price') }}">
                  </div>
                  <div class="col-md-6 col-md-offset-4">
                     <button type="submit" class="btn btn-primary"
                        style="background:#990000; border-color:#990000" ;>
                     <i class="fa fa-btn fa-user"></i> Register
                     </button>
                  </div>
               </form>
            </div>
         </div>
      </div>
   </div>
</div>
<br>
<br>
<footer>
   @extends('layouts.footer')
</footer>
@endsection

这是模特 餐馆模型,它与地址表有一种关系(餐馆有很多地址)

<?php
    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class restaurant extends Model
    {
    //
        protected $fillable=['name','telephone' ,'description','delivery','DeliveryPrice','ownerName','ownerPhone', 'kitchen','open','close'];

        public function addresses(){
            return $this->hasMany('App/address');
        }
    }
?>

用户模型

<?php

    namespace App;

    use Illuminate\Foundation\Auth\User as Authenticatable;

    class User extends Authenticatable
    {
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $table = 'users';//add
        protected $fillable = [
            'name', 'username','email', 'password','phone',
        ];

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];

        public function restaurant(){
            return $this->hasMany('App/restaurant');
        }
    }
}

地址模型

       <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class address extends Model
{
    //
    protected $fillable=[
       'city','region'
    ];
}

控制器

{

namespace App\Http\Controllers;

use App\address;
use App\restaurant;
use App\User;
use Illuminate\Http\Request;
use App\restaurant as restaurantAlias;
use App\Http\Requests;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use phpDocumentor\Reflection\DocBlock\Tags\Uses;

class restaurantController extends Controller
{
    //
    public function index()
    {
        $use=User::all();
        $res=restaurant::all();
        $add=address::all();
        return view('NewMember',compact('use','res','add'));
    }

    public function create()
    {
        // $restaurant=restaurant::all();
        // return view('restaurant.NewMember',compact($restaurant));


        return view('NewMember');
    }


    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show()
    {
        // $restaurant1= restaurant::find($id);

        // return view('restaurantmenus/',$restaurant1);
        return view('restaurantmenus');

    }

    public function store(Request $request)
    {
        // taking the restaurant user information

        $restaurantUser = new User();
        $restaurantUser->email = $request->email;
        $restaurantUser->username = $request->username;
        $restaurantUser->type = 'restaurant';
        $restaurantUser->password = Hash::make($request->password);
        $restaurantUser->save();


        //taking the restaurant information
        $object = new restaurant();

        $object->name = $request->name;
        $object->telephone = $request->Telephone;
        $object->description = $request->description;
        $object->delivery = $request->delivery;
        $object->DeliveryPrice = $request->DeliveryPrice;
        $object->kitchen = $request->kitchen;
        $object->open = $request->open;
        $object->close = $request->close;
        $object->user_id = $restaurantUser->id;
        $object->$restaurantUser->save();
        // dd($object->IDrestaurant);

        // taking the restaurant address
        $addr = new address();
        $addr->city = $request->city;
        $addr->region = $request->region;
        $addr->restaurant_id = $object->id;
        $addr->$object->$restaurantUser->save();


        $idr = $object->IDrestaurant;
        return redirect('/restaurantmenus/' . $idr);
    }


}

}

路由我认为发布路由.csrf_token()中的问题是不允许发布路由将数据发送到数据库。我曾尝试在$except类中添加到VerifyCsrfToken的路由,但没有成功

   Route::post('/NewMember','restaurantController@store');
   Route::get('/NewMember','restaurantController@create');
   Route::get('/restaurantmenus','restaurantController@show');

0 个答案:

没有答案