Laravel Polymorphic Relations commentable_type validation

问题描述 投票:3回答:4

我正在使用REST API来接收数据。 数据模型与多态相关,类似于文档中的模型: https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations

posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

比方说,例如,API正在接收这个新评论:

{
    "body": "This a test comment",
    "commentable_type": "posts",
    "commentable_id": "1"
}

如何验证收到的commentable_type是否存在且有效?

php laravel eloquent
4个回答
4
投票

如果我正确理解了您的问题,那么您将尝试验证多态关系的对象是否存在,对于给定的commentable_typecommentable_id

如果是这种情况,则没有现有的验证规则,但您可以创建一个。 基于the documentation,您可以做以下事情:

首先,在服务提供者的boot方法中添加新规则(例如AppServiceProvider):

Validator::extend('poly_exists', function ($attribute, $value, $parameters, $validator) {
    if (!$objectType = array_get($validator->getData(), $parameters[0], false)) {
        return false;
    }

    return !empty(resolve($objectType)->find($value));
});

这就是你如何使用它:

'commentable_id' => 'required|poly_exists:commentable_type

规则的作用是尝试从输入值中获取可注释类型(基于传递给规则的参数,在我们的例子中是commentable_type),然后解析对象并尝试查找给定ID的记录( $value)。

请注意,为了使其正常工作,commentable_type的值必须是完全限定的类名(例如App\Models\Post)。

希望这可以帮助!


1
投票

更好的方法,包括变形映射:

    Validator::extend('poly_exists', function ($attribute, $value, $parameters, $validator) {
        if (! $type = array_get($validator->getData(), $parameters[0], false)) {
            return false;
        }

        if (Relation::getMorphedModel($type)) {
            $type = Relation::getMorphedModel($type);
        }

        if (! class_exists($type)) {
            return false;
        }

        return ! empty(resolve($type)->find($value));
    });

0
投票

编辑

我可能第一次误解了。如果你想检查commentable_type中保存的模型是否存在,你可以这样做:

$type = $comment->commentable_type;
if(class_exists($type)) echo "it exists";

根据您的需要,您可以对inheritance进行额外检查(例如,它扩展了类Model)。或其他任何符合您需求的东西。

Aaditi

如果我是你,我就会这样做。我会将属性protected $allRelations添加到你的Comment模型并手动放入所有的关系。然后制作一些帮助模型来检查它是否在数组中。

简单的例子:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    // ..

    protected $allRelations= [
        'posts' => '\App\Post',
        'videos' => '\App\Video',
    ];

    public static function validateRelationNs($ns) {
        return in_array($ns, $this->allRelations);
    }

    public static function validateRelationName($name) {
        return array_key_exists($name, $this->allRelations);
    }

    // ...
}

老答案:

Laravel期望多态类型列的模型的完整名称空间名称(在您的情况下,commentable_type应该是\Full\Ns\Post,而不是posts)。

确保正确性的最简单方法是始终保存它through the relationship。例如:

$post = Post::first();
$comment = new Comment($attributes);
$post->comments()->save($comment).

这将自动正确设置commentable_idcommentable_type(假设您的关系正确定义)。

额外检查

除此之外,你可以通过模型events检查。您可以在保存到数据库之前对其进行验证。


0
投票

我的最终版本适用于验证类型和ID:

Validator::extend('poly_exists', function ($attribute, $value, $parameters, $validator) {
    if (!$objectType = array_get($validator->getData(), $parameters[0], false)) {
        return false;
    }

    if (!class_exists($objectType)) {
        return false;
    }

    return !empty(resolve($objectType)->find($value));
});
© www.soinside.com 2019 - 2024. All rights reserved.