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 **
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:
class Post extends Model
{
public function author(): BelongsTo {
return $this->belongsTo(User::class);
}
// $this->author_name
public getAuthorNameAttribute($value)
{
return ucfirst($value)
}
}
category.blade.php:
<li>
<a href="category/{{ $catego->id }}">
{{ $catego->categories }}
</a>
</li>
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.
use Http/Controllers/CategoryController;
/* don't shorten variable names just be more specific or location based within your repository. This way when doing a search on your code base the location or use case is intuitive and easily found.*/
Route::get('category/{category}', [CategoryController::class, 'detail']);
with the binding you can then achieve something like this
CategoryController.php
function detail(Category $category, Request $request) {
if(is_empty($category)) {
return view('index')->with('status','category not exist');
}
$categoryImages = image::where('categories_id', $category->id)->get();
return view('categories', compact('category', 'categoryImages'));
}
UserController.php
//again here naming doesn't make sense doing an all on an index but returning back to the view in singular form.
function index(Request $request) {
$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'));
}
return view('index')->with('status','category not exist');
// this really should be reversed to save on queiries there are also other ways
if(!category::where('id',$id)->exists()) {
return view('index')->with('status','category not exist');
}
$category = category::where('id', $id)->first();
$viewimage = image::where('categories_id', $category->id)->get();
return view('categories',compact('category','viewimage'));
}