Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

562
Vistas
Trying to get property 'id' of non-object Laravel error

I am working on someone else code in a Project and when I try to run the php artisan migrate I am encountering an error!

class SeedAdminUser extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // seed admin user
        $admin = User::create([
            'name' => 'Admin',
            'email' => 'admin@admin.se',
            'password' => Hash::make('password'),
            'plan_id' => null,
        ]);

        $admin->assignRole(['administrator', 'company']);

        $plan = Plan::find(1);
        Subscription::create([
            'user_id' => 1,
            'plan_id' => $plan->id,
            'starts_at' => Carbon::now(),
            'ends_at' => $plan->interval == 'monthly' ? Carbon::now()->addMonth() : Carbon::now()->addMonths(12),
        ]);
    }

I am getting an error because of this line 'plan_id' => $plan->id,

Here is the Error message -

   ErrorException 

  Trying to get property 'id' of non-object

  at database/migrations/2021_06_04_055759_seed_admin_user.php:34
    30| 
    31|         $plan = Plan::find(1);
    32|         Subscription::create([
    33|             'user_id' => 1,
  > 34|             'plan_id' => $plan->id,
    35|             'starts_at' => Carbon::now(),
    36|             'ends_at' => $plan->interval == 'monthly' ? Carbon::now()->addMonth() : Carbon::now()->addMonths(12),
    37|         ]);
    38|     }

  1   database/migrations/2021_06_04_055759_seed_admin_user.php:34
      Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Trying to get property 'id' of non-object", "/Applications/MAMP/htdocs/iSurvey/database/migrations/2021_06_04_055759_seed_admin_user.php", [Object(App\User)])

      +21 vendor frames 
  23  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Any Idea what is wrong with that line 'plan_id' => $plan->id, and how to fix it

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This is because there is no records in plan model, First check if records exists in plan model

dd($plan);

or

$plan = $plan ? $plan->id : '';
over 4 years ago · Santiago Trujillo Denunciar

0

The problem is exactly what the error says. $plan is not an object, so trying to access a propriety on it result in an error.

Why is $plan not an object? Because Plan::find(1); cannot find a plan with an id of 1. $plan is probably null so you're effectively running null->id.

Since you're running migrations, you might want to make sure this code runs after your plans table is populated. I would give it a shot with php artisan migrate:fresh --seed (warning: this will empty your db tables and then repopulate them with migrations/seeders) or seeding in general.

Consider also using findOrFail instead of find to throw an exeception if the model is not found, since your subsequent code in this example depends on the model being found.

over 4 years ago · Santiago Trujillo Denunciar

0

Make sure your plans are seeded before this migration runs. It's safer to use findOrFail instead. This way, you'll know the instant a plan isn't found.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda