Search code examples
laravel-8crud

while using Edit function in controller missing parameter error is showing up and when checked using dd {#connection: null #table: null }


i am trying to make a edit function for my subcategories while its show eerror like missing parament, dd showing connection null

the web.php in my routes web.php

<?php
use App\Http\Controllers\SubCategoryController;

Route::resource('/subcategory', SubCategoryController::class);

the subcategorycontroller controller

public function edit(subCategory $subCategory)
{
    // dd($subCategory);
}

the subcategory model model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SubCategory extends Model
{
    use HasFactory;
    protected $fillable =['name'];
}

my migration file for subcategory migration

<?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');
    }
}

the output for dd output

 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 [▶]

} the connection and table is showing null when its supposed to say mysql,subcategory

i am also providing the code where i did not used dd and done normaly and its error controller

public function edit(subCategory $subCategories)
    {
        // dd($subCategories);
        return view('subcategory/edit', compact('subCategories'));
    }

the edit.blade.php file in my views 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> &nbsp;
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

and the output of missing parameter was obtained output

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

how can i solve this


Solution

  • changing the edit function in the controller fixed the problem

    public function edit($id)
    {
      
        $subCategory  = Subcategory::find($id);
        return view('subcategory/edit', compact('subCategory'));
    }