Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

570
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!