调用 bool 上的成员函数 getRelationExistenceQuery()
Call to a member function getRelationExistenceQuery() on bool
我有三个表 - 位置、行业、接触点。它们与多对多多态关系相连。当我尝试使用此查询按位置过滤我的扇区时
$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');
}
}
有人知道我哪里出错了吗?
您正在从 Sector@locations
返回 bool
。您正在访问 属性 withTimestamps
,一个布尔值,而不是调用 withTimestamps
方法 returns 关系对象。
public function locations()
{
return $this->morphToMany(Location::class, 'weight', 'location_weight')
->withPivot('weight')
->withTimestamps(); // <---- method call
}
您也在 Touchpoint
模型中做同样的事情。
我有三个表 - 位置、行业、接触点。它们与多对多多态关系相连。当我尝试使用此查询按位置过滤我的扇区时
$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');
}
}
有人知道我哪里出错了吗?
您正在从 Sector@locations
返回 bool
。您正在访问 属性 withTimestamps
,一个布尔值,而不是调用 withTimestamps
方法 returns 关系对象。
public function locations()
{
return $this->morphToMany(Location::class, 'weight', 'location_weight')
->withPivot('weight')
->withTimestamps(); // <---- method call
}
您也在 Touchpoint
模型中做同样的事情。