postgresql 用方法在Laravel中雄辩不行,用PgSql

mwngjboj  于 2023-06-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(123)

我是Laravel Eloquent ORM使用PgSql,当我使用with()方法是返回错误。
SQLSTATE [42883]:未定义函数:7错误:操作员不存在:uuid = integer行1:
Laravel版本10.7.1 Php版本8.1.17
下面是表的结构,由于某种原因,我改变了表名。

parents
--------
id (uuid)
name (string)
user_id (uuid)
description (text)

details
---------
id (uuid)
parent_id (uuid)
title (stirng)
description (tinytext)

下面是来自控制器的代码

/**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $this->authorize('viewParent', \App\Models\Parent::class);

        $model = \App\Models\Parent::with(['user', 'parentChilds'])->findOrFail($id);

        return view("$this->view.show", [
            'model' => $model,
            'title' => "$this->title",
            'route' => $this->route,
        ]);
    }

但是当我使用$modelParent->parentChilds()->createMany([...data])方法时,它没有返回任何错误。
父模型

/**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'user_id',
        'description',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'string',
    ];

    /**
     * Get the user that owns the MinuteOfMeeting
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id')->withTrashed();
    }

    /**
     * Get all of the minuteOfMeetingDetails for the MinuteOfMeeting
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function parentChilds(): HasMany
    {
        return $this->hasMany(ParentChild::class, 'parent_id', 'id');
    }

子模型

/**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'parent_id',
        'title',
        'description'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'string',
        'parent_id' => 'string',
    ];

    /**
     * Get the minuteOfMeeting that owns the MinuteOfMeetingDetail
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(Parent::class, 'parent', 'id')->withTrashed();
    }
x759pob2

x759pob21#

正如@Frank Heikens的评论中提到的,您需要验证输入是否是UUID。

use App\Models\Parent;
use Illuminate\Validation\Validator;
use Illuminate\Database\Eloquent\ModelNotFoundException;

/**
 * Display the specified resource.
 */
public function show(Request $request, string $id)
{
    $validator = Validator::make($request->all(), [
        'id' => 'required|uuid',
    ]);

    if ($validator->fails()) {
        return redirect('listing-page') /* change this to your intended url */
                    ->withErrors($validator);
    }

    $this->authorize('viewParent', Parent::class);

    try {
        $model = Parent::with(['user', 'parentChilds'])->findOrFail($id);

        return view("$this->view.show", [
            'model' => $model,
            'title' => "$this->title",
            'route' => $this->route,
        ]);
    } catch (ModelNotFoundException $e) {
        abort("404");
    }
}

您需要在子模型上显式地声明表名。

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class ParentChild extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'details';
}

在您的子模型中,方法parent的第二个参数应该是parent_id,而不是parent

/**
 * Get the minuteOfMeeting that owns the MinuteOfMeetingDetail
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function parent(): BelongsTo
{
    return $this->belongsTo(Parent::class, 'parent_id', 'id')->withTrashed();
}

文档

相关问题