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

oyincode's avatar

Eloquent Model returns empty array even though there's data in the database

This is my blade component, but the $notifications variable returns empty and I have some data in the DB Please help

<?php

namespace App\Http\Livewire;

use App\Models\Notification;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class LaraJobNotifications extends Component
{
    public function render()
    {
        $notifications = Notification::all();
   
        
        var_dump($notifications);

        return view('livewire.lara-job-notifications',[
                'notifications' => $notifications,
            ]
        );
    }
}

This is the Notification Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Notification extends Model
{
    use HasFactory;

    protected $fillable = ['title', 'job_link', 'company_name'];
}
0 likes
28 replies
LaryAI's avatar
Level 58

The issue might be related to the namespace of the Notification model. Make sure that the namespace in the LaraJobNotifications component matches the namespace in the Notification model.

Here's the updated code:

namespace App\Http\Livewire;

use App\Models\Notification;
use Livewire\Component;

class LaraJobNotifications extends Component
{
    public function render()
    {
        $notifications = Notification::all();
   
        var_dump($notifications);

        return view('livewire.lara-job-notifications', [
            'notifications' => $notifications,
        ]);
    }
}

Also, ensure that you have data in the notifications table in your database.

If the issue persists, you can try running the following command to clear the application cache:

php artisan cache:clear

If none of the above solutions work, please provide more information about your database configuration and any error messages you are receiving.

oyincode's avatar

After running the cache:clear command, this is what I get when I call var_dump($notifications);

object(Illuminate\Database\Eloquent\Collection)#1167 (2) { ["items":protected]=> array(0) { } ["escapeWhenCastingToString":protected]=> bool(false) }
MohamedTammam's avatar

@oyincode Then you don't have records on the database, or something wrong with your model structure.

BTW, it this Larajob NativePHP bounty app? šŸ˜…

hupp's avatar

@oyincode Check with this code and let me know your feedback.

// app/Http/Livewire/LaraJobNotifications.php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Notification; // Adjust the namespace according to your model location

class LaraJobNotifications extends Component
{
    public $notifications;

    public function mount()
    {
        $this->notifications = Notification::all();
    }

    public function render()
    {
        return view('livewire.lara-job-notifications');
    }
}
hupp's avatar

@oyincode Check your Database is same as you check in tool and in laravel project connected. Verify table name is proper.

 $this->notifications = Notification::get()->toArray();
oyincode's avatar

@hupp this is what I get array(0) { }. The table exists in my DB. The name of the table is notifications. I currently have 16 records in there

hupp's avatar

@oyincode Does they are soft_deleted ?? in that case you want to get the you have to use

Notification::withTrashed()->get()->toArray();

May now you get all soft_deleted records too.

hupp's avatar

@JussiMannisto Check DB Connection & Check Database Name same you checking is the same in .env.

oyincode's avatar

@JussiMannisto Yes, it doesn't use soft deletion.

The funny thing is when I run DB:select('select * from migrations'), I get the data from the DB. But when I run DB:select('select * from notifications'), it's still empty. I'm not sure where the problem might be

JussiMannisto's avatar

@oyincode Well your database obviously doesn't have any notifications then. Where did you get the 16 records you mentioned above?

hupp's avatar

@oyincode Which tool you used to check 16 records are there !

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=db_user
DB_PASSWORD=db_pass

Verify this in .env and mysql tool used.

hupp's avatar

@oyincode i told you to verify it many times before. But you are not doing that first. Check this .env file or config/database.php

JussiMannisto's avatar

Is your DB configuration correct? And are you using the correct DB connection? How are you checking that your DB has those models?

What do you get if you retrieve the models through tinker:

php artisan tinker
App\Models\Notification::all();
oyincode's avatar

@JussiMannisto When I run the php artisan tinker, this is what I get

> App\Models\Notification::all();
= Illuminate\Database\Eloquent\Collection {#7380
    all: [
      App\Models\Notification {#7382
        id: 1,
        title: "Senior Laravel Developer / Tech Lead (work from home)",
        job_link: "https://larajobs.com/job/3046",
        company_name: "Snapforms",
        created_at: "2023-07-25 03:51:00",
        updated_at: "2023-07-25 03:51:00",
      },
      App\Models\Notification {#7383
        id: 2,
        title: "Junior to Mid-level Front-end Web Developer",
        job_link: "https://larajobs.com/job/859",
        company_name: "Genos International",
        created_at: "2023-07-25 03:51:01",
        updated_at: "2023-07-25 03:51:01",
      },
      App\Models\Notification {#7384
        id: 3,
        title: "Senior Software Developer",
        job_link: "https://larajobs.com/job/2199",
        company_name: "TeachBoost",
:
JussiMannisto's avatar

@oyincode So there are notifications here but you don't see them in your component. Can you check if you're using the same database connection.

First in tinker:

php artisan tinker
App\Models\Notification::query()->getConnection()->getName();
App\Models\Notification::query()->getConnection()->getDatabaseName();

And then in your component:

dump(Notification::query()->getConnection()->getName());
dump(Notification::query()->getConnection()->getDatabaseName());

Btw, how are you running your app?

JussiMannisto's avatar

@oyincode

Doesn't this prove that there is data in the Model or am I missing something?

It only proves that there is data somewhere. But it doesn't look like you're querying that DB in your Livewire component.

oyincode's avatar

@JussiMannisto You are a life saver.

The Model uses App\Models\Notification::query()->getConnection()->getName() = 'mysql' App\Models\Notification::query()->getConnection()->getDatabaseName() = ''laravel_notifier'

The Component uses dump(Notification::query()->getConnection()->getName()) = 'nativephp' dump(Notification::query()->getConnection()->getDatabaseName()); = '"/Users/oyindamolaoyetunmibi/Library/Application Support/NativePHP/database/database.sqlite"

So apparently the component is connected to a different DB from my Model. So how can I rectify this?

I'm using NativePHP by the way

oyincode's avatar

@JussiMannisto yes I found the problem. NativePHP uses SQlite alone for its DB purposes. So storing my data in MySQL was never going to work. Thank @jussimannisto, you have been of great help

Snapey's avatar

@oyincode mark one of the answers as best reply so that the question shows as answered

Please or to participate in this conversation.