Eloquent Relationships

Eloquent Relationships

Instructor-svgAl-Mamun Sarkar
Jul 02 , 2020
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram

এই লেসনে আমরা দেখবো লারাভেল এ কিভাবে Eloquent Relationships ব্যবহার করতে হয়।Eloquent Relationships ব্যবহার করে কিভাবে One-to-One, One-to-Many, Many-to-Many নিয়ে কাজ করতে হয়। 

 

Database ERD

 

Source Code:

 

CreatePostsTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->foreignId('user_id');
            $table->text('description');
            $table->boolean('status');
            $table->timestamps();
        });
    }

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

CreateAddressTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAddressTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('address', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id');
            $table->string('state')->nullable();
            $table->string('city')->nullable();
            $table->string('country')->nullable();
            $table->timestamps();
        });
    }

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

CreateTagsTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTagsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->timestamps();
        });
    }

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

CreatePostTagTable:


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostTagTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('post_tag', function (Blueprint $table) {
            $table->foreignId('post_id');
            $table->foreignId('tag_id');
        });
    }

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

 

Routes:

use Illuminate\Support\Facades\Route;
use App\Post;
use App\User;
use App\Address;
use App\Tag;


Route::get('one-to-one', function() {
	$user = User::find(2);
	echo $user->name . '<br/>';
	echo $user->email . '<br/>';
	echo $user->address->country . '<br/>';
});


Route::get('one-to-one-inverse', function() {
	$address = Address::find(2);
	echo $address->city . '<br/>';
	echo $address->user->name . '<br/>';

	dd($address->user);
});


Route::get('one-to-many', function() {
	$user = User::find(1);
	echo $user->name . '<br/>';
	
	foreach ($user->posts as $post) {
		echo $post->title . '<br/>';
	}
});

Route::get('one-to-many-inverse', function() {
	$post = Post::find(17);
	echo $post->title . '<br/>';
	echo $post->user->name . '<br/>';
	
	// $post->user->name  = 'Mamun Sarkar';
	// $post->user->save();

	dd($post->user);
});


Route::get('many-to-many', function() {
	$post = Post::find(2);
	echo $post->title . '<br/>';

	echo '<h2> Tags </h2>';
	foreach ($post->tags as $tag) {
		echo $tag->title . '<br/>';
	}

	// dd($post->tags);
});


Route::get('many-to-many-inverse', function() {
	$tag = Tag::find(1);
	echo $tag->title . '<br/>';

	echo '<h2> Posts </h2>';
	foreach ($tag->posts as $post) {
		echo $post->title . '<br/>';
	}
	// dd($tag->posts);
});

Route::get('attach', function() {
	$post = Post::find(1);
	$post->tags()->attach([1, 2]);
});

Route::get('sync', function() {
	$post = Post::find(1);
	$post->tags()->sync([1, 2]);
});

Route::get('detach', function() {
	$post = Post::find(1);
	$post->tags()->detach();
});