#There is some problem in my Controller

19 messages · Page 1 of 1 (latest)

west vessel
#

I am making a CRUD in laravel, My data is inserting in database and also showing on Admin panel but when I press edit button it gives me the error i.e;

"Too few arguments to function App\Http\Controllers\UserModelController::edit(), 0 passed in C:\xampp\htdocs\laravelCRUD\CRUD\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected"

faint heron
#

Please read the #rules

#

and the issue probably due to you are not passing the id when you clicked the button.

#

Please share the frontend codes?

west vessel
#

U mean View page?

faint heron
#

the codes that contain the edit button

west vessel
faint heron
#

Again, please read the #rules

#

Sharing Code: Please do not take screenshots of code, instead, use triple backticks around your code when pasting into Discord:
☝️

west vessel
#

ooh sorry

#
            <tr>
                <th>{{ $emp->id }}</th>
                <th>{{ $emp->name }}</th>
                <th>{{ $emp->email }}</th>
                <th>{{ $emp->password }}</th>
                <th><img src="{{ asset('images' . $emp->image)}}" alt="" width="100" ></th>
                <th>
                    <a href="{{ route('edit', $emp->id)}}" class="btn btn-primary">Edit</a>
                    <a href="{{ route('destroy',$emp->id)}}" class="btn btn-danger">Delete</a>
                </th>
              </tr> 
            @endforeach
#

And here is the code of my controller of edit

    public function edit($id)
    {
        $employe = UserModel::find($id);
        return view('edit', ['editData' => $employe]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => ['required'],
            'email' => ['required'],
        ]);

        $result = UserModel::find($id)->update([
            'name' => $request->name,
            'email' => $request->email,

        ]);
        if ($result) {
            return redirect()->route('edit', $id)->with('success', 'Employe has been Updated');
        } else {
            return redirect()->route('edit', $id)->with('failed', 'Employe has failed to Update');
        }
    }
faint heron
#

Share your route as well.

west vessel
#

Route

Route::get('/edit/id',[UserModelController::class,'edit'])->name('edit');
Route::post('/edit/id',[UserModelController::class,'update']);
faint heron
west vessel
#

but now this error appears

Attempt to read property "id" on null
faint heron
#

Dump your $employe before the return

west vessel
#
App\Models\UserModel {#295 ▼ // app\Http\Controllers\UserModelController.php:87
  #connection: "mysql"
  #table: "user_models"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:7 [▼
    "id" => 1
    "name" => "ewf3rv3r3q3g1"
    "email" => "[email protected]"
    "password" => "5298e818188645a83ceaa5eb927cd798"
    "image" => "C:\xampp\tmp\php83D5.tmp"
    "created_at" => "2022-12-28 19:20:17"
    "updated_at" => "2022-12-28 19:20:17"
  ]
  #original: array:7 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:4 [▶]
  #guarded: array:1 [▶]
faint heron
#

return view('edit', ['editData' => $employe]);
so the error is come from the page returned