Harout
Harout

Reputation: 174

When i click the edit button, to edit a category its not working. Error : Trying to get property 'id' of non-object

CategoryController:

public function edit($id)
    {
    return view('category.edit')->with('categorys',Category::find($id));
    }


    public function update(Request $request, $id)
    {
    $category=Category::find($id);
    $category->name=$request->name;
    $category->save();
    return redirect()->back();
    }

edit.blade.php:

@extends('layouts.app')
@section('content')
<div class="card">
<div class="card-header">
    Manage Food Category
</div>
@foreach ($categorys as $category)
<div class="card-body">
    <form action="{‌{ route('category.update',$category->id) }}">
    @method('PUT')
    @csrf
    <div class="form-group">
    <label for="name">Update Name</label>
    <input type="text" name="name" class="form-control" value="{‌{ $category->name }}">
    <div class="form-group">
    <button type="submit" class="my-2 btn btn-outline-primary">Update</button>
    </div>
    </div>
    </form>
    </div>  
@endforeach

</div>
@endsection

When i click the edit button, to edit a category its not working. Error : Trying to get property 'id' of non-object

Upvotes: 1

Views: 33

Answers (1)

A.A Noman
A.A Noman

Reputation: 5270

No need foreach. Use like this

<form action="{‌{ route('category.update',$categorys->id) }}">

Upvotes: 1

Related Questions