Laravel多态关系commentable_type验证

ran*_*425 5 php laravel eloquent

我正在使用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
Run Code Online (Sandbox Code Playgroud)

例如,假设API收到了以下新注释:

{
    "body": "This a test comment",
    "commentable_type": "posts",
    "commentable_id": "1"
}
Run Code Online (Sandbox Code Playgroud)

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

小智 9

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

如果是这种情况,则没有现有的验证规则可以这样做,但您可以创建一个。
根据文档,您可以执行以下操作:

首先,在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));
});
Run Code Online (Sandbox Code Playgroud)

这就是你将如何使用它:

'commentable_id' => 'required|poly_exists:commentable_type
Run Code Online (Sandbox Code Playgroud)

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

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

希望这可以帮助!


Ved*_*ant 5

包括变形图的更好方法:

    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));
    });
Run Code Online (Sandbox Code Playgroud)


Dev*_*evK 1

编辑

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

$type = $comment->commentable_type;
if(class_exists($type)) echo "it exists";
Run Code Online (Sandbox Code Playgroud)

根据您的需要,您可以对其继承进行额外检查(例如,它扩展了 class Model)。或者任何其他真正适合您需求的东西。

编辑2

如果我是你,我就会这么做。我会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);
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

旧答案:

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

确保正确性的最简单方法就是始终通过关系来保存。例如:

$post = Post::first();
$comment = new Comment($attributes);
$post->comments()->save($comment).
Run Code Online (Sandbox Code Playgroud)

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

额外检查

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