我允许用户按类型搜索传递给函数:
public function typeSearch($type)
{
$events = Event::where('type', $type)
->with('entities')
->get();
return view('events.searchResultEvents', compact('events'));
}
它的作用几乎与它应该做的一样,它确实检索实体但不是正确的实体,例如:
事件id = 25,entity_id = 2它检索:实体id = 25,而它应该是2.
如何更改此权限以便检索正确的记录?
实施例::
查看图像,Event is = 25且entity_id = 1.entity_id是一个外键,它链接到'实体上的id。表
来自关系我们拥有'实体' id = 25,而它应该是1,因为entity_id = 1
活动模型::
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Event extends Model
{
protected $table = 'events';
public $timestamps = true;
use Searchable;
public function entities()
{
return $this->belongsTo('App\Entity', 'id');
}
public function users()
{
return $this->belongsTo('App\User', 'id');
}
public function events()
{
return $this->belongsTo('App\DirtyEvent', 'id');
}
}
答案 0 :(得分:1)
我认为您的关系方法存在问题。
public function entities()
{
return $this->belongsTo('App\Entity', 'id'); // Ithink you need to change it
}
将第二个参数中的'id'
更改为'entity_id'
public function entities()
{
return $this->belongsTo('App\Entity', 'entity_id');
}
第二个参数是这样使用的,我不知道如何在技术上解释它,但这是一个例子:
$event = Event::find(1);
//this will return the events with id=1 and that data have entity_id=2
$entities = $event->entities;
//If you set the second parameter in your Event model to 'id',
//this $entities variable contain the data from entities table with id=1, because your $event id=1
//But if you set the second parameter to 'entity_id'
//this $entities variable contain the data from entities with the id=2, because your $event entity_id=2