Laravel 8 數據庫不會使用 HTML 表單更新 (Laravel 8 database won't update using HTML form)


問題描述

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 Ismailthomas jAbdlrahman Saber)

參考文件

  1. Laravel 8 database won't update using HTML form (CC BY‑SA 2.5/3.0/4.0)

#laravel-8 #Laravel #Database #PHP #html






相關問題

在 Laravel 的表中計算 user_id (Count user_id in a table in Laravel)

如何通過模型顯示表中的數據,自定義行除外 (How to show data from table via Model except a custom row)

如何在 Laravel 的模型中為所有貨幣字段(十進制屬性)加上逗號 (How to put comma to all money fields (decimal attributes) in a model in Laravel)

Laravel:8.x 目標類 [ArticlesController] 不存在 (Laravel:8.x Target class [ArticlesController] does not exist)

如何根據 laravel 中的一列獲取最新記錄? (How to get latest record based on one column in laravel?)

訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)

更改徽標默認通知電子郵件 laravel 8 (change logo default notification email laravel 8)

Laravel 8 - 如果用戶有權調用路由,請簽入控制器 (Laravel 8 - Check in Controller if user has permission to call route)

laravel 微風 Multi Auth - 具有兩個不同註冊的 Admin Guard (laravel breeze Multi Auth - Admin Guard with two diffirent registration)

找不到組件的類或視圖 (Unable to locate a class or view for component)

Laravel 8 數據庫不會使用 HTML 表單更新 (Laravel 8 database won't update using HTML form)

Laravel問題從電子郵件驗證傳遞錯誤消息 (Laravel problem to pass error message from email verification)







留言討論