Home > Back-end >  Trying to get property 'id' of non-object Laravel error
Trying to get property 'id' of non-object Laravel error

Time:09-16

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' => '[email protected]',
            '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

CodePudding user response:

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 : '';

CodePudding user response:

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.

  • Related