以下查询是正确的吗?

时间:2016-10-22 10:14:25

标签: laravel laravel-5 laravel-5.3

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

表结构

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();

我是否正确地遵循了这一点?你觉得怎么样?

1 个答案:

答案 0 :(得分:1)

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

使用您提供的架构,您的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 (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约定是它们将始终是资源的单数形式,表名将是复数形式。

表格名称:帖子 型号名称:发布

您不必遵循此操作,但这意味着您必须在模型中声明表名。另外,由于您的表格中没有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 Logic应该是相同的)

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

希望这有帮助!