Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

angerfist's avatar

foreach() argument must be of type array|object, string given (View: /home/sites/alexo/resources/views/livewire/admin/admin-service-category-component.blade.php)

I can not find the reason how to remove this error admin-service-category-component.blade.php

<div>
    <style>
        nav svg {
            height: 20px;
        }
        nav .hidden{
            display: block; !important;

        }
    </style>
        <div class="section-title-01 honmob">
            <div class="bg_parallax image_02_parallax"></div>
            <div class="opacy_bg_02">
                <div class="container">
                    <h1>Service Categories</h1>
                    <div class="crumbs">
                        <ul>
                            <li><a href="/">Home</a></li>
                            <li>/</li>
                            <li>Service Categories</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <section class="content-central">
            <div class="content_info">
                <div class="paddings-mini">
                    <div class="container">
                        <div class="row portfolioContainer">
                            <div class="col-md-12 profile1">
                                <table class="table table-striped">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Image</th>
                                        <th>Name</th>
                                        <th>Slug</th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    @foreach($scategories as $scategory)
                                        <tr>
                                            <td>{{$scategory->id}}</td>
                                            <td><img src="{{asset('images/categories')}}/{{$scategory->image}}" width="60"  alt=""/></td>
                                            <td>{{$scategory->name}}</td>
                                            <td>{{$scategory->slug}}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>

                                </table>
                                {{$scategories->links()}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</div>

web.php

Route::get('/',HomeComponent::class)->name('home');
Route::get('/service-categories',ServiceCategoriesComponent::class)->name('home.service_categories');
//For Customer
Route::middleware(['auth:sanctum', 'verified'])->group(function(){
    Route::get('/customer/dashboard', CustomerDashboardComponent::class)->name('customer.dashboard');
});
//For Service Provider
Route::middleware(['auth:sanctum', 'verified','authsprovider'])->group(function(){
    Route::get('/sprovider/dashboard', SproviderDashboardComponent::class)->name('sprovider.dashboard');
});
//For Admin
Route::middleware(['auth:sanctum', 'verified','authadmin'])->group(function(){
 Route::get('/admin/dashboard', AdminDashboardComponent::class)->name('admin.dashboard');
 Route::get('/admin/service-categories',AdminServiceCategoryComponent::class)->name('admin.service_categories');
});

AdminServiceCategoryComponent.php

<?php

namespace App\Http\Livewire\Admin;

use App\Models\ServiceCategory;
use Livewire\Component;
use Livewire\WithPagination;


class AdminServiceCategoryComponent extends Component
{
    use WithPagination;
    public function render()
    {
        $scategories = ServiceCategory::paginate(10);
        return view('livewire.admin.admin-service-category-component',['scategories'=>'$scategories'])->layout('layouts.base');
    }
}

Tell me how to get rid of

0 likes
32 replies
MichalOravec's avatar

You have quotes here

['scategories'=> '$scategories']

Remove them

class AdminServiceCategoryComponent extends Component
{
    use WithPagination;

    public function render()
    {
        $scategories = ServiceCategory::paginate(10);

        return view('livewire.admin.admin-service-category-component', ['scategories'=> $scategories])->layout('layouts.base');
    }
}
johnDoe220's avatar

you must delete single qute

$scategories = ServiceCategory::paginate(10);
        return view('livewire.admin.admin-service-category-component',['scategories'=>$scategories])->layout('layouts.base');

or use compact

$scategories = ServiceCategory::paginate(10);
        return view('livewire.admin.admin-service-category-component', compact('scategories'))->layout('layouts.base');
angerfist's avatar

AdminServiceCategoryComponent.php

class AdminServiceCategoryComponent extends Component
{
    use WithPagination;

    public function render()
    {
        $scategories = ServiceCategory::paginate(10);

        return view('livewire.admin.admin-service-category-component', ['scategories'=> $scategories])->layout('layouts.base');
    }
}

admin-service-category-component.blade.php

<div>
    <style>
        nav svg {
            height: 20px;
        }
        nav .hidden{
            display: block; !important;

        }
    </style>
        <div class="section-title-01 honmob">
            <div class="bg_parallax image_02_parallax"></div>
            <div class="opacy_bg_02">
                <div class="container">
                    <h1>Service Categories</h1>
                    <div class="crumbs">
                        <ul>
                            <li><a href="/">Home</a></li>
                            <li>/</li>
                            <li>Service Categories</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <section class="content-central">
            <div class="content_info">
                <div class="paddings-mini">
                    <div class="container">
                        <div class="row portfolioContainer">
                            <div class="col-md-12 profile1">
                                <table class="table table-striped">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Image</th>
                                        <th>Name</th>
                                        <th>Slug</th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    @foreach($scategories as $scategory)
                                        <tr>
                                            <td>{{$scategory->id}}</td>
                                            <td><img src="{{asset('images/categories')}}/{{$scategory->image}}" width="60"  alt=""/></td>
                                            <td>{{$scategory->name}}</td>
                                            <td>{{$scategory->slug}}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>

                                </table>
                                {{$scategories->links()}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</div>

web.php

Route::get('/',HomeComponent::class)->name('home');
Route::get('/service-categories',ServiceCategoriesComponent::class)->name('home.service_categories');
//For Customer
Route::middleware(['auth:sanctum', 'verified'])->group(function(){
    Route::get('/customer/dashboard', CustomerDashboardComponent::class)->name('customer.dashboard');
});
//For Service Provider
Route::middleware(['auth:sanctum', 'verified','authsprovider'])->group(function(){
    Route::get('/sprovider/dashboard', SproviderDashboardComponent::class)->name('sprovider.dashboard');
});
//For Admin
Route::middleware(['auth:sanctum', 'verified','authadmin'])->group(function(){
 Route::get('/admin/dashboard', AdminDashboardComponent::class)->name('admin.dashboard');
 Route::get('/admin/service-categories',AdminServiceCategoryComponent::class)->name('admin.service_categories');
});

how can you fix it?

Snapey's avatar

show the complete AdminServiceCategoryComponent component please

Also, are you using any global scopes ?

angerfist's avatar

@Snapey

<?php

namespace App\Http\Livewire\Admin;

use App\Models\ServiceCategory;
use Livewire\Component;
use Livewire\WithPagination;


class AdminServiceCategoryComponent extends Component
{
    use WithPagination;

    public function render()
    {
        $scategories = ServiceCategory::paginate(10);

        return view('livewire.admin.admin-service-category-component', ['scategories'=> '$scategories'])->layout('layouts.base');
    }
}
tykus's avatar
tykus
Best Answer
Level 104

@angerfist you still have the single quotes wrapping here '$scategories' - this passes the string literal - '$scategories', not the contents of the variable. Change it to this:

return view('livewire.admin.admin-service-category-component', ['scategories'=> $scategories])
    ->layout('layouts.base');
2 likes
johnDoe220's avatar

@angerfist if you want use ServiseCategoryModel into view use the view composer: my code example

<?php

namespace App\Providers;

use App\Models\Category;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer(['front.*', 'auth.*'], function ($view){
            $view->with('categories', Category::where('parent_id', 0)->get());
        });
    }
}

