需要一些关于如何解决此问题的指导:
使用 API 资源的 PHP 7.2、Laravel 5.6
我有一个位置表(位置名称、ID 等)和一个报告表。一(位置)到多(报告)。我正在使用 Searchy 进行位置搜索。当我搜索某个位置时,我希望在结果中包含该位置也可用的所有报告。
我相信这是一个很好的联接案例,但 Searchy 无法进行联接,因此我正在尝试解决这个问题。如果有更好的方法,我洗耳恭听。顺便说一句,我是 Laravel 的新手,所以假设我什么都不知道。
$response = Searchy::locations('location_name')->query($query)->get();
// Add reports to array in each location result
foreach($response->items as $location_row){
$response->items[$location_row->location_id]->reports[] = Searchy::reports('location_id')->query($location_row->location_id)->get();
}
// Return the results
return new LocationResource($response);
我得到的具体错误是 items
数组在 Collection 对象中不可用。确实如此,这只是私有(private)的。这告诉我我做错了。
附录:我使用 Searchy 是因为它的模糊搜索,如果这不是最佳路线并且有更好的替代方案,我完全支持它。
编辑 响应对象(缩写):
object(Illuminate\Support\Collection)#497 (1) {
["items":protected]=>
array(5) {
[0]=>
object(stdClass)#491 (10) {
["id"]=>
int(235)
["locality_id"]=>
string(32) "102938098209384"
["locality_name"]=>
string(7) "AREA 1"
["location_name"]=>
string(10) "Station 10"
["location_id"]=>
string(32) "098098019823909"
["location_address"]=>
string(21) "xx.xx.xx.xx"
["location_last_update"]=>
string(19) "2018-03-30 00:00:00"
["created_at"]=>
string(19) "2018-07-16 16:29:13"
["updated_at"]=>
string(19) "2018-07-16 16:29:13"
["relevance"]=>
string(2) "36"
}
}
}
最佳答案
没有办法测试它,但尝试切换 foreach 循环,如下所示:
$response = Searchy::locations('location_name')->query($query)->get();
// Add reports to array in each location result
foreach($response as $locationKey => &$locationRow){
$locationRow->reports[] = Searchy::reports('location_id')->query($location_row->location_id)->get();
}
// Return the results
return new LocationResource($response);
为了帮助您更好地可视化您的需求,您可以做的一件事是将它们作为数组
使用,并且您需要做的就是添加->toArray()
位于 ->get()
方法末尾(get()
方法是获取数据并返回 集合
的方法之一code>, toArray
将集合
转换为数组
)
编辑:根据 Searchy,您可以操作查询对象,因此我猜您可以添加连接
You can use getQuery() instead of get() to return an instance of the Database Query Object in case you want to do further manipulation to the results
我认为在这种情况下,您可能会从 EagerLoading 中受益。不确定 Searchy 是否允许,但是当涉及到查询时,如果可以的话,我通常选择使用 Laravel 的 ORM,或者使用 QueryBuilder 将它们写出来,这非常简单
https://laravel.com/docs/5.6/eloquent-relationships#eager-loading
假设您有这 2 个模型,位置和报告
class Location extends Model
{
protected $fillable = [/*An array that contains a list of fields of your table*/];
//When you do Locations::create($arrayOfData);, Laravel will do insert into ($fillables) values ($arrayOfData).
//Hidden means they'll be hidden whenever you fetch an object type of Location
protected $hidden = ["created_at","updated_at"];
//Here, we establish a relationship this model has to reports
//You can further down and set custom to what are the foreign keys and local keys on the link above
public function reports()
{
return $this->hasMany("App\Models\Report");
}
}
class Report extends Model {
protected $fillable =['id','location_id',...];
protected $hidden = ["created_at","updated_at"];
public function reports() {
return $this->belongsTo("App\Models\Location");
}
}
查询为:Location::with('report')->get();
关于php - 从两个表嵌套 Laravel API 搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51456025/