To update the added categories, I encounter errors that are returned to me. I think there are functions missing or I wrote badly at the level of my controller
I got this error
ErrorException Attempt to read property "category_name" on int
How to solve that ?
Here is my controller CategorieController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Categorie;
class CategorieController extends Controller
{
//
public function ajoutercategorie(){
return view('admin.ajoutercategorie');
}
public function sauvercategorie(Request $request){
$validatedData = $request->validate([
'category_name' => 'required | max:255',
]);
$categorie = Categorie::create($validatedData);
return redirect('/ajoutercategorie')->with('status', 'La catégorie '
.$categorie->category_name.' a été ajoutée avec succès');
}
public function categorie(){
$categories = Categorie::get();
return view('admin.categorie')->with('categories', $categories);
}
public function edit_categorie($id){
$categorie = Categorie::find($id);
return view('admin.editcategorie')->with('categorie', $categorie);
}
public function modifiercategorie(Request $request, Categorie $id){
$validatedData = $request->validate([
'category_name' => 'required | max:255',
]);
$categorie = Categorie::whereId($id)->update($validatedData);
return redirect('/categorie')->with('status', 'La catégorie'
.$categorie->category_name. 'a été modifiée avec succès');
}
}
Here my editcategorie.blade.php
@extends('layouts.appadmin')
@section('title')
Modifier la catégorie
@endsection
@section('contenu')
<div >
<div >
<div >
<div >
<h4 >Modifier la catégorie</h4>
@if (Session::has('status'))
<div >
{{Session::get('status')}}
</div>
@endif
@if ($errors->any())
<div >
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form id="commentForm" method="POST" action="{{ route('categories.modifiercategorie') }}">
@csrf
<fieldset>
<div >
<label for="cemail">Nom de la catégorie</label>
<input id="cemail" type="text" name="category_name" value="{{$categorie->category_name}}">
</div>
<input type="submit" value="Modifier">
</fieldset>
</form>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
{{--<script src="Administrateur/js/form-validation.js"></script>
<script src="Administrateur/js/bt-maxLength.js"></script>--}}
@endsection
My categorie.blade.php
@extends('layouts.appadmin')
@section('title')
Catégorie
@endsection
@section('contenu')
<div >
<div >
<h4 >Catégorie</h4>
@if (Session::has('status'))
<div >
{{Session::get('status')}}
</div>
@endif
<div >
<div >
<div >
<table id="order-listing" >
<thead>
<tr>
<th>Order #</th>
<th>Nom de la catégorie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $categorie)
<tr>
<td>{{$categorie->id}}</td>
<td>{{$categorie->category_name}}</td>
<td>
<button onclick="window.location ='{{url('/edit_categorie/' .$categorie->id)}}'">Modifier</button>
<button >Supprimer</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script src="Administrateur/js/data-table.js"></script>
@endsection
</body>
</html>
How can I solve this ?
I think the error comes from my function modifiercategorie in my CategorieController
CodePudding user response:
Your problem is here $categorie = Categorie::whereId($id)->update($validatedData);
because update
method returns count of updated columns. You should find a category.
Try to do this
...
$categorie = Categorie::whereId($id)->first()
$categorie->update($validatedData);
...
CodePudding user response:
in the method that you mentioned(modifiercategorie):
$categorie = Categorie::whereId($id)->update($validatedData);
here you are doing wrong you cannot update it's data by Validate method. you need to put data like this :
$categorie = Categorie::whereId($id)->update(['category_name'=>$request->category_name]);