【问题标题】:How can I pass form data to my controller and then store it to database ? laravel如何将表单数据传递给我的控制器,然后将其存储到数据库中?拉拉维尔
【发布时间】:2021-02-08 15:51:01
【问题描述】:

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

请告诉我我在哪里犯了错误,因为我尝试了很多东西,但都没有成功:(总是出错

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

【问题讨论】:

  • 你遇到了什么错误?
  • 我认为你需要在函数中传递 Request 而不是 array
  • @sta 请再次检查我也发布了错误,所以很抱歉之前忘记了。
  • @RobinSingh 如果你能告诉我怎么做,我将不胜感激?
  • @UmairAli 而不是 protected function create(array $data) 尝试 protected function create(Request $data) 或者如果不起作用也尝试公开您的功能

标签: php mysql laravel


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    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-&gt;nameOfParameter 一起使用 它的名称是您通过 HTML 表单传递的名称。 希望有用。

    文档:Laravel 8 Request Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2021-06-10
      • 2020-06-19
      • 2017-01-10
      • 2018-07-07
      • 2017-02-06
      相关资源
      最近更新 更多