在控制器中使用 Edit 函数时,缺少参数错误出现,使用 dd {#connection: null #table: null } 检查时
while using Edit function in controller missing parameter error is showing up and when checked using dd {#connection: null #table: null }
我正在尝试为我的子类别创建一个编辑功能,但它显示错误,例如缺少参数,dd 显示连接为空
我的路线中的 web.php
web.php
<?php
use App\Http\Controllers\SubCategoryController;
Route::resource('/subcategory', SubCategoryController::class);
子类别控制器
控制器
public function edit(subCategory $subCategory)
{
// dd($subCategory);
}
子类模型
型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SubCategory extends Model
{
use HasFactory;
protected $fillable =['name'];
}
我的子类别迁移文件
迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSubCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sub_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('category_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sub_categories');
}
}
dd 的输出
输出
App\Models\SubCategory {#1377 ▼
#fillable: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
连接和 table 在应该显示 mysql,subcategory
时显示为 null
我还提供了我没有使用 dd 并正常完成的代码及其错误
控制器
public function edit(subCategory $subCategories)
{
// dd($subCategories);
return view('subcategory/edit', compact('subCategories'));
}
我认为的edit.blade.php文件
edit.blade
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Edit Category</div>
<div class="card-body">
<form method="POST" action="{{ route('subcategory.update',[$subCategories->id]) }}">
@method('PUT')
@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" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Commit Changes') }}
</button>
<a class="btn btn-primary" href="{{route('subcategory.index')}}">{{ __('lang.Cancel') }}</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
并得到缺失参数的输出
输出
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameter for [Route: subcategory.update] [URI:
subcategory/{subcategory}] [Missing parameter: subcategory]. (View:
C:\xampp\htdocs\Exam\resources\views\subcategory\edit.blade.php)
http://127.0.0.1:8000/subcategory/1/edit
我该如何解决这个问题
更改控制器中的编辑功能解决了问题
public function edit($id)
{
$subCategory = Subcategory::find($id);
return view('subcategory/edit', compact('subCategory'));
}
我正在尝试为我的子类别创建一个编辑功能,但它显示错误,例如缺少参数,dd 显示连接为空
我的路线中的 web.php web.php
<?php
use App\Http\Controllers\SubCategoryController;
Route::resource('/subcategory', SubCategoryController::class);
子类别控制器 控制器
public function edit(subCategory $subCategory)
{
// dd($subCategory);
}
子类模型 型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SubCategory extends Model
{
use HasFactory;
protected $fillable =['name'];
}
我的子类别迁移文件 迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSubCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sub_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('category_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sub_categories');
}
}
dd 的输出 输出
App\Models\SubCategory {#1377 ▼
#fillable: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
} 连接和 table 在应该显示 mysql,subcategory
时显示为 null我还提供了我没有使用 dd 并正常完成的代码及其错误 控制器
public function edit(subCategory $subCategories)
{
// dd($subCategories);
return view('subcategory/edit', compact('subCategories'));
}
我认为的edit.blade.php文件 edit.blade
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Edit Category</div>
<div class="card-body">
<form method="POST" action="{{ route('subcategory.update',[$subCategories->id]) }}">
@method('PUT')
@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" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Commit Changes') }}
</button>
<a class="btn btn-primary" href="{{route('subcategory.index')}}">{{ __('lang.Cancel') }}</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
并得到缺失参数的输出 输出
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameter for [Route: subcategory.update] [URI:
subcategory/{subcategory}] [Missing parameter: subcategory]. (View:
C:\xampp\htdocs\Exam\resources\views\subcategory\edit.blade.php)
http://127.0.0.1:8000/subcategory/1/edit
我该如何解决这个问题
更改控制器中的编辑功能解决了问题
public function edit($id)
{
$subCategory = Subcategory::find($id);
return view('subcategory/edit', compact('subCategory'));
}