Laravel Eloquent: Relationships (??????? ???????????: ?????????)- Explained in Bangla
?? ??????????????? ???? ??????? ??????????? ORM ?? ??????? ????????? ??????????? ??? ??????? ???? ???? One to One, One to Many, Belongs To, Has One of Many, Has One Through ??? Has Many Through ????????????? ?????? ???? ? ??????? ???? ??? ?????? ?????? ?????? ??? ??? ??????????? ??????? ?? ???? ??????????? ??? ??? ?????? ( Laravel Eloquent: Relationships )
Laravel Eloquent ?? ???????? ??????? ?????????????
?????? ????! ???? ???? ?????? ?? ???? ??????? ??????????? ?????????????? ??????? ?????? ???? ?????? ???? ????? ??? ?? ???? ???? ?????, ?????? ???? ??? ??????? ???? ????? ??? ?? ????? ??? ????????? ??? ??? ????? ?????? ???? ????? ??? ??????? ????????????? ??? ??????? ???!
????? ????, ????????? ??? ??? ???? ?????? ???? ?????? ????? ?????? ?????? ??????? ?????? ???? ??? ???????? ??????????????? ???? ????? ?????? ????? ???? ?? ?? ??????????? ?????? ?????? ??? ????????????? ?????????????? ???????? ??????? ???? ????? ???
???? ??????? ???, ????????? ?? ?????????????? ??? ?????? ?????? ?????? ???? ???? One to One, One to Many ???????? ???? ???? ??????? ????? ???? ??? ???? ?????? ??? ????? ?????? ??? ????
One to One ?????????
??? ???! ???? ????? ??? ?? ??????? ??? ??? One to One ???? ?? ????? ??? ??? ???????? ??? ???? ???? ?? ???? ??? ???? ?????? ???, ??????? ?????? ???? ?????? ?????? ???? ??? ?????? ???????? ?????? ???? One to One ?????????? ??????
???????? ??? ???? ???, ?????? ???? ???? ???? ???? ???:
// Book.php
public function bookInfo()
{
return $this->hasOne(BookInfo::class);
}
// BookInfo.php
public function book()
{
return $this->belongsTo(Book::class);
}
Book ?????? hasOne() ?????? ???? ?? ?? ???? BookInfo ???? ??????? ???? ?? BookInfo ?????? belongsTo() ?????? ???? ?? ??? Book ?????? ???? ??????????
????? ?????? ???? ??? ??? ????:
// ???? ?????? ?? ???? ??????
$book = Book::with('bookInfo')->first();
// ?????? ?????? ?????? ??????
$pageCount = $book->bookInfo->page_count;
One to Many ?????????
??????? ????? ???? ???? ???? ???? One to Many ??????????? ???? ??? ???, ???? ?????? ?????? ?????? ??????? ??? ???? ?? ?????? ?????? ?? ????? ????? ????? ???? ? ?????? ????? One to Many ????????? ?????? ???? ??? ????????
????? ?????? ????:
// Author.php
public function books()
{
return $this->hasMany(Book::class);
}
// Book.php
public function author()
{
return $this->belongsTo(Author::class);
}
Author ?????? hasMany() ?????? ???? ?? ?? ?????? Book ????? ????? ?? Book ?????? belongsTo() ?????? ???? ?? ??? ???? Author ?? ???? ??????????
???? ???? ???? ?????? ??? ??? ????:
// ???? ?????? ?? ?? ??????
$author = Author::with('books')->first();
foreach($author->books as $book) {
echo $book->title;
}
// ???? ?????? ?????? ??? ??????
$book = Book::first();
echo $book->author->name;
Belongs To ?????????
?????? ??????! ?????? ?? One to Many ??????????? ?????? ??? ???? ???? belongs to ????? ??????????? ?????? ???????? ?????? ???, ???? ???? ???? ?????? ?????? ??? ??? ?????? ???? ??? ???????? ??? ????????? ??????? belongs to ?????????? ??????
???? ???? ???:
// Comment.php
public function book()
{
return $this->belongsTo(Book::class);
}
// Book.php
public function comments()
{
return $this->hasMany(Comment::class);
}
Comment ?????? belongsTo() ?????? ???? ?? ??? Book ?????? ???? ?????? ?? Book ?????? hasMany() ?????? ???? ?? ?? ?????? Comment ????? ?????
???? ??? ???? ??????:
// ???? ?????? ?? ?????? ??????
$book = Book::with('comments')->first();
foreach($book->comments as $comment) {
echo $comment->body;
}
// ???? ???????? ?????? ??? ??????
$comment = Comment::first();
echo $comment->book->title;
Has One of Many ?????????
???? ???????????? ??????????? ???? ?????? Has One of Many ???? ?? ??????? ?????, ???? ?????? ??????? ?????? ???? ???? ??? ???? ??? ?????? ?????? ????? ?????? ???? ???????? ????? ??? ??????? ????? ??????? ?????? ???? ??? ???? ??????? ????????? ???? ???? ???? ?? Has One of Many ??????????
// Book.php
public function audioBook()
{
return $this->hasOneThrough(
AudioBook::class,
BookAudioLink::class,
'book_id', // Foreign key on BookAudioLink table
'id', // Foreign key on AudioBook table
'id', // Local key on Book table
'audio_book_id' // Local key on BookAudioLink table
);
}
// BookAudioLink.php
public function book()
{
return $this->belongsTo(Book::class);
}
public function audioBook()
{
return $this->belongsTo(AudioBook::class);
}
// AudioBook.php
public function books()
{
return $this->hasManyThrough(
Book::class,
BookAudioLink::class,
'audio_book_id', // Foreign key on BookAudioLink table
'id', // Foreign key on Book table
'id', // Local key on AudioBook table
'book_id' // Local key on BookAudioLink table
);
}
????? ???? ???? ?????? BookAudioLink ???? ??????? ????? ?? ???? Book ??? AudioBook ???? ?????? ?????? ???? ??????????? ???? ????, ??? ?????? ???? ????? ???? ?????
Has One Through ?????????
???? ????? Has One of Many ???????????? ???? ???? ??????? ??? ????? ???? ??? ???? ????????? ????? Has One Through ???? ?? ?? ???? ?? ??????? ???? ?? ???? ??????? ???? ??? ???? ????? ??? ?????? ???? ??????? ????????
?? ?????? ????? ????? ?????? ???? ????? ????? ????? ???? ??? ????????? ???? ????? ????? ????? ????? ???? Book ??? Rating ?????? ????? Review ?????? ????????
// Book.php
public function rating()
{
return $this->hasOneThrough(
Rating::class,
Review::class,
'book_id', // Foreign key on Review table
'id', // Foreign key on Rating table
'id', // Local key on Book table
'id' // Local key on Review table
);
}
// Review.php
public function book()
{
return $this->belongsTo(Book::class);
}
public function rating()
{
return $this->hasOne(Rating::class);
}
// Rating.php
public function review()
{
return $this->belongsTo(Review::class);
}
????? Book ?????? hasOneThrough() ?????? Review ?????? ??????? Rating ?????? ????? ????? ????? ???? ?? ??????? ?????? ???? ??????? ????? ???? ?????? ???????
Has Many Through ?????????
?? ??? ??????????? ?? Has Many Through? ?? ????? ????? ???? ?????? ?? ??? ???? ??????? ?????? ???? ???? ??????? ?????? ??? ???? ?????? ??????? ???? ?????? ???? ??????? ????????
???????? ?? ?? ???? ????, ???? ???? ???????????? ????????? ?????? ??????? ??????????? ?????? ??? ????? ???? (?????, ??????? ???????)? ?? ??????? ????? ?????? ????? ????? ????? ????? Product, Role ??? User - ?? ????? ?????? ?????? ???? ???? ???? ?????? ???? Has Many Through ??????????? ?????? ????? ?????? ??? ??????? ??????? ??? ??????
// ProductRole.php
public function product()
{
return $this->belongsTo(Product::class);
}
public function role()
{
return $this->belongsTo(Role::class);
}
// Role.php
public function products()
{
return $this->hasManyThrough(
Product::class,
ProductRole::class,
'role_id', // Foreign key on ProductRole table
'id', // Foreign key on Product table
'id', // Local key on Role table
'product_id' // Local key on ProductRole table
);
}
public function users()
{
return $this->belongsToMany(User::class);
}
????? Product ?????? hasManyThrough() ?????? ProductRole ?????? ??????? Role ?????? ?????? ?? ?????? ????? ??????? Role ????? hasManyThrough() ?????? ??????? Product ?????? ?????? ?? ?????? ???? ?????
??? ???? ???? ??????? ???? ????? ????? ????????? ??????????? ????????????? ???? ???? ???? ????? ??? ????? ???? ??? ???? ????, ???? ??????? ????????? ? ????????? ??????? ?????????? ????? ???????
???? ?????? ???
????? ???? ??? ?????? ?????? ?? ?? ?? ????????????? ??????? ??? ???? ????? ????? ????:
// ???? ?????? ?? ?? ??????
$author = Author::with('books')->first();
foreach($author->books as $book) {
echo $book->title;
}
// ???? ?????? ???? ??? ????????? ???? ??????
$book = Book::with('author', 'publisher')->first();
echo $book->author->name;
echo $book->publisher->name;
// ???? ?????? ????? ?????? (Has One Through ?????????)
$book = Book::with('rating')->first();
echo $book->rating->value;
// ???? ??????????? ?? ??? ??? ??? ???????? ???????? ?????? ?????? (Has Many Through ?????????)
$product = Product::with('roles.users')->first();
foreach($product->roles as $role) {
echo $role->name;
foreach($role->users as $user) {
echo $user->name;
}
}
?? ???????????? ?????? ?????? ?????? ???? ????????? with() ?????? ???????? ??? ???? ?????? ????????????? ??? ????? ????????? ??? ???? ????? ??? ????????? ??????? ???? ???? ??????? ??? ??? ???? ???? ???? ??? ???? ???????? ???? ????????
??????
???! ???? ????? ?????? ??? ?? ????????????? ???? ???? ???? ???? ??? ????????????? ????? ????? ???? ????? ?????????????? ??????? ???????????? ??? ?????????? ?????? ???? ??????? ?? ??? ???? ???? ????? ??? ????????????? ??? ??? ???? ?????
????? ??? ????? ??? ??? ?? ?????????????? ??????? ??? ??? ????? ?????????? ?????????? ??? ?????? ????? ???? ???? ????? ??????? ???? ??????? ??? ??????? ?? ????? ??? ??????????? ??? ??????? ???? ???????? ??? ?? ????!