Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Corbin's avatar

Not sure if I hit a Laravel bug with API resources: Using API resource switches php artisan localhost port on save

I'm following along with the Let's build a forum series here on laracsts. What I'm trying to do is make an activity feed for posts and comments and use API resources to output the JSON. The problem I keep getting is when I try and form the activity for Posts and Comments using their own resource the post on my server switches from 8000 to 8001 and then when I try and load the activity in the browser. Then when I try and use http://127.0.0.1:8001/profile/fbins/activity/ the port will switch to 8002 like so:

php artisan serve
Laravel development server started: http://127.0.0.1:8000
[Wed Jan  1 19:55:46 2020] 127.0.0.1:49613 [200]: /favicon.ico
Laravel development server started: http://127.0.0.1:8001
Laravel development server started: http://127.0.0.1:8002
Laravel development server started: http://127.0.0.1:8003

ActivityResource

class ActivityResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        if($this->type === 'create_comment'){
            return new CommentResource($this->subject);
        } elseif ($this->type === 'create_post'){
            return new PostResource($this->subject);
        }
    }
}

Activity Collection

class ActivityCollection extends ResourceCollection
{
    /**
     * The resource that this resource collects.
     *
     * @var string
     */
    public $collects = 'App\Http\Resources\ActivityResource';

    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'data' => $this->collection,
        ];
    }
}

Activity Model

class Activity extends Model
{
    protected $guarded = [];

    protected $with = ['subject'];

    public function subject()
    {
        return $this->morphTo();
    }
}

ProfileController

    public function activity(User $user)
    {
       return new ActivityCollection(Activity::where(['user_id' => $user->id])->latest()->paginate(1));
    }

When I change my activity resource just to output $this->subject everything works just fine, except for the fact that the json output isn't customized with an API resource.

class ActivityResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return $this->subject;
    }
}

I have no idea what's breaking and no idea how to figure this out. I use resources in other resources all of the time for example:

Working Example Comment Resource

class CommentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'creator' => new UserResource($this->creator),
            'content' => $this->content,
            'children' => new ChildCommentCollection($this->children),
        ];
    }
}

I guess my question is why is this broken and how do I fix it?

Edit

I've tried changing the name of the ActivityResource, I've tried adding resources that didn't have polymorphic relations:

ActivityResource

return [
            'id' => $this->id,
            'creator' => new UserResource($this->creator),
];

And I've tried running: composer dump-autoload, php artisan clear-compiled, and composer clear-cache

Still same error. Only on the activity resource and collection

edit 2

Now when I run phpunit I'm getting

.........Segmentation fault: 11

edit 3

Commenting out my ActivityResource output stops the Segmentation fault

public function toArray($request)
    {
        /*return [
            'id' => $this->id,
            'creator' => new UserResource($this->creator),
        ];*/
        
        /*if($this->type === 'create_comment'){
            return 'comment';//new CommentResource($this->subject);
        } elseif ($this->type === 'create_post'){
            return 'post'; //PostResource($this->subject);
        }*/
    }
0 likes
1 reply
Corbin's avatar
Corbin
OP
Best Answer
Level 9

The problem was in my UserResource I called the ActivityResource and in the ActivityResource I called Resources that would call the UserResource. It made an infinite loop.

public function toArray($request)
    {
        return [
            'name' => $this->name,
            'username' => $this->username,
            'slug' => $this->slug,
            'bio' => $this->bio,
            //'activity' => new ActivityCollection($this->activity()->latest()->paginate(1)),  
        ];
    }

Please or to participate in this conversation.