Laravel One to One Eloquent Relationship Example

Apr 26, 2021 . Admin

Hello Friends,

Now let's see example of how to use hasone relationship in laravel. This is a short guide on laravel if hasone relationship. We will use how to use hasone relationship in laravel. Here you will learn how to use hasone relationship in laravel. We will use how to use if hasone relationship in laravel. Let's get started with how to hasone relationship use in laravel.

Here i will give you many example how you can check hasone relationship in laravel.

Create Migrations:

Now we have to create migration of "portfolios" and "portfolioCategories" table. so let's create like as below:

portfolios table migration:

<?php

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

class CreatePortfoliosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('portfolios', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('sub_title');
            $table->integer('portfolio_category_id');
            $table->text('description');
            $table->string('image');
            $table->string('client');
            $table->date('date');
            $table->timestamps();
        });
    }

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

portfolioCategories table migration:

<?php

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

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('portfolio_categories');
    }
}
?>
Create Models:

Here, we will create Portfolio and PortfolioCategory table model. we will also use "hasOne()" for relationship of both model.

Portfolio Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Portfolio extends Model
{
    use HasFactory;

    public $table = 'portfolios';
   
	public $fillable = ['title','sub_title','portfolio_category_id','description'];

	/**
	    * Write code on Method
	    *
	    * @return response()
	*/
	public function PortfolioCategory(){
        return $this->hasOne(PortfolioCategory::class, 'id', 'portfolio_category_id');
	}
}
?>

PortfolioCategory Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Portfolio_Category extends Model
{
    use HasFactory;

    public $table = 'portfolio_categories';
   
	public $fillable = ['name'];
}
?>

Retrieve Records:

$portfolioCategory = Portfolio::find(1)->portfolioCategory;
dd($portfolioCategory);
$portfolio = PortfolioCategory::find(1)->portfolio;
dd($portfolio);

Create Records:

$portfolio = Portfolio::find(1);
 
$portfolioCategory = new PortfolioCategory;
$portfolioCategory->portfolioCategory = '9429343852';
 
$portfolio->portfolioCategory()->save($portfolioCategory);
$portfolioCategory = PortfolioCategory::find(1);
 
$portfolio = Portfolio::find(10);
 
$portfolioCategory->portfolio()->associate($portfolio)->save();

It will help you....

#Laravel 8 #Laravel