1

I am Laravel Eloquent ORM using PgSql, when i use with() method is return error.

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: uuid = integer LINE 1:

Laravel Version 10.7.1 Php Version 8.1.17

Here is the table structure, for some reason i change the table name.

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

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

here is the code from controller

    /**
     * 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,
        ]);
    }

But when i use method $modelParent->parentChilds()->createMany([...data]) it retur no error

Parent Model

    /**
     * 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');
    }

Child Model

     /**
     * 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();
    }
Zulfikar
  • 49
  • 6
  • Not a php programmer myself, but it looks like your input is not correct, see this error message: ERROR: operator does not exist: uuid = integer. Where does this integer come from? And check the generated SQL in your log files, that makes debugging usually easier – Frank Heikens Jun 28 '23 at 04:21
  • Thank for replying, i already tried, Laravel eloquent include som relational. when i tried using manual query, not using eloquent orm it work fine, no return error, i think the error give from orm laravel – Zulfikar Jun 28 '23 at 04:26
  • 2
    The problem is the integer, where does it come from? Something in your code is using an integer, where it should use a uuid (or string) – Frank Heikens Jun 28 '23 at 05:40
  • I think that from with() eloquent method – Zulfikar Jun 28 '23 at 06:02

1 Answers1

2

As mentioned in the comments by @Frank Heikens, you would need to validate the input to be an 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");
    }
}

You would need to explicitly state the table name on your child model.

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

The second parameter for the method parent in your Child Model should be parent_id and not 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();
}

Documentation

linktoahref
  • 7,812
  • 3
  • 29
  • 51