Laravel 8 数据库不会使用 HTML 表单更新
Posted
技术标签:
【中文标题】Laravel 8 数据库不会使用 HTML 表单更新【英文标题】:Laravel 8 database won't update using HTML form 【发布时间】:2021-08-10 00:13:27 【问题描述】:我目前正在为用户创建一个 html 表单,以便在数据库表中插入他们的信息。但是,即使在我创建了控制器、模型和路由之后,数据库也不会更新。
这是我的文件,
控制器文件,updateDatabase.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Application;
class updateDatabase extends Controller
public function update(Request $request)
return $request->input();
$data= Application::find($request->id);
$data->plateno=$request->plateno;
$data->type=$request->type;
$data->colour=$request->colour;
$data->brand=$request->brand;
$data->model=$request->model;
$data->save();
return redirect('application');
模型文件,Application.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Application extends Model
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'plateno',
'type',
'colour',
'brand',
'model',
'id',
];
public $timestamps=false;
查看文件,application.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.min.js"></script>
<link href=" asset('css/user.css') " rel="stylesheet">
<title>Application</title>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<img src=" asset('css/logo.png') ">
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<!-- <li class="active"><a href="#"> Home</a></li>
<li><a href="#">Applications</a></li>
<li><a href="#">Status</a></li> -->
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href=" route('userprofile') ">Profile</a>
</li>
<form method="POST" action=" route('logout') ">
@csrf
<li>
<x-responsive-nav-link :href="route('logout')" style="color:white"
onclick="event.preventDefault();
this.closest('form').submit();">
<span class="glyphicon glyphicon-log-out"> __('Logout') </span>
</x-responsive-nav-link>
</li>
</form>
</ul>
</div>
</div>
</nav>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-2 sidenav">
<nav id="sidebar">
<ul class="list-unstyled components" >
<li >
<a href=" route('dashboard') ">Home</a>
</li>
<li class="active">
<a href="#" data-toggle="collapse" aria-expanded="false">Application</a>
</li>
<li>
<a href=" route('status') ">Status</a>
</li>
</ul>
</nav>
</div>
<div class="col-sm-8 text-left">
<h4>Particulars of Applicant</h4>
<p>Please check your details </p>
<div class="container">
<div class="form-group">
<div class="col-md-6">
<label for="name">Name:</label>
<div class="font-medium text-base text-gray-800"> Auth::user()->name </div>
<br>
</div>
<div class="col-md-6">
<label for="email">Email:</label>
<div class="font-medium text-sm text-gray-500"> Auth::user()->email </div>
<br>
</div>
<div class="col-md-6">
<label for="typeUser">Student / Staff</label>
<div class="font-medium text-sm text-gray-500"> Auth::user()->type </div>
<br>
</div>
<div class="col-md-6">
<label for="Phone">Phone Number:</label>
<div class="font-medium text-sm text-gray-500"> Auth::user()->phone </div>
<br>
</div>
</div>
</div>
<hr>
<div class="container-left">
<form method="POST" action="/application">
@csrf
<div class="form-group col-md-6">
<h4>Particulars of Vehicle 1</h4>
<br>
<label for="plateno">Vehicle Registration Number:</label>
<input id="plateno" class="form-control form-control-sm" type="text" name="name" placeholder="Vehicle Registration Number" :value="old('plateno')" required autofocus>
<br>
<label for="colour">Colour(Vehicle):</label>
<input id="colour" class="form-control form-control-sm" type="text" name="colour" placeholder="Vehicle colour" :value="old('colour')" required autofocus>
<br>
<label for="type">Type of vehicle</label>
<input id="type" class="form-control form-control-sm" type="text" name="type" placeholder="Motorcycle / Car" :value="old('type')" required autofocus>
<br>
<label for="brand">Brand of vehicle</label>
<input id="brand" class="form-control form-control-sm" type="text" name="brand" placeholder="Brand, Eg : Honda" :value="old('brand')" required autofocus>
<br>
<label for="model">Model of vehicle</label>
<input id="model" class="form-control form-control-sm" type="text" name="model" placeholder="Model, Eg : EX5" :value="old('model')" required autofocus>
</div>
<p>*Please recheck your vehicle details</p>
<hr>
<div class="needs-validation">
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback">
You must agree before submitting.
</div>
</div>
<button type="submit" class="btn btn-default" style="background-color: #003D7B; color:white">Submit</button>
</div>
</div>
</form>
</div>
<hr>
</div>
</div>
</div>
<footer class="container-fluid text-center">
<p>Copyright 2020 | Developed By IIUMVeRS</p>
</footer>
</body>
</html>
路由文件,web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\updateDatabase;
/*
|--------------------------------------------------------------------------
| 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 route for both
Route::group(['middleware' => ['auth']], function()
Route::get('/dashboard', 'App\Http\Controllers\DashboardController@index')->name('dashboard');
);
// for users
Route::group(['middleware' => ['auth', 'role:user']], function()
Route::get('/dashboard/myprofile', 'App\Http\Controllers\DashboardController@myprofile')->name('dashboard.myprofile');
);
//User's Application
Route::get('/application', function ()
return view('application');
)->middleware(['auth', 'role:user'])->name('application');
Route::post('/application', [updateDatabase::class, 'update']);
//User's Status
Route::get('/status', function ()
return view('status');
)->middleware(['auth', 'role:user'])->name('status');
//User's Profile
Route::get('/userprofile', function ()
return view('userprofile');
)->middleware(['auth', 'role:user'])->name('userprofile');
//Admin's Profile
Route::get('/adminprofile', function ()
return view('adminprofile');
)->middleware(['auth', 'role:admin'])->name('adminprofile');
//Admin Application Record
Route::get('/records', function ()
return view('records');
)->middleware(['auth', 'role:admin'])->name('records');
//Admin Application Approval
Route::get('/approval', function ()
return view('approval');
)->middleware(['auth', 'role:admin'])->name('approval');
require __DIR__.'/auth.php';
【问题讨论】:
【参考方案1】:在您执行任何属性更新之前,我立即看到您的 updateDatabase 控制器有一个 return $request->input();
。
【讨论】:
【参考方案2】:为什么你一开始是return $request->input()
在update function
你的代码将停止并且不会更新任何东西,因为你在函数的第一个使用return 所以它只会运行第一行我的意思是return $request->input()
;行,删除它然后重试
【讨论】:
以上是关于Laravel 8 数据库不会使用 HTML 表单更新的主要内容,如果未能解决你的问题,请参考以下文章
如何通过应用程序中的表单为用户创建个人资料?我正在使用 laravel 8