以下查询是否正确?

Is the following query is correct one?

我正在使用 laravel eloquent 模型,我想加入多个 table,所以我写了以下查询,但没有得到预期的结果。

而table结构是

resource_type 
  id
  type

communiction_links
  id
  inst_id
  rety_id
  cont_id
  value

contact
  id
  fname
  lname
  image
  park

而查询是

\App\Contact::join('communication_links', 'contacts.id', '=', 'communication_links.cont_id')
    ->join('resource_types','resource_types.id','=','communication_links.rety_id')
    ->select(
        'contacts.id',
        'contacts.image',
        'contacts.fname',
        'contacts.lname',
        'communication_links.value'
    )
    ->where('resource_types.type', 'LIKE', "{mobile}%")
    ->orWhere('communication_links.value', 'LIKE', "{$request->search_string}%")
    ->orWhere('contacts.fname', 'LIKE', "{$request->search_string}%")
    ->orWhere('contacts.lname', 'LIKE', "{$request->search_string}%")
    ->get();

我的理解正确吗?你怎么看?

使用 Eloquent 您可以定义不同 table 之间的关系。

使用您提供的架构,您的 Eloquent 模型将设置如下:

联系 (Contact.php)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * Communication Links Relationship
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function communicationLinks()
    {
        return $this->hasMany(CommunicationLink::class, 'cont_id');
    }
}

资源类型 (ResourceType.php)

namespace App;

use Illuminate\Database\Eloquent\Model;

class ResourceType extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
    /**
     * Communication Links Relationship
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function communicationLinks()
    {
        return $this->hasMany(CommunicationLink::class, 'rety_id');
    }
}

通讯链接 (CommunicationLink.php)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CommunicationLink extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * Contact Relationship
     *
     * @return mixed
     */
    public function contact()
    {
        return $this->belongsTo(Contact::class, 'cont_id');
    }

    /**
     * Resource Types Relationship
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     */
    public function resourceType()
    {
        return $this->belongsTo(ResourceType::class, 'rety_id');
    }
}

Laravel 与模型名称的约定是它们将始终是资源的单数版本,而 table 名称将是复数,例如

table 姓名 : 帖子 型号名称 : Post

您不必遵循此操作,但这意味着您必须在模型中声明 table 名称。此外,由于您的 table 中没有 created_atupdated_at,因此您需要将 public $timestamps = false; 添加到您的模型中。

使用 Eloquent 你上面的查询将变成:

$results = \App\Contact::with('communicationLinks.resourceType')
    ->where('fname', 'LIKE', "{$request->search_string}%")
    ->orWhere('lname', 'LIKE', "{$request->search_string}%")
    ->orWhereHas('communicationLinks.resourceType', function ($q) {
        $q->where('type', 'LIKE', "{mobile}%");
    })
    ->orWhereHas('communicationLinks', function ($q) use ($request) {
        $q->where('value', 'LIKE', "{$request->search_string}%");
    })
    ->get();

如果您不介意来自 resource_types 的信息,您可以更改:

with('communicationLinks.resourceType')

成为:

with('communicationLinks')

更多信息您可以查看文档:

https://laravel.com/docs/5.3/eloquent https://laravel.com/docs/5.3/eloquent-relationships

或者查看这些教程(因为它是旧版本,一些应用程序结构可能有点不同,但 Eloquent 逻辑应该是相同的)

https://laracasts.com/series/laravel-5-fundamentals/episodes/8 https://laracasts.com/series/laravel-5-fundamentals/episodes/14

希望对您有所帮助!