Laravel 8错误尝试在null上分配属性“plateno”
Posted
技术标签:
【中文标题】Laravel 8错误尝试在null上分配属性“plateno”【英文标题】:Laravel 8 Error Attempt to assign property "plateno" on null 【发布时间】:2021-08-10 02:13:38 【问题描述】:我目前正在构建一个页面,以使用 html 表单将行插入到数据库表中。我已经创建了模型、控制器、路由和 Web 视图文件。但是,它返回了一个错误
Error
Attempt to assign property "plateno" on null
这是我的文件:
资源\视图\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\UpdateDatabaseController;
/*
|--------------------------------------------------------------------------
| 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', [UpdateDatabaseController::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';
app\Http\Controllers\UpdateDatabaseController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Providers\RouteServiceProvider;
use App\Models\Vehicle;
class UpdateDatabaseController extends Controller
public function update(Request $request)
$data= Vehicle::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(RouteServiceProvider::HOME);
app\Models\Vehicle.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'plateno',
'type',
'colour',
'brand',
'model',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
public $timestamps=false;
【问题讨论】:
$data= Vehicle::find($request->id);
数据变量为空,你可能没有正确提交id
出现这种情况是因为查询时没有找到记录,所以$data变量为空
但错误不在该行,它在 UpdateDatabaseController.php,第 18 行:$data->plateno=$request->plateno;
【参考方案1】:
在您的application.blade.php
文件中,您忘记更改palteno 输入标签 上的name 属性。所以替换这一行:
<input id="plateno" class="form-control form-control-sm" type="text" name="name" placeholder="Vehicle Registration Number" :value="old('plateno')" required autofocus>
与:
<input id="plateno" class="form-control form-control-sm" type="text" name="plateno" placeholder="Vehicle Registration Number" :value="old('plateno')" required autofocus>
你应该验证$request
,在你的控制器update方法中添加这一行:
if (!$data)
abort(404);
【讨论】:
它仍然无法工作它返回相同的错误Error Attempt to assign property "plateno" on null
我忘了说你应该验证 $request!我已经更新了我的答案
问题是$data
为空,所以检查并调试$request->id
。【参考方案2】:
首先更改输入元素的名称
<label for="plateno">Vehicle Registration Number:</label>
<input id="plateno" class="form-control form-control-sm" type="text" name="plateno" placeholder="Vehicle Registration Number" :value="old('plateno')" required autofocus>
在表单中添加一个隐藏的输入,其中包含要更新的项目的 id
<input hidden name="id" value="$item->id">
在组件中
$data= Vehicle::find($request->get('id'));
$data->plateno=$request->get('plateno');
$data->type=$request->get('type');
$data->colour=$request->get('colour');
$data->brand=$request->get('brand');
$data->model=$request->get('model');
$data->save();
【讨论】:
以上是关于Laravel 8错误尝试在null上分配属性“plateno”的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 Laravel 8 中读取 null 上的属性“名称”
Laravel 8 的问题:在 null 上调用成员函数 count()
laravel 8 中的文件上传 - 在 null 上调用成员函数 getClientOriginalName() 时出错