Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

568
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda