如何在拉拉维尔存储表单数据?

路线/网络.php


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

Route::post('/index','UserController@store');

控制器:用户控制器.php


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Users;


class UserController extends Controller

{

    public function index()

    {

        return view('index');

    }


    public function store(Request $request)

    {

        $user = new Users();

        $user->fname = $request->input('fname');

        $user->lname = $request->input('lname');

        $user->email = $request->input('email');

        $user->password = $request->input('password');

        $user->save();

        return redirect()->route('index')->with('info','User Added Successfully');

    }

}

查看:索引刀片.php


@extends('layout.main')

@section('title','Index Page')

@section('content')

    <div class="container">

        <div class="row">

            <form id="myform" action="{{route('store')}}">

                @csrf

                <div class="form-group">

                    <label for="fname">{{ trans('core.fname') }}</label>

                    <input type="text" class="form-control" id="fname" name="fname"/>

                </div>

                <div class="form-group">

                    <label for="fname">{{ trans('core.lname') }}</label>

                    <input type="text" class="form-control" id="lname" name="lname"/>

                </div>

                <div class="form-group">

                    <label for="fname">{{ trans('core.email') }}</label>

                    <input type="text" class="form-control" id="email" name="email"/>

弑天下
浏览 89回答 3
3回答

繁华开满天机

&nbsp;public function store(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this->validate($request,[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'fname' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lname' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'password' => 'required'&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; $user = new Users();&nbsp; &nbsp; &nbsp; &nbsp; $user->fname = $request->fname;&nbsp; &nbsp; &nbsp; &nbsp; $user->lname = $request->lname;&nbsp; &nbsp; &nbsp; &nbsp; $user->email = $request->email;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->password = Hash::make($request->password);&nbsp; &nbsp; &nbsp; &nbsp; $user->save();&nbsp; &nbsp; &nbsp; &nbsp; Toastr::success('User Successfully Saved','Success');&nbsp; &nbsp; &nbsp; &nbsp; return redirect()->route('admin.user.index');}

小唯快跑啊

试试这个Route::post('/index','UserController@store')->name('index.store');和刀片文件route('index.store')见&nbsp;https://laravel.com/docs/5.8/routing#named-routes

慕沐林林

您的web.phpRoute::post('/index','UserController@store')->name('store');添加缺少的表单method="POST"@extends('layout.main')@section('title','Index Page')@section('content')&nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <form id="myform" action="{{route('store')}}" method="post">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @csrf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="fname">{{ trans('core.fname') }}</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" id="fname" name="fname"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="fname">{{ trans('core.lname') }}</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" id="lname" name="lname"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="fname">{{ trans('core.email') }}</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="form-control" id="email" name="email"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="fname">{{ trans('core.password') }}</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="password" class="form-control" id="password" name="password"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" class="btn btn-primary" id="submit" name="submit" value="{{trans('core.submit')}}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>@endsection
打开App,查看更多内容
随时随地看视频慕课网APP