Skip to content

Usage of class_basename cause to use wrong PHP class #130

@bastos71

Description

@bastos71

In getClassNameForRelationships methods, usage of class_basename allows to get the class without the FQN, causing an issue when multiple classes are called with the same base name in the project.

Example :

  • model classes
    • App\Models\Product <- Parent
    • App\Models\ProductPack <- Child
    • App\Models\ProductSimple <- Child
  • resource class App\Http\Resources\Product
  • creating this relation :
<?php

namespace App\Models;

use App\Models\Interfaces\ProductInterface;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Parental\HasParent;

class ProductPack extends Product implements ProductInterface
{
    use HasParent;

    public const TYPE = 'pack';

    public function containedProducts(): BelongsToMany
    {
        return $this->belongsToMany(ProductSimple::class);
    }
}

Testing in tinker will output this error :

> \App\Models\ProductPack::first()->containedProducts;
[!] Aliasing 'Product' to 'App\Http\Resources\Product' for this Tinker session.

   ArgumentCountError  Too few arguments to function Illuminate\Http\Resources\Json\JsonResource::__construct(), 0 passed in vendor/tightenco/parental/src/HasParent.php on line 78 and exactly 1 expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions