laravel 5.3关系中的问题?

时间:2016-10-24 11:37:46

标签: laravel laravel-5.3

我想根据姓名或手机号在联系人表和通讯链接表中搜索。我的手机在'value'中的communication_links表中,fname和lname在contacts表中。但是通过使用rest_id在通信链接和resource_status中存在关系。并且resource_status再次与资源类型相关联。

这是我的原始SQL查询

SELECT  contacts.fname, contacts.lname, communication_links.value FROM contacts ,communication_links,resource_status 
                    where 
                    (contacts.id = communication_links.cont_id)
                    AND
                    (communication_links.rest_id = resource_status.id)
                    AND
                    resource_type.status LIKE '{mobile}%' 
                    AND 
                   (communication_links.value LIKE '{$search_string}%' OR 
                    contacts.fname LIKE '{$search_string}% OR contacts.lname LIKE '{$search_string}%  )

我有以下表结构

contacts
  id
  fname
  lname
  dob

communication_links
  id
  rest_id
  cont_id
  value

resource_type
  id
  type

resource_status
  id
  Status
  rety_id

我创建了以下模型以维持关系

Contact.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $connection = 'mysql_freesubs';

    public $timestamps = false;

    public function communicationLinks()
    {
        return $this->hasMany(Communication_link::class, 'cont_id');
    }
}

Communication_link.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Communication_link extends Model
{
    protected $connection = 'mysql_freesubs';


    public $timestamps = false;

    public function resource()
    {
        return $this->belongsTo(Resource_status::class, 'rest_id');
    }

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

Resource_status.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Resource_status extends Model
{
    protected $connection = 'mysql_freesubs';
    protected $table = 'resource_status';


}

Resource_type.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Resource_type extends Model
{
     protected $connection = 'mysql_freesubs';

    public $timestamps = false;

    public function resourceStatuses()
    {
        return $this->hasMany(Resource_status::class, 'rety_id');
    }
}

我尝试编写与上面给出的相同的查询,但我没有得到满意的结果:

$parent_contact = \App\Contact::with('communicationLinks.resource')
                                            ->whereHas('communicationLinks.resource', function ($query) {
                                                    $query->where('status', 'LIKE', "{mobile}%");
                                                })

                                            ->whereHas('communicationLinks.contact',function ($query) use($request) {
                                                $query->where('communicationLinks.value','LIKE',"{$request->search_string}%")
                                                      ->orWhere('fname','LIKE',"{$request->search_string}%")
                                                      ->orWhere('lname','LIKE',"{$request->search_string}%");
                                                })
                                             ->get();

1 个答案:

答案 0 :(得分:2)

   $query = DB::table("contacts as a")
              ->select(array("a.fname", "b.lname", "b.value"))
              ->join("communication_links AS b", "a.id", "=", "b.cont_id")
              ->join("resource_type AS c", "b.rest_id", "=", "c.id")
              ->where("a.status", "like", $mobile."%")
              ->where("b.value", "like", $search_string."%")
              ->orWhere("a.fname", "like", $search_string."%")
              ->orWhere("a.lname", "like", $search_string."%")
              ->get();

那应该为你做好工作。

您可以遍历它以获得所需的值