I keep encountering this error on Laravel 8 with PHP 8. Im grabbing the id from the view like so:
<li><a href="category/{{$catego->id}}">{{$catego->categories}}</a></li>
This then goes to web.php like so:
Route::get('category/{id}', [UserController::class, 'categories']);
It then goes to the categories function like so:
function index(){
$category = category::all();
return view('index', )->with('category',$category);
}
function categories($id){
if(category::where('id',$id)->exists())
{
$category = category::where('id', $id)->first();
$viewimage = image::where('categories_id', $category->id)->get();
return view('categories',compact('category','viewimage'));
}
else {
return view('index')->with('status','category not exist');
}
}
It gives me correct information when i debug;
dd($viewimage)
But on categories page it give me error.
@extends('layout')
@section('categories')
<section class="main-section">
<div class="main-container">
<div class="img-main-container">
<div class="img-container">
@forelse($viewimage as $catimg)
<img src="{{asset('upload/images/'.$catimg->image)}}" alt="">
</div>
<p>{{$catimg->name}}</p>
@empty
<p>data not found</p>
@endforelse
</div>
</div>
</section>
@endsection
Here is layout page data I fetched category at two place, one is in header and second is in the form
Header category fetched here
@foreach($category as $catego)
<li><a href="category/{{$catego->id ?? 'not found'}}">{{$catego->categories ?? 'not found'}}</a></li>
@endforeach
category in form fetched here
@foreach($category as $catego)
<option value="{{$catego->id ?? 'not found'}}">{{$catego->categories ?? 'not found'}}</option>
@endforeach
When i use @umarFayyaz way i got the data how i want, but categories in the header lost showing "not found"
**Uploaded full controller above **
3
Answers
Here i came up with a solution.
I removed the category fetching query from Controller and fetched categories from layout page.
I didn't understand why it worked. Helpful If someone can explain.
@endforeach
in the categories function, $category is a instance so you can’t use foreach on it.
Instead of
<li><a href="category/{{$catego->id}}">{{$catego->categories}}</a></li>
, try this<li><a href="{{ url('category',$category->id')}}">{{$category->categories}}</a></li>
if you want to get an instance of category.I was going to edit your question, but there was too much detail that I didn’t want to override the original question. Perhaps take a look at this formatting and some of the benefits of laravel to simplify and possibly fix the problem.
Below try and label the file in which you are describing. Use StudlyCaps or better known as PascalCasing for class names. Variables should be in camelCasing. Typically database tables are plural when it relates to a single model (‘users’, ‘categories). When in a pivot snake casing again without pluralization and in alphabetical order (ie category_user) and column names are in snake_casing (ie a post made by a user… ‘posts’ table and ‘user_id’ column on posts… ‘posts.user_id’). The main reason I state these is because laravel does a lot under the hood to attempt to handle relationships, methods, etc… based on naming conventions. when you have a belongsTo relationship you will see when properly named something like.
Device.php:
category.blade.php:
Here you can reach for route model binding or route explicit binding. Also I think you should and want to separate this into two separate controller. UserController should know stuff about users and CategoryController should know stuff about categories… S.O.L.I.D principles where you are most likely coupling and violate Single Responsibility, Open/Closed, Interface Segragation, and Dependency Inversion which means with interface implementations and using abstractions instead of concrete classes setup in this way would also violate Liskov.
with the binding you can then achieve something like this
CategoryController.php
UserController.php