将模型与表 Laravel 5.2 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34423319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:52:10  来源:igfitidea点击:

Joining Model with Table Laravel 5.2

laraveljoineloquentlaravel-5.1laravel-5.2

提问by Gaurav Mehta

I have created a model in Laravel.

我在 Laravel 中创建了一个模型。

class deals extends Model
{
//
protected $table = 'tbl_deal';
}

Now I want to use this model to retrieve data in my Controller. However, the data extracted from this model also needs to be constrained by a column in another table. Below is the basic schema

现在我想使用这个模型来检索我的控制器中的数据。但是,从该模型中提取的数据还需要受到另一个表中的列的约束。以下是基本架构

tbl_deal

tbl_deal

  • deal_id
  • merchant_id
  • deal_text
  • 交易编号
  • 商户ID
  • 交易文本

tbl_merchant

tbl_merchant

  • merchant_id
  • merchant_url_text
  • 商户ID
  • 商户网址文本

My current Controller has the following code to extract all deals from deals model.

我当前的控制器具有以下代码来从交易模型中提取所有交易。

$deals = deals::all();

I want to constraint the deals by merchant_url_text. Since that is in merchant table, I would need to join it with the existing deal model.

我想通过merchant_url_text 限制交易。由于这是在商家表中,我需要将它与现有的交易模型结合起来。

Any help on how I can do that in my controller and if this is the right way to solve this kind of problem.

关于我如何在我的控制器中做到这一点的任何帮助,以及这是否是解决此类问题的正确方法。

Thanks.

谢谢。

采纳答案by Marcin Nabia?ek

Add to your dealsmodel

添加到您的deals模型

the following function (called relationship):

以下函数(称为关系):

public function merchant()
{
   return $this->belongsTo(Merchant::class, merchant_id);
}

and now you will be able to get all deals with merchant using

现在您将能够使用

$deals = deals::with('merchant')->get();

For example to display the first one use:

例如要显示第一个使用:

$deal = $deals->first();
echo $deal->merchant->merchant_url_text;

However to use this you need to create also Merchantmodel and using this method no joinwill be used.

但是,要使用它,您还需要创建Merchant模型,并且不会使用此方法join

In case you want to use simple join instead of relationship in this case, you can use:

如果您想在这种情况下使用简单连接而不是关系,您可以使用:

$deals = deals::selectRaw('tbl_deal.*, tbl_merchant.merchant_url_text')
              ->leftJoin('tbl_merchant','tbl_deal.merchant_id','=','table_merchant.merchant_id')->get();

And now to display 1st merchant text you can use:

现在要显示第一个商家文本,您可以使用:

$deal = $deals->first();
echo $deal->merchant_url_text;

I strongly recommend to read Laravel documentation to fully understand basic Laravel concepts before writing any code (for example by convention model name should be Dealinstead of dealsand table name should be dealsinstead of tbl_deal, similar for primary key - it should be simple idinstead of deal_id).

我强烈建议在编写任何代码之前阅读 Laravel 文档以完全理解基本的 Laravel 概念(例如,按照约定,模型名称应该是Deal而不是deals表名应该是deals而不是tbl_deal,类似于主键 - 它应该是简单的id而不是deal_id)。