【问题标题】:Call to a member function getRelationExistenceQuery() on bool在 bool 上调用成员函数 getRelationExistenceQuery()
【发布时间】:2022-01-02 05:37:15
【问题描述】:

我有三个表格 - 位置、部门、接触点。它们与多对多的多态关系相连。当我尝试使用此查询按位置过滤我的扇区时

$sectors = Sector::whereHas('locations', function($q) use ($id) {
            $q->where('location_id', $id);
        })->get();

Laravel 抛出此错误 - “调用 bool 上的成员函数 getRelationExistenceQuery()”

这驻留在一个名为 SectorController 的控制器中

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\SectorRequest;
use Illuminate\Http\Request;
use App\Models\Location;
use App\Models\Sector;

class SectorController extends Controller
{
   public function byLocation(Location $location)
     {
        $breadcrumbs = [
            ['link' => "/", 'name' => "Home"], 
            ['link' => "javascript:void(0)", 'name' => "Admin"], 
            ['link' => "/admin/customers", 'name' => "Customers"],
            ['name' => "Locations"],
            ['name' => "Sectors"]
        ];

        $id = $location->id;

        $sectors = Sector::whereHas('locations', function($q) use ($id) {
            $q->where('location_id', $id);
        })->get();
        
        return view('admin.sectors.index', [
            'sectors' => $sectors, 
            'breadcrumbs' => $breadcrumbs
        ]);
    }

部门模型

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Sector extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'description',
        'sort_order', 
        'req_pics', 
        'req_recs'
    ];

    public function locations()
    {
        return $this->morphToMany(Location::class, 'weight', 'location_weight')
                    ->withPivot('weight')
                    ->withTimestamps;
    }
}

接触点模型

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Touchpoint extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name','description','sort_order', 'req_pics', 'req_recs', 'sector_id'
    ];

    public function audit_sector_touchpoints()
    {
        return $this->belongsToMany(Audit_Sector_Touchpoint::class);
    }

    public function services()
    {
        return $this->belongsToMany(Service::class)
                    ->withPivot('weight')
                    ->withTimestamps();
    }

    public function locations()
    {
        return $this->morphToMany(Location::class, 'weight', 'location_weight')
                    ->withPivot('weight')
                    ->withTimestamps;
    }
}

位置模型

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    class Location extends Model
    {
        use HasFactory;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var string[]
         */
        protected $fillable = [
            'name',
            'customer_id',
            'service_id',
            'address1',
            'address2',
            'user_id',
            'phone',
            'city_id',
            'state_id',
            'country_id',
            'postcode',
            'latitude',
            'longitude'
        ];
    
        public function audits()
        {
            return $this->belongsToMany('App\Models\Audit');
        }
    
        public function sectors()
        {
            return $this->morphedByMany(Sector::class, 'weight', 'location_weight');
        }
    
        public function touchpoints()
        {
            return $this->morphedByMany(Touchpoint::class, 'weight', 'location_weight');
        }
    }

有人知道我哪里出错了吗?

【问题讨论】:

    标签: eloquent many-to-many laravel-8 polymorphic-associations


    【解决方案1】:

    您正在从Sector@locations 返回一个bool。您正在访问属性withTimestamps,一个布尔值,而不是调用返回关系对象的方法withTimestamps

    public function locations()
    {
        return $this->morphToMany(Location::class, 'weight', 'location_weight')
            ->withPivot('weight')
            ->withTimestamps(); // <---- method call
    }
    

    您也在 Touchpoint 模型中做同样的事情。

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2015-09-07
      相关资源
      最近更新 更多