php 如何将表单数据传递到控制器,然后存储到数据库?laravel

tuwxkamq  于 2022-12-17  发布在  PHP
关注(0)|答案(2)|浏览(162)

嗨,我其实是新的laravel,学习,我已经被分配了一项任务,我一直在工作了很多,但我真的卡住了,我需要一些帮助,这是管理 Jmeter 板中,我试图创建一个表单,提交图片中显示的表单的值,从视图到路由和控制器,其中控制器使用创建函数将所有这些值添加到数据库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');

这是家庭控制器. 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']),
        ]);
    }
}

请告诉我哪里是我犯错误,因为我尝试了很多事情,但它从来没有工作:(总是给错误

u3r8eeie

u3r8eeie1#

尝试使用公共函数提交表单数据&使用Request而不是数组

public function create(Request $request)
    {
        return User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);
    }
wyyhbhjk

wyyhbhjk2#

  1. Laravel自己处理通过GET或POST方法传递的参数。
    *.您必须在控制器中使用Illuminate/Http/Request,与index函数中使用的完全相同。
    如果只是显示视图,则不需要在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 Request Documentation

相关问题