Answer the question
In order to leave comments, you need to log in
How to work with Adjacency List in Eloquent/Laravel if key is uuid type?
There is this sign:
{
/**
* 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');
}
}
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();
});
}
}
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']),
]);
"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
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;
}
foreach ($childrens as $child) {
$child->parent()->associate($child)->save();
}
dd($relation = $this->childSections())
, the source is in hand and each step on the screen Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question