连接模型与表Laravel 5.2 [英] Joining Model with Table Laravel 5.2

查看:161
本文介绍了连接模型与表Laravel 5.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel创建了一个模型。

 类交易扩展模型
{
//
protected $ table ='tbl_deal';
}

现在我想使用此模型在我的Controller中检索数据。然而,从该模型中提取的数据也需要被另一个表中的列约束。以下是基本架构



tbl_deal




  • deal_id

  • merchant_id

  • deal_text



tbl_merchant




  • merchant_id

  • merchant_url_text



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

  $ deals = deals :: all(); 

我想通过merchant_url_text限制交易。因为这是在商家表中,我需要加入现有的交易模式。



任何帮助我如何在我的控制器中做到这一点,如果这是解决方案



谢谢。 >添加到您的交易模型



以下函数(称为关系):

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

现在,您将能够使用

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

例如显示第一个使用:

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

不过要使用这个,您还需要创建商家模型并使用这种方法不会使用 join



如果你想使用简单连接在这种情况下,您可以使用:

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

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

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

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


I have created a model in 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

  • deal_id
  • merchant_id
  • deal_text

tbl_merchant

  • merchant_id
  • merchant_url_text

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.

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.

解决方案

Add to your deals model

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 Merchant model and using this method no join will be used.

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 Deal instead of deals and table name should be deals instead of tbl_deal, similar for primary key - it should be simple id instead of deal_id).

这篇关于连接模型与表Laravel 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