Eloquent Has One Through Relationship Tutorial in Laravel 11
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
// $users = User::get();
// return $users;
// To show All Users details as shown below :
// $users = User::with("company")->with("phoneNumber")->get();
// return $users;
// To show particular User details as shown below :
// $users = User::with("company")->with("phoneNumber")->find(2);
// return $users;
// $users = User::with("company")->with("phoneNumber")->find(2);
// echo $users->name . "<br>";
// echo $users->company->company_name . "<br>";
// echo $users->phoneNumber->number;
// show All users data in proper format as shown below :
$users = User::with("company")->with("phoneNumber")->get();
foreach ($users as $data) {
echo "<b>Name : </b>" . $data->name . "<br>";
echo "<b>Company Name : </b>" . $data->company->company_name . "<br>";
echo "<b>Company Phone Number : </b>" . $data->phoneNumber->number . "<br>";
echo "<hr>";
}
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Above File is app\Http\Controllers\UserController FileBelow File is app\Http\Controllers\CompanyController File
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CompanyController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Below File is app\Models\User File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public $timestamps = false;
protected $table = "users";
protected $fillable = ["name", "email"];
public function company()
{
return $this->hasOne(Company::class);
}
public function phoneNumber()
{
return $this->hasOneThrough(Phone_number::class, Company::class);
}
}
Below File is app\Models\Company File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
public $timestamps = false;
protected $table = "companies";
protected $fillable = ["company_name", "user_id"];
}
Below File is app\Models\Phone_number File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Phone_number extends Model
{
public $timestamps = false;
protected $table = "phone_numbers";
protected $fillable = ["number", "company_id"];
}
Below File is database\json\users File
[
{
"name": "Yahoo Baba",
"email": "yahoobaba@gmail.com"
},
{
"name": "Salman Khan",
"email": "salman@gmail.com"
},
{
"name": "Deepika Padukone",
"email": "deepika@gmail.com"
},
{
"name": "Abhishek Bachchan",
"email": "abhishek@gmail.com"
}
]
Below File is database\json\companies File
[
{
"company_name": "Tesla",
"user_id": 1
},
{
"company_name": "Google",
"user_id": 2
},
{
"company_name": "Microsoft",
"user_id": 3
},
{
"company_name": "Facebook",
"user_id": 4
}
]
Below File is database\json\phone_number File
[
{
"number": "55223366",
"company_id": 1
},
{
"number": "77116688",
"company_id": 2
},
{
"number": "88997711",
"company_id": 3
},
{
"number": "33884411",
"company_id": 4
}
]
Below File is database\migrations\2024_12_11_095523_create_users_table File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->string("email", 50)->unique();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
Below File is database\migrations\2024_12_11_100419_create_companies_table File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->string("company_name");
$table->unsignedBigInteger("user_id");
$table->foreign("user_id")->references("id")->on("users")->onUpdate("cascade")->onDelete("cascade");
// Second way for Making Foreign key as shown below :
// $table->foreignId("user_id")->constrained("users")->onUpdate("cascade")->onDelete("cascade");
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('companies');
}
};
Below File is database\migrations\2024_12_11_101616_create_phone_numbers_table File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('phone_numbers', function (Blueprint $table) {
$table->id();
$table->string("number", 15)->unique();
$table->unsignedBigInteger("company_id");
$table->foreign("company_id")->references("id")->on("companies")->onUpdate("cascade")->onDelete("cascade");
// Second way for making Foreign key as shown below :
// $table->foreignId("company_id")->constrained("companies")->onUpdate("cascade")->onDelete("cascade");
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('phone_numbers');
}
};
Below File is database\seeders\DatabaseSeeder File
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
// User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call([
UserSeeder::class,
CompanySeeder::class,
PhoneNumber::class
]);
}
}
Below File is database\seeders\UserSeeder File
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$json = File::get(path: "database/json/users.json");
$users = collect(json_decode($json));
$users->each(function ($user) {
User::create([
"name" => $user->name,
"email" => $user->email
]);
});
}
}
Below File is database\seeders\CompanySeeder File
<?php
namespace Database\Seeders;
use App\Models\Company;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
class CompanySeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$json = File::get(path: "database/json/companies.json");
$companies = collect(json_decode($json));
$companies->each(function ($company) {
Company::create([
"company_name" => $company->company_name,
"user_id" => $company->user_id
]);
});
}
}
Below File is database\seeder\PhoneNumberSeeder File
<?php
namespace Database\Seeders;
use App\Models\Phone_number;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
class PhoneNumberSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$json = File::get(path: "database/json/phone_numbers.json");
$phone_numbers = collect(json_decode($json));
$phone_numbers->each(function ($phone_number) {
Phone_number::create([
"number" => $phone_number->number,
"company_id" => $phone_number->company_id
]);
});
}
}
Below File is routes\web File
.png)
.png)
.png)
.png)
Comments
Post a Comment