如何正确使用Spatie\LaravelData来检索数据?

huangapple go评论42阅读模式
英文:

How to properly use Spatie\LaravelData for retrieveing data?

问题

我了解到Spatie\LaravelData是在我搜索如何在Laravel中实现DTO时介绍给我的。
对于单个模型的插入/更新,使用数据类非常简单。我倾向于以这种方式保持我的更新是原子性的。

然而,我在使用LaravelData检索对象并发送到前端时遇到了问题,特别是对于嵌套模型。

我有这个示例,我有Post和Category模型:

class Post extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'category_id'
    ];

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }
}

class Category extends Model
{
    use HasFactory;

    protected $fillable = [
        'category',
        'description'
    ];
}

我有这些数据类:

class PostData extends Data
{
    public function __construct(
        public string $title,
        public string $category_id,
    ) {
    }
}

class CategoryData extends Data
{
    public function __construct(
        public string $category,
        public string $description
    ) {
    }
}

我希望能够发送一个包含Post及其Category的STO对象,也希望能够在索引页上拥有一组Post及其Category。

我尝试使用它们文档中描述的这种方法,但首先它会报错,其次我不确定如何从controller的index()或view()方法中返回它:

class PostData extends Data
{
    public function __construct(
        public string $title,
        public string $category_id,
        #[DataCollectionOf(CategoryData::class)]
        public DataCollection $category,
    ) {
    }
}

有人曾经使用Spatie\LaravelData来将DTO返回给前端吗?

英文:

I was introduced to Spatie\LaravelData when I was searching for how to implement DTOs in Laravel.
Using Data classes for insert/update for a single model is pretty straightforward. I tend to keep my updates atomic in this way.

However I have a problem for using LaravelData to retrieve objects and send to front end, specially with nested models.

I have this example I have Post and Category models:

class Post extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'category_id'
    ];

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

}

class Category extends Model
{
    use HasFactory;

    protected $fillable = [
        'category',
        'description'
    ];
}

I have these Data classes:

class PostData extends Data
{
    public function __construct(
        public string $title,
        public string $category_id,
    ) {
    }
}

class CategoryData extends Data
{
    public function __construct(
        public string $category,
        public string $description
    ) {
    }
}

I want to be able to send a STO object with Post and its Category, also would love to be able to have a collection of Posts and their Categories for index page.

I tried using this approach as described in their documentation, but first it gives an error and second I'm not sure how to return that from index() or view() methods in controller:

class PostData extends Data
{
    public function __construct(
        public string $title,
        public string $category_id,
        #[DataCollectionOf(CategoryData::class)]
        public DataCollection $category,
    ) {
    }
}

Anyone ever used Spatie\LaravelData for returning DTOs to from end?

答案1

得分: 4

你试图使用 DataCollection,但应该使用嵌套。这是因为你的帖子与分类模型有一个 BelongsTo 关系。

class PostData extends Data
{
    public function __construct(
        public string $title,
        public string $category_id,
        public CategoryData $category,
    ) {
    }
}
英文:

You are trying to use a DataCollection when you should be using nesting. This is because your post has a BelongsTo relationship with the category model.

class PostData extends Data
{
    public function __construct(
        public string $title,
        public string $category_id,
        public CategoryData $category,
    ) {
    }
}

huangapple
  • 本文由 发表于 2023年2月6日 17:26:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定