問題描述
Laravel 8 數據庫不會使用 HTML 表單更新 (Laravel 8 database won't update using HTML form)
我目前正在為用戶創建一個 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:
Right off the bat I see that your updateDatabase controller has a return $request‑>input();
before you perform any attribute updates.
方法 2:
why you're return $request‑>input()
at first in the update function
your code will stop and will not update anything because you're use return at first of the function so it'll just run first line I mean return $request‑>input()
; line, remove it then try again
(by Zakwan Ismail、thomas j、Abdlrahman Saber)