繁体   English   中英

如何将表单数据传递给我的 controller,然后将其存储到数据库中? laravel

[英]How can I pass form data to my controller and then store it to database ? laravel

嗨,我实际上是 laravel 的新手,并且正在学习,我被分配了一项任务,我一直在工作很多,但我真的被困住了,我需要一些帮助,这是我正在尝试创建表单的管理仪表板which submit the values of the form shown in the picture, from the view to the route and controller where controller uses a create function to add all those values in the database mysql

在此处输入图像描述

主页视图的代码:

 @extends('layouts.app')

@section('content')

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Dashboard') }}</div>

                <div class="card-body">

                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                    {{ __('You are logged in') }} {{$user}} ..!
                </div>

                <div class="card-body">
                    <div class="card-body">
                        <form method="POST" action="{{route('home.create')}}">
                            @csrf

                            <div class="form-group row">
                                <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                                <div class="col-md-6">
                                    <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

                                    @error('name')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                                <div class="col-md-6">
                                    <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

                                    @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                                <div class="col-md-6">
                                    <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

                                    @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

                                <div class="col-md-6">
                                    <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                                </div>
                            </div>

                            <div class="form-group row mb-0">
                                <div class="col-md-6 offset-md-4">
                                    <button type="submit" class="btn btn-primary">
                                        {{ __('Adduser') }}
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>


                </div>

            </div>
        </div>
    </div>
</div>
@endsection

web.php 中我的路线的代码在这里:

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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!
|
*/

Route::get('/', function () {



    return view('welcome');
});

Auth::routes();

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

route::get('/admin/user/roles',['middleware'=>['role','auth','web'],function(){

    return "Middleware role";
}]);


route::get('/home','AdminController@index');
//route::post('/home/{name}/email/password/confirmpassword','HomeController@create');
Route::post('home', 'HomeController@create')->name('home.create');

这是 HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {



         return view('home');


    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

请告诉我我在哪里犯错因为我尝试了很多东西但它从来没有奏效:(总是出错

这是我在运行它时遇到的错误:

在此处输入图像描述

在此处输入图像描述

尝试公共 function 提交表单数据并使用请求而不是数组

public function create(Request $request)
    {
        return User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);
    }
  1. Laravel 自己处理通过 GET 或 POST 方法传递的参数。
    *。 您必须在 controller 中使用Illuminate/Http/Request ,就像index function 中使用的一样。
    如果您只是显示视图,则不需要在index中使用Request $request
    相反,您应该在create中使用它。
    并使用它:

 public function create(Request $request) { $data = $request->validate([ // validation rules ]); $user = User::create($data); //.. anything you want }

请注意,您仍然可以将每个参数与$request->nameOfParameter使用。它的名称是您通过 HTML 表单传递的名称。 希望有用。

文档: Laravel 8 请求文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM