Summer Sale! All accounts are 50% off this week.

georgetown74's avatar

Error: Attempt to assign property "title" on null

I am at 9:53 in Episode 10 of the Build A Voting App series ("Testing: Showing Ideas") and am getting the following error when trying to run the "ideas_pagination_works" test below:

  1. Tests\Feature\ShowIdeasTest::ideas_pagination_works Error: Attempt to assign property "title" on null

/var/www/html/tests/Feature/ShowIdeasTest.php:60

Line 60 is this instance:

$ideaOnFirstPage->title = 'My First Idea';

Any suggestions?

    /** @test */
    public function ideas_pagination_works()
    {

        Idea::factory(Idea::PAGINATION_COUNT + 1)->create();

        $ideaOnFirstPage = Idea::find(1);
        $ideaOnFirstPage->title = 'My First Idea';
        $ideaOnFirstPage->save();

        $ideaOnSecondPage = Idea::find(PAGINATION_COUNT + 1);
        $ideaOnSecondPage->title = 'My Nth Idea';
        $ideaOnSecondPage->save();

        $response = $this->get('/');

        $response->assertSee($ideaOnFirstPage->title);
        $response->assertDontSee($ideaOnSecondPage->title);

        $response = $this->get('/?page=2');

        $response->assertSee($ideaOnSecondPage->title);
        $response->assertDontSee($ideaOnFirstPage->title);

    }
0 likes
2 replies
MohamedTammam's avatar
Level 51

That means you don't have a record with id=1

You can use findOrFail instead to throw 404 error when the record isn't found.

$ideaOnFirstPage = Idea::findOrFail(1);

Please or to participate in this conversation.