Home > Database > Mysql Tutorial > How to Join Three Tables in Laravel Using Eloquent Relationships?

How to Join Three Tables in Laravel Using Eloquent Relationships?

Patricia Arquette
Release: 2024-11-14 16:48:02
Original
367 people have browsed it

How to Join Three Tables in Laravel Using Eloquent Relationships?

Joining Three Tables Using Laravel Eloquent Model

Introduction

In Laravel, retrieving relational data is simplified with Eloquent models. This article demonstrates how to join three tables using Eloquent to retrieve data from the Articles, Categories, and Users tables.

Database Schema

Consider the following database schema:

Articles Table Categories Table User Table
id id id
title category_name user_name
body
user_type
categories_id

user_id

Objective

The goal is to retrieve articles along with their corresponding category name instead of the category_id, and user name instead of the user_id.

Eloquent Implementation

Define the relationships between the models based on the database schema:

Article.php:

namespace App\Models;

use Eloquent;

class Article extends Eloquent
{
    protected $table = 'articles';

    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }

    public function category()
    {
        return $this->belongsTo('App\Models\Category');
    }
}
Copy after login

Category.php:

namespace App\Models;

use Eloquent;

class Category extends Eloquent
{
    protected $table = "categories";

    public function articles()
    {
        return $this->hasMany('App\Models\Article');
    }
}
Copy after login

User.php:

namespace App\Models;

use Eloquent;

class User extends Eloquent
{
    protected $table = 'users';

    public function articles()
    {
        return $this->hasMany('App\Models\Article');
    }
}
Copy after login

Data Retrieval

To retrieve articles with related data:

$articles = \App\Models\Article::with(['user','category'])->get();
Copy after login

Usage

Access the related data as follows:

// Retrieve user name
$article->user->user_name

// Retrieve category name
$article->category->category_name
Copy after login

Retrieval with Specific Relations

To retrieve articles within a category or by a specific user:

$categories = \App\Models\Category::with('articles')->get();
$users = \App\Models\Category::with('users')->get();
Copy after login

Conclusion

Laravel's Eloquent models greatly simplify the task of retrieving relational data. By understanding the relationships between models and setting them up in your code, you can easily access the necessary data and perform more complex queries efficiently.

The above is the detailed content of How to Join Three Tables in Laravel Using Eloquent Relationships?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template