source

라라벨 웅변 모델의 세 테이블 결합 방법

bestscript 2022. 12. 3. 12:45

라라벨 웅변 모델의 세 테이블 결합 방법

테이블이 세 개 있습니다

기사표

 id
 title
 body
 categories_id
 user_id

카테고리 테이블

  id
  category_name

사용자 테이블

 id
 user_name
 user_type

user_id 대신 category_id 및 user_name 대신 카테고리 이름으로 기사를 표시하고 싶다.이러한 쿼리를 시도합니다.작업입니다!

$articles =DB::table('articles')
                ->join('categories', 'articles.id', '=', 'categories.id')
                ->join('users', 'users.id', '=', 'articles.user_id')
                ->select('articles.id','articles.title','articles.body','users.username', 'category.name')
                ->get();

하지만 나는 웅변적인 방법으로 하고 싶다.제발, 내가 어떻게 하겠어?

웅변을 사용하면 관계형 데이터를 쉽게 검색할 수 있습니다.Larabel 5의 시나리오와 함께 다음 예를 확인하십시오.

3가지 모델이 있습니다.

  1. 기사(사용자 및 카테고리에 속함)

  2. 카테고리(많은 기사가 있습니다)

  3. 사용자(많은 기사가 있음)


  1. 기사.php
    <?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');
        }
    }
  1. 카테고리.php
    <?php
    namespace App\Models;
    
    use Eloquent;
    
    class Category extends Eloquent {
        protected $table = "categories";
    
        public function articles() {
            return $this->hasMany('App\Models\Article');
        }
    }
  1. User.php
    <?php
    namespace App\Models;
    use Eloquent;
    
    class User extends Eloquent {
        protected $table = 'users';
    
        public function articles() {
            return $this->hasMany('App\Models\Article');
        }
    }

모델의 데이터베이스 관계와 설정을 이해해야 합니다.사용자가 많은 기사를 가지고 있습니다.카테고리에는 많은 기사가 있습니다.문서는 사용자와 카테고리에 속합니다.Laravel에서 관계를 설정하면 관련 정보를 쉽게 검색할 수 있습니다.

예를 들어, 사용자 및 카테고리를 사용하여 문서를 가져오려면 다음을 작성해야 합니다.

$article = \App\Models\Article::with(['user','category'])->first();

다음과 같이 사용할 수 있습니다.

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

//retrieve category name 
$article->category->category_name

다른 경우 카테고리 내의 모든 문서를 검색하거나 특정 사용자의 모든 문서를 검색해야 할 수 있습니다.다음과 같이 쓸 수 있습니다.

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

자세한 것은, http://laravel.com/docs/5.0/eloquent 를 참조해 주세요.

시험:

$articles = DB::table('articles')
            ->select('articles.id as articles_id', ..... )
            ->join('categories', 'articles.categories_id', '=', 'categories.id')
            ->join('users', 'articles.user_id', '=', 'user.id')

            ->get();
$articles =DB::table('articles')
                ->join('categories','articles.id', '=', 'categories.id')
                ->join('user', 'articles.user_id', '=', 'user.id')
                ->select('articles.id','articles.title','articles.body','user.user_name', 'categories.category_name')
                ->get();
return view('myarticlesview',['articles'=>$articles]);

언급URL : https://stackoverflow.com/questions/29165410/how-to-join-three-table-by-laravel-eloquent-model