简体   繁体   English

比较两个不同表中的两个不同列,并使用laravel 5.4中的orm关系显示任一表中其他列的数据

[英]Compare two different columns from two different tables and show other column's data from either table using orm relationship in laravel 5.4

I have two models one is Companies and another one is Interviews . 我有两种模式,一种是“ 公司” ,另一种是“ 面试” The Companies table primary key is Company_details_id and this key is used as a foreign key f_company_id in Interviews table. Companies表的主键是Company_details_id,并且此键在Interviews表中用作外键f_company_id

Now my question was how to compare both key values if my condition is true it will return the column company_name from companies table. 现在我的问题是,如果我的条件为true,如何比较两个键值,它将从companies表中返回列company_name

My Companies model: 我的公司模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;


class Companies extends Model
{
 protected $table = 'company_details';

  protected $primaryKey = 'company_details_id';


  public function interviews()
  {
      return $this->hasmany('App\Interviews', 'f_company_id');
  }


}

My Interviews model: 我的访谈模型:

<?php

use Illuminate\Database\Eloquent\Model;


class Interviews extends Model
{
 protected $table = 'interview_schedule';

 protected $primaryKey ='schedule_id';



 public function getCompanies()  
   {
        return $this->belongsto('App/Companies'); 
    }


}

My Controller: 我的控制器:

<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Companies;
use App\Interviews;


class PracticeController extends Controller

{
   public function getAll()
   {

    $getcompany=Companies::where('Companies.company_details_id','=','Interviews.f_company_id')
                 ->select('company_name')->get();

      echo $getcompany; 

   }


}

please, guys, I need a clear answer using with eloquent orm in laravel 5.4 伙计们,请给我一个明确的答案,与laravel 5.4中雄辩的orm一起使用

It's not entirely clear what you are trying to achieve, but here's a few guidelines. 尚不清楚您要实现的目标,但这是一些指导原则。

Firstly you appear to have "one company can have many interviews" and "an interview can only belong to one company" as your relationships. 首先,您的关系看起来像是“一个公司可以有很多面试”和“一个面试只能属于一个公司”。 You have set these up correctly in your models for the most part. 大多数情况下,您已经在模型中正确设置了这些设置。

Because of your relationship, your "getAll" query should simply be: 由于您的关系,您的“ getAll”查询应该简单地是:

$companies = Company::with(['interviews'])->select(['company_name'])->get();

Then you can just loop through them with relative ease: 然后,您可以相对轻松地遍历它们:

foreach($companies as $company)
{
  foreach($company->interviews as $interview) {
    // Do something with $interview
  }
}

Using with calls the relationship "interviews" which performing all of the where clauses between the two tables naturally, based on assumed keys. 使用with调用关系“采访”,该关系根据假定的键自然执行两个表之间的所有where子句。 As you appear to have non-standard primary keys, you'll need to define the hasMany and belongsTo in more detail. 由于您似乎拥有非标准的主键,因此需要更详细地定义hasMany和belongsTo。

public function interviews()
{ 
  return $this->hasMany(Interview::class, 'f_company_id', 'company_details_id');
}

I recommend calling your models Company and Interview, not the plural versions. 我建议将模型称为Company and Interview,而不是复数版本。 This is because a single record deals with a single company (an assumption based on what you wrote). 这是因为单个记录与单个公司打交道(基于您所写内容的假设)。 You'll find it less confusing in the long run. 从长远来看,您会发现它不会造成混乱。

I think you can try this: 我认为您可以尝试以下方法:

 DB::table('interview_schedule')
 ->select('company.company_name')
 ->join('company','company_details.company_details_id','=','interview_schedule.f_company_id')
 ->where('interview_schedule.f_company_id','=','company.company_details_id')
 ->get();

Hope this work for you! 希望这项工作对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM