msr2406's avatar
Level 11

Tinker not retrieving models

Tinker will count() the number of records for my tables but it will not ::find(1) or ::first() or ::all() etc. It just hangs. db:show delivers: SQLite .............................................................. 3.37.2
Connection .......................................................... sqlite
Database .......................................... database/database.sqlite
Host .......................................................................
Port .......................................................................
Username ...................................................................
URL ........................................................................
Open Connections ...........................................................
Tables .................................................................. 19
Total Size ....................................................... 164.00 KB

Table ................................................................. Size
admin_profiles ..................................................... 4.00 KB
cache .............................................................. 8.00 KB
cache_locks ........................................................ 8.00 KB
client_profile_volunteer_profile ................................... 4.00 KB
client_profiles ................................................... 12.00 KB
failed_jobs ........................................................ 8.00 KB
job_batches ........................................................ 8.00 KB
jobs ............................................................... 8.00 KB
migrations ......................................................... 4.00 KB
notes .............................................................. 4.00 KB
password_reset_tokens .............................................. 8.00 KB
personal_access_tokens ............................................ 12.00 KB
sessions .......................................................... 16.00 KB
team_invitations ................................................... 8.00 KB
team_user .......................................................... 8.00 KB
teams .............................................................. 8.00 KB
users ............................................................. 20.00 KB
volunteer_profiles ................................................. 4.00 KB
volunteer_references .............................................. 12.00 K

My componser.json is:

It does, however all work fine with the same database and models in a separate project where the composer.json is:

I've run composer update on the errant project but still have the problem. I'm stumped. Any ideas?

0 likes
6 replies
LaryAI's avatar
Level 58

It sounds like you're experiencing an issue where Tinker is hanging when trying to retrieve models using methods like ::find(1), ::first(), or ::all(). Here are a few steps you can take to troubleshoot and potentially resolve this issue:

  1. Check Database Connection: Ensure that your database connection is correctly configured in your .env file. Since you're using SQLite, make sure the path to your SQLite database is correct.

    DB_CONNECTION=sqlite
    DB_DATABASE=/full/path/to/database/database.sqlite
    
  2. Run Migrations: Ensure that your database schema is up to date by running the migrations.

    php artisan migrate
    
  3. Clear Cache: Sometimes, Laravel's cache can cause issues. Clear the cache to ensure that there are no stale configurations.

    php artisan config:cache
    php artisan config:clear
    php artisan cache:clear
    php artisan route:clear
    php artisan view:clear
    
  4. Check Model Configuration: Ensure that your models are correctly configured and that they are pointing to the correct tables. For example, your User model should look something like this:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class User extends Authenticatable
    {
        use HasFactory;
    
        protected $table = 'users'; // Ensure this matches your table name
    }
    
  5. Check for Infinite Loops or Heavy Operations: Ensure that there are no infinite loops or heavy operations in your model's boot methods, accessors, mutators, or relationships that could cause Tinker to hang.

  6. Check for Database Locks: Since you're using SQLite, ensure that there are no database locks that could be causing the issue. You can try deleting the database.sqlite file and recreating it if necessary.

  7. Check for Errors in Logs: Check the Laravel logs for any errors that might give you more insight into what's going wrong.

    tail -f storage/logs/laravel.log
    
  8. Test with a Simple Query: Try running a simple query directly in Tinker to see if it works. For example:

    php artisan tinker
    
    DB::table('users')->first();
    

    If this works, the issue might be with your Eloquent models.

  9. Reinstall Composer Dependencies: Sometimes, reinstalling the Composer dependencies can resolve issues.

    rm -rf vendor
    composer install
    
  10. Check for Differences in Working Project: Since you mentioned that the same database and models work fine in a separate project, compare the configurations, model definitions, and any custom code between the two projects to identify any differences.

If none of these steps resolve the issue, please provide more details about any error messages or logs you encounter, and we can further investigate the problem.

msr2406's avatar
Level 11

@laryai None of these steps resolved the issue. The other main difference between the two instances is that the one that works is a LiveWire project and the one that does not is a JetStream project set up for Vue3 and Inertia. But I don't see how that can make a difference to Eloquent queries in Tinker.

msr2406's avatar
Level 11

So for model ClientProfile,

DB::table('client_profiles')->first()

works as expected. but

use App\Models\ClientProfile

followed by

ClientProfile::first()

does not. I've cleared the caches.

msr2406's avatar
Level 11

The other main difference between the two instances is that the one that works is a LiveWire project and the one that does not is a JetStream project set up for Vue3 and Inertia. But I don't see how that can make a difference to Eloquent queries in Tinker.

msr2406's avatar
msr2406
OP
Best Answer
Level 11

Found the problem. It was the use of the $with clause in a model that causes a looping of underlying database selects. I found the problem by duplicating the ::find in a controller and dd()'ing the result and waiting for the browser to time out. Then I found the select looping in the error page generated by laravel in the browser. Got there in the end!

Snapey's avatar

@msr2406 $with is dangerous and should be used sparingly. Its too easy to end up with recursive model loading as you have discovered.

With the existence of Eager Loading, there are very few places where it genuinely helps.

End of PUBLIC SERVICE ANNOUNCEMENT

Please or to participate in this conversation.