Eloquent attiecību špikeris
Viens pret vienu attiecība
Biznesa definīcija: Lietotājam (User) ir iepirkumu grozs.
Izstrādātāja definīcija: Modelim User ir viens ShoppingCart. ShoppingCart pieder vienam User.
Diagramma
Attiecības skaidrojums:
ShoppingCart tabulai jāglabā lietotāja ID.
Modeļi: Tātad mums vajag divus modeļus: User un ShoppingCart. Šie modeļi tiks aizpildīti ar šādu kodu:
class User{
/*
* This function defines a relation with Shopping Cart and tells that ID is stored in
* ShoppingCart model
*/
public function shoppingCart()
{
return $this->hasOne(ShoppingCart::class);
}
}
class ShoppingCart{
/*
* This function defines a relation with the User and tells that ID is stored in
* this model
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Datubāzes migrācija:
Schema::create('users', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('shopping_carts', function (Blueprint $table) {
$table->increments('id');
# Some more columns which you will need
$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users');
});
Ierakstu saglabāšana: Lai izveidotu saikni starp User un ShoppingCart.
$user->shoppingCart()->save($shoppingCart);
Lai izveidotu saikni starp ShoppingCart un User.
$shoppingCart->user()->associate($user)->save();
Viens pret daudziem attiecība
Biznesa definīcija: Lietotājam jāspēj pievienot iepirkumu grozam vairākas preces.
Izstrādātāja definīcija: Modelim ShoppingCart ir vairākas preces (Item).
Diagramma
Attiecības skaidrojums: Item tabulai jāglabā ShoppingCart ID.
Modeļi: Tātad mums vajag divus modeļus: ShoppingCart un Item. Šie modeļi tiks aizpildīti ar šādu kodu:
class ShoppingCart{
/*
* This function defines a relation with Item and tells that ID is stored in
* Item model
*/
public function items()
{
return $this->hasMany(Item::class);
}
}
class Item{
/*
* This function defines a relation with Shopping Cart and tells that ID is stored in
* this model
*/
public function shoppingCart()
{
return $this->belongsTo(ShoppingCart::class);
}
}
Datubāzes migrācija:
Schema::create('shopping_carts', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
# Some more columns which you will need
$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users');
});
Ierakstu saglabāšana: Lai izveidotu saikni starp iepirkumu grozu un precēm.
// Create multiple relations between Thief and Car.
$shoppingCart->items()->saveMany([$item1,$item2]);
// Or use the save() function for single model.
$shoppingCart-> items()->save($item1);
Lai izveidotu saikni starp preci un iepirkumu grozu.
$item->shoppingCart()->associate($shoppingCart)->save();
Polimorfa viens pret daudziem attiecība
Biznesa definīcija: Lietotājs var atstāt vairākas ziņas. Atbalsts (Support) var atstāt vairākas ziņas.
Izstrādātāja definīcija: Modelim User ir daudz ziņu (Message). Modelim Support ir daudz ziņu. Message var piederēt modelim User vai Support.
Diagramma
Attiecības skaidrojums: Message tabulai jāglabā ziņas sūtītāja ID un tips.
Modeļi: Tātad mums vajag trīs modeļus: User, Support un Message. Šie modeļi tiks aizpildīti ar šādu kodu:
class User{
/*
* This function defines a relation with Message and tells that ID and type is stored in
* Message model
*/
public function messages()
{
return $this-> morphMany(Message::class, 'sender');
}
}
class Support{
/*
* This function defines a relation with the Message and tells that ID is stored in
* Message model
*/
public function messages()
{
return $this-> morphMany(Message::class, 'sender');
}
}
class Message{
/*
* This function defines a relation and tells that ID and type is stored in Message model
*/
public function sender()
{
return $this->morphTo();
}
}
Datubāzes migrācija:
Schema::create('users', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('supports', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('messages', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
$table->integer('sender_id')->unsigned()->index()->nullable();
$table->string('sender_type')->nullable();
// or use $table->morphs('sender'); instead of "sender_id" and "sender_type"
});
Ierakstu saglabāšana: Izveidojiet saikni starp sūtītāju (User/Support) un Message.
// Create multiple relations.
$user->messages()->saveMany([$message1,$message2]);
$support->messages()->saveMany([$message1,$message2]);
// Or use the save() function for single model.
$user->messages()->save($message);
$support->messages()->save($message);
Lai izveidotu saikni starp Message un sūtītāju.
$message->sender()->associate($user)->save();
$message-> sender()->associate($support)->save();
Daudzi pret daudziem attiecība
Biznesa definīcija: Precei var būt vairākas birkas (Tag).
Izstrādātāja definīcija: Modelim Item ir daudz birku (Tag). Modelim Tag ir daudz preču (Item).
Diagramma
Attiecības skaidrojums: Tā kā viena birka var piederēt vairākām precēm un vienai precei var būt vairākas birkas, mums vajag trešo tabulu (ko sauc par Pivot tabulu), kura uzskaitīs saikni starp šiem diviem modeļiem.
Modeļi: Tātad mums vajag divus modeļus: Tag un Item. Šie modeļi tiks aizpildīti ar šādu kodu:
class Tag{
public function items()
{
return $this->belongsToMany(Items::class);
}
}
class Item{
public function tags()
{
return $this->belongsToMany(Tag::class);
}
}
Datubāzes migrācija:
Schema::create('items', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('tags', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('item_tag', function (Blueprint $table) {
$table->id();
$table->integer('item_id')->unsigned()->index();
$table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
Ierakstu saglabāšana: Izveidojiet saikni starp Item un Tag
$item->tags()->attach([$tag1->id,$tag2->id]);
// Or use the sync() function to prevent duplicated relations.
$item->tags()->sync([$tag1->id,$tag2->id]);
Vai izveidojiet saikni starp Tag un Item
$tag->items()->attach([$item1->id,$item2->id]);
// Or use the sync() function to prevent duplicated relations.
$tag->items()->sync([$item1->id,$item2->id]);
Polimorfa daudzi pret daudziem attiecība
Biznesa definīcija: Produktiem un ierakstiem (Post) ir vairākas birkas
Izstrādātāja definīcija: Modelim Product ir daudz birku (Tag). Modelim Post ir daudz birku. Tag var izmantot daudzi tagable modeļi (Product un Post)
Diagramma
Attiecības skaidrojums: Tā kā viena birka var piederēt vairākiem ierakstiem vai produktiem, un vienam produktam vai ierakstam var būt vairākas birkas, mums vajag trešo tabulu (ko sauc par Pivot tabulu), kura uzskaitīs saikni starp šiem modeļiem.
Modeļi: Tātad mums vajag trīs modeļus: Product, Post un Tag. Šie modeļi tiks aizpildīti ar šādu kodu:
class Product{
public function tags()
{
return $this->morphToMany(Tag::class, 'tagable');
}
}
class Post{
public function tags()
{
return $this->morphToMany(Tag::class, 'tagable');
}
}
class Tag{
public function products()
{
return $this->morphedByMany(Product::class, 'tagable');
}
public function posts()
{
return $this->morphedByMany(Post::class, 'tagable');
}
}
Datubāzes migrācija:
Schema::create('products', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('posts', function (Blueprint $table) {
$table->id();
# Some more columns which you will need
});
Schema::create('tagables', function (Blueprint $table) {
$table->id();
$table->integer('tagable_id')->unsigned()->index();
$table->string('tagable_type');
// or use $table->morphs(‘tagable’); instead of "tagable_id" and "tagable_type"
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
Ierakstu saglabāšana: Izveidojiet saikni starp tagable modeli (Product vai Post) un Tag.
$product->tags()->saveMany([$tag1, $tag2]);
$post->tags()->saveMany([$tag1, $tag2]);
// Or use the save() function for single model.
$product->tags()->save($tag1);
$post->tags()->save($tag1);
Vai izveidojiet saikni starp Tag un tagable modeli.
$tag->products()->attach([$product1->id,$product2->id]);
$tag->posts()->attach([$post1->id,$post2->id]);
// Or use the sync() function to prevent duplicated relations.
$tag->products()->sync([$product1->id,$product2->id]);
$tag->posts()->sync([$post1->id,$post2->id]);