A
A
Anton Anton2019-08-02 10:53:15
Laravel
Anton Anton, 2019-08-02 10:53:15

How to work with Adjacency List in Eloquent/Laravel if key is uuid type?

There is this sign:

spoiler
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sections', function (Blueprint $table) {
            $table->uuid('id');
            $table->uuid('parent_id')->nullable();
            $table->string('caption', 150);
            $table->timestamps();

            $table->index('parent_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sections');
    }
}

and model c with id auto-generation when creating (although if you don’t generate id in creating, but pass it as an attribute when creating, nothing changes):
spoiler
class Section extends Model
{
    public function childSections()
    {
        return $this->hasMany('App\Section', 'parent_id', 'id');
    }
    public function parentSection()
    {
        return $this->belongsTo('App\Section', 'parent_id', 'id');
    }

    /**
     * model life cycle event listeners
     */
    public static function boot()
    {
        parent::boot();

        static::creating(function ($instance) {
            $instance->attributes['id'] = Str::uuid();
        });
    }
}

with a simple creation through App\Section::create()id in the database, it is normally filled, but when using "relationships":
$section = App\Section::create(['caption' => '1']);
        $section->childSections()->saveMany([
            new App\Section(['caption' => '1.1']),
            new App\Section(['caption' => '1.2']),
            new App\Section(['caption' => '1.3']),
            ]);

for some reason, the "children" have a parent_id "0"(not even null, but a string with zero). How to make sure that parent_id is filled with the desired value? (if you use a standard int with auto-increment as id, everything works)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Vasilkov, 2019-08-02
@gzhegow

Well, that's what I wrote yesterday. Nobody knows how to take off from Laravel, so they called me and drove me with a stick for the question.
Because if you look at the source, then

public function saveMany($models)
    {
        foreach ($models as $model) {
            $this->save($model);
        }

        return $models;
    }

And where do you see IDs here?
He stupidly filled in, probably 0 in the database by default (NOT NULL DEFAULT 0). Or the model immediately
says Welcome to the world of Laravel, where everyone knows that it is beautiful, but no one will help you, because it is beautiful.
Of course, there is no idea, but if we assume that I understood it, then there is
foreach ($childrens as $child) {
  $child->parent()->associate($child)->save();
}

Although if you dig deeper, then there seems to be ->setForeignAttributesForCreate(), which should do this, but either the getter does not give it a value, or the setter locks the installation, or there are still a thousand reasons, in short dd($relation = $this->childSections()), the source is in hand and each step on the screen
Why not vice versa? And fuck knows

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question