Skip to main content

Laravel One to One Eloquent Relationship Tutorial

laravel hasone relationship

Hi Dev,

Today,I will learn you 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 "Products" and "ProductCategories" table. so let's create like as below:

Products table migration:


<?php

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

class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('Products', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('sub_title');
$table->integer('product_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('Products');
}
}
?>

ProductCategories table migration:


<?php

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

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

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

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

Product Model:


<?php

namespace App\Models;

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

class Product extends Model
{
use HasFactory;

public $table = 'Products';

public $fillable = ['title','sub_title','product_category_id','description'];

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

ProductCategory Model:


<?php

namespace App\Models;

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

class Product_Category extends Model
{
use HasFactory;

public $table = 'Product_categories';

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

Retrieve Records:


$ProductCategory = Product::find(1)->ProductCategory;
dd($ProductCategory);

$Product = ProductCategory::find(1)->Product;
dd($Product);

Create Records:


$Product = Product::find(1);

$ProductCategory = new ProductCategory;
$ProductCategory->ProductCategory->name = 'Mobile';

$Product->ProductCategory()->save($ProductCategory);

$ProductCategory = ProductCategory::find(1);

$Product = Product::find(10);

$ProductCategory->Product()->associate($Product)->save();

It will help you....

Comments

Popular posts from this blog

Laravel Validation Image Example

Hi Guys, Today, I will learn you to create validation image in laravel.we will show example of laravel validation image.The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp). Here, I will give you full example for simply image validation in laravel bellow. solution $request->validate([ 'file' => 'image' ]); Route : routes/web.php Route::get('form/create','FromController@index'); Route::post('form/store','FromController@store')->name('form.store'); Controller : app/Http/Controllers/BlogController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Blade; use App\Models\User; use App\Models\Post; class FromController extends Controller { /** * Write code on Method * * @return response() */ public function create() { return view('form'); } /** * Write code on Method ...

Laravel Get Headers From Request Example

Hi Dev, In this blog ,I Will learn you how to get header in laravel. I will show example of laravel get headers from request you can easliy laravel get all headers from request. I will explain laravel get all headers. I can blow you can easily get headers from request in laravel this example. Exmaple:1 Here In this exmaple laravel Get headers from request to a header method /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index(Request $request) { $headers = $request->header('connection'); dd($headers); } Output : "keep-alive" Exmaple:2 Here In this exmaple laravel get headers from request using a request builder to a header method /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { $headers = \Request::header(); dd($headers); } Output : array:13 [ "host" => array:1 [ 0 => "localho...

PHP Upload File Using Ckeditor Example - Itwebtuts

Hii Developer, In this example,I am going to show you How to do upload custom file with CKEDITOR in PHP application. In this tutorial i explain step by step example code of How to image upload with CKEDITOR php. You can see simple example of php ckeditor custom image upload using browse button. Here i give you full example of How to How to do custom file upload with in CKEDITOR step by step like create one file in this file we are integrate CKEDITO and secound another file which we are created for uploadin custom file. Following The Step: 1)create one index.php file 2)create cstom file uploading file Step:1 Create one index.php file Now we have download CKEDITOR in our local PC. then we are create our index.php file and this file we are use CKEDITOR <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="robots" content="noindex, nofollow"> <title>Ckeditor File Upload</title> ...