i now send category model into all views into front and auth directory

angerfist's avatar

@tykus fixed it to your version, I still get an error:

foreach() argument must be of type array|object, null given (View: /home/sites/alexo/resources/views/livewire/admin/admin-service-category-component.blade.php) 
Snapey's avatar

@johnDoe220 of course. I know that. I just fail to see what throwing this randomly into the discussion achieves

angerfist's avatar

admin-service-category-component.blade.php

<div>
    <style>
        nav svg {
            height: 20px;
        }
        nav .hidden{
            display: block; !important;

        }
    </style>
        <div class="section-title-01 honmob">
            <div class="bg_parallax image_02_parallax"></div>
            <div class="opacy_bg_02">
                <div class="container">
                    <h1>Service Categories</h1>
                    <div class="crumbs">
                        <ul>
                            <li><a href="/">Home</a></li>
                            <li>/</li>
                            <li>Service Categories</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <section class="content-central">
            <div class="content_info">
                <div class="paddings-mini">
                    <div class="container">
                        <div class="row portfolioContainer">
                            <div class="col-md-12 profile1">
                                <table class="table table-striped">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Image</th>
                                        <th>Name</th>
                                        <th>Slug</th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    @foreach($scategories as $scategory)
                                        <tr>
                                            <td>{{$scategory->id}}</td>
                                            <td><img src="{{asset('images/categories')}}/{{$scategory->image}}" width="60"  alt=""/></td>
                                            <td>{{$scategory->name}}</td>
                                            <td>{{$scategory->slug}}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>

                                </table>
                                {{$scategories->links()}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</div>
Snapey's avatar

The view does not matter if you are getting null for ServiceCategory

you did not answer my question about global scopes

Snapey's avatar

open tinker

>>> App\Models\ServiceCategory::all();

what do you get

Snapey's avatar

ok so thats fine. You should be able to paginate the results in the view unless you have some form of scope in place that you have not declared.

If you change the way the collection is passed to the view, as suggested by michal over 3 hours ago then all should be fine

angerfist's avatar

@Snapey yes after michelle told me i corrected

AdminServiceCategoryComponent.php

<?php

namespace App\Http\Livewire\Admin;

use App\Models\ServiceCategory;
use Livewire\Component;
use Livewire\WithPagination;


class AdminServiceCategoryComponent extends Component
{
    use WithPagination;

    public function render()
    {
        $scategories = ServiceCategory::paginate(10);

        return view('livewire.admin.admin-service-category-component', ['scategories'=> $scategories])->layout('layouts.base');
    }
}

ErrorException foreach() argument must be of type array|object, null given (View: /home/sites/alexo/resources/views/livewire/admin/admin-service-category-component.blade.php)

Snapey's avatar

@angerfist and you have said that

        $scategories = ServiceCategory::paginate(10);

dd($scategories); 

        return view('livewire.admin.admin-service-category-component', ['scategories'=> $scategories])->layout('layouts.base');

returns null ?

angerfist's avatar

@Snapey yes https://i.imgur.com/YIbSYhm.png

class AdminServiceCategoryComponent extends Component
{
    use WithPagination;

    public function render()
    {
        $scategories = ServiceCategory::paginate(10);
        dd($scategories);

        return view('livewire.admin.admin-service-category-component', ['scategories'=> $scategories])->layout('layouts.base');
    }
}
tykus's avatar

@angerfist why did you override the paginate method?????

paginate should be the parent Eloquent method which returns a Paginator instance - why are you implementing something different??? Get rid of that method

Snapey's avatar

you need to go and stand in the corner for a bit facing the wall

Please or to participate in this conversation.