简体   繁体   English

使用 eloquent 和 laravel 从多个 sql 表中获取数据

[英]Get data from multiple sql tables using eloquent with laravel

What i want to achieve?我想达到什么目标?

Show user which pacts he is following.向用户显示他正在遵循的协议。

What I am trying我在尝试什么

I have designed two tables which are 'pacts' & 'pacts_follwers'我设计了两个表,分别是“pacts”和“pacts_follwers”

Table 'pacts' has the details of all the pacts'pacts'包含所有协议详细信息

Table ' pacts_follwers ' has details of users following a particular pact .表 ' pacts_folwers ' 包含遵循特定协议用户的详细信息

These links will give you the images of both the tables这些链接将为您提供两个表的图像

For Schema Refer Images对于架构参考图像契约表

契约_folwers

So how to get pacts that user is following.那么如何获得用户正在遵循的协议。

What I have tried?我尝试过什么?

Sql Query查询

SELECT pacts.*, (SELECT pactsid FROM pacts_follwers WHERE pacts.id = pacts_follwers.pactsid
and pacts_follwers.userid = 2 ) as pactID FROM `pacts`

Sql query Result Sql查询结果在此处输入图片说明

This query will give pactId some value, where the value is null means the user is not following that pact.此查询将为 pactId 提供一些值,其中值为 null 表示用户未遵守该协议。 If this is the solution then i would need Eloquent for this which i am unable to make .如果这是解决方案,那么我将需要 Eloquent,但我无法做到这一点

1st table pacts第一桌协议

id
title
about
created_at
updated_at
pactsImage

2nd table pacts_follwers第二个表pacts_folwers

id
pactsid
userid
created_at
updated_at

Controller Code控制器代码

$pacts = DB::select("SELECT pacts.*, (SELECT pactsid FROM pacts_follwers WHERE pacts.id =
pacts_follwers.pactsid and pacts_follwers.userid = ".Auth::id()." ) as pactID FROM `pacts`");

You need to setup hasManyThrough relationship for User and Pact.您需要为 User 和 Pact 设置hasManyThrough关系。

class User extends Model {

   public function pacts() {
      return $this->hasManyThrough(
           Pact::class,
           PactFollower::class
           'userid',
           'pactsid'
      );
   }
}

I don't fully understand if you want to achieve "get user's all pacts" or "if pact is followed by user".我不完全理解您是要实现“获取用户的所有协议”还是“如果用户遵循协议”。 Either way, you need to setup related relationships.无论哪种方式,您都需要设置相关关系。


Or really simple (and not efficient way)或者真的很简单(而不是有效的方式)

class Pact extends Model {

    public function followers() {
       return $this->hasMany(PactFollower::class, 'pactsid')
    }
}

Now you can use something like现在你可以使用类似的东西

$userIdsForPact = Pact::followers()->pluck('userid');

if ($userIdsForPact->has($user->id)) {
 // your operation
}


Edit: For "if pact is followed by user", you need to setup belongsToThrough relationship.编辑:对于“如果用户遵循协议”,您需要设置belongsToThrough关系。 It doesn't come out of the box with Laravel but staudenmeir/belongs-to-through package should serve you well.它不是 Laravel 开箱即用的,但staudenmeir/belongs-to-through包应该可以很好地为您服务。

After setting the relationship properly, you can use something like this.正确设置关系后,您可以使用这样的东西。

Pact::with('user')->get();

Or add some methods in your Pact model:或者在你的 Pact 模型中添加一些方法:

public function followedByUser($user) {
    return $this->users->has($user);
}

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

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