【问题标题】:How to select some field and order by on Querying Relationship Existence? Laravel 5.3如何通过查询关系存在来选择一些字段和顺序?拉拉维尔 5.3
【发布时间】:2017-02-25 04:11:11
【问题描述】:

我的代码是这样的:

public function getFavoriteStore($param)
{
    $num = 20;
    $q = $param['q'];
    $location = $param['location'];

    $result = $this->store_repository->whereHas('favorites', function ($query) use($q, $location) {
        $query->select('stores.id', 'stores.name', 'stores.photo','stores.address')
              ->where('stores.status', '=', 1)
              ->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
        if($location)
           $query->where('stores.address', 'like', "%$location%");

        if($q) {
            $query->where('stores.name', 'like', "%$q%")
                  ->where('stores.address', 'like', "%$q%", 'or');
        }
        $query->orderBy('favorites.updated_at', 'desc');
    })->paginate($num);

    return $result;
}

有效

但是,order by 不起作用

此外,上面的查询,我只选择了一些字段。但是当我调试查询时,结果显示所有字段

好像还有错误

有没有人可以帮助我?

我遵循这个教程:https://laravel.com/docs/5.3/eloquent-relationships#querying-relationship-existence

更新

我最喜欢的模特是这样的:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Favorite extends Model
{
    protected $fillable = ['user_id', 'favoritable_id', 'favoritable_type'];
    public function favoritable()
    {
        return $this->morphTo();
    }
}

我的店铺模型是这样的:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Store extends Model
{   
    protected $fillable = ['name', 'address', 'phones', 'address', 'email'];
    public function favorites()
    {
        return $this->morphMany(Favorite::class, 'favoritable');
    }
}

Favorites 表有字段 id、user_id、favoritable_id、favoritable_type

Stores 表包含字段 id、姓名、地址、电话、地址、电子邮件、照片

store和favorite的关系是id(stores表)和favoritable_id(favorites表)

【问题讨论】:

  • 我会使用 Laravel 调试栏查看生成的实际 SQL 并从那里开始。
  • 店铺和收藏有什么关系?

标签: php laravel laravel-5.3 laravel-eloquent


【解决方案1】:

whereHas() 仅用于检查关系是否存在,因此您必须使用join() 对相关表的结果进行排序

$query = $this->store_repository
    ->join('favorites', function ($join) {
        $join->on('stores.id', '=', 'favorites.favoritable_id')
            ->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
    })
    ->where('stores.status', '=', 1)
    ->select('stores.id', 'stores.name', 'stores.photo','stores.address');

if($location)
    $query->where('stores.address', 'like', "%$location%");

if($q) {
    $query->where('stores.name', 'like', "%$q%")
        ->where('stores.address', 'like', "%$q%", 'or');
}

$result = $query->orderBy('favorites.updated_at', 'desc')->paginate($num);

我还没有测试过,但我很确定它会起作用。

【讨论】:

  • 但是,我稍微修改了你的代码。像这样:if($location) $query = $query-&gt;where('stores.address', 'like', "%$location%"); if($q) { $query = $query-&gt;where('stores.name', 'like', "%$q%") -&gt;where('stores.address', 'like', "%$q%", 'or'); } $query = $query-&gt;orderBy('favorites.updated_at', 'desc')-&gt;paginate($num);。因为存在错误:Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
  • 正如我在上一个问题中指出的那样,您不必为每个 where 子句存储 $query 变量。你能再检查一下更新的答案吗?
  • @Amit Gupta,对不起。存在错误:Missing argument 3 for Rinvex\Repository\Repositories\BaseRepository::join()。我用这个:github.com/rinvex/repository
  • @Amit Gupta,我需要你的帮助。看看这个:stackoverflow.com/questions/43780749/…
猜你喜欢
  • 2017-07-16
  • 2015-04-13
  • 2017-08-22
  • 2017-09-21
  • 2022-01-26
  • 2017-10-02
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
相关资源
最近更新 更多