Php 如何在Laravel 5.2中更深入地使用雄辩的hasMany?
我正试图从数据库中获得一种结果收集,仍然利用了雄辩的优势 我有一个叫做“检查”的表,一个叫做“绿色区域”的表和一个叫做“资产”的表 每次检查都有一个单独的绿色区域,每个绿色区域都有许多资产 当我获取检查时,我会这样做:Php 如何在Laravel 5.2中更深入地使用雄辩的hasMany?,php,laravel,eloquent,foreign-keys,Php,Laravel,Eloquent,Foreign Keys,我正试图从数据库中获得一种结果收集,仍然利用了雄辩的优势 我有一个叫做“检查”的表,一个叫做“绿色区域”的表和一个叫做“资产”的表 每次检查都有一个单独的绿色区域,每个绿色区域都有许多资产 当我获取检查时,我会这样做: Route::get('inspections', ['middleware' => 'cors', function() { $isp = new \App\Inspection(); return $isp
Route::get('inspections', ['middleware' => 'cors', function()
{
$isp = new \App\Inspection();
return $isp
->with('greenAreas')
->with('assets') // where each green area has its own set of assets
->get();
}]);
检验模型
绿地模型
路线
现在我想做这样的事情:
Route::get('inspections', ['middleware' => 'cors', function()
{
$isp = new \App\Inspection();
return $isp
->with('greenAreas')
->with('assets') // where each green area has its own set of assets
->get();
}]);
如评论中所述,我希望获得该绿地的所有资产,然后是该检查的所有绿地
我该怎么做?
谢谢
这将获取由Inspection$id检查的绿地的所有资产
我不确定您要查找哪个查询。所有模型类和关系的第一个将这样定义,请确保用表中正确的外键替换关系函数中的外键
namespace App; //model class for inspection
use Illuminate\Database\Eloquent\Model;
class Inspection extends Model
{
protected $table = 'as_inspections';
public function greenArea()
{
return $this->belongsTo('App\GreenArea', 'greenareaid','id');
}
}
namespace App; //model for green area
use Illuminate\Database\Eloquent\Model;
class GreenArea extends Model
{
protected $table = 'as_green_areas';
public function inspection()
{
return $this->hasOne('App\Inspection', 'greenareaid','id');
}
public function assets()
{
return $this->hasMany('App\Asset', 'greenareaid','id');
}
}
namespace App; //model for asset
use Illuminate\Database\Eloquent\Model;
class Asset extends Model
{
protected $table = 'as_assets';
public function greenArea()
{
return $this->belongsTo('App\GreenArea', 'greenareaid','id');
}
}
然后,您可以使用eloquent的急切加载,轻松地获得以下示例中的相关模型。。传递检验模型的主键以查找方法,然后为其获取绿色区域和相关资产
Inspection:find(1)->with('greenArea.assets');
这是一个AJAX请求,移动应用程序需要API提供一组关于检查的信息。而且它没有通过任何ID。所以我需要得到所有的检查,每次检查相关的绿色区域,以及每个绿色区域的所有资产。我甚至不知道如何用('greenArea.assets')搜索这个:/
Inspection::all()->代码>应该工作检查::find($id)->greenAreas()->first(),这不正确,当关系定义为hasone时,您不需要调用first(),您好!谢谢你的回答!我目前正在尝试实现它。只有一个问题。greenareaid、inspectionid是外键吗?在as_inspections表中,我有一个名为green_area_fk的fk,我应该将inspectionid与green_area_fk交换,还是将id保留为inspections表的主键?我对这些方法的可选参数感到非常困惑…它起作用了!谢谢如果你有时间链接到我的一些资源,以了解更多关于像hasMany等雄辩方法的可选参数,这将是惊人的!请看我上面更新的代码,我简单地更改了belongsTo方法,在您的情况下,您可以这样称呼它,您有一个绿色区域的检查,因为您在检查表中保留外键,所以请始终注意,belongsTo关系是在外键所在的位置定义的。。。最好的教程是Laravel.com,文档,我从一年多前就开始参考它,并完成了一个ERP系统……现在它变得更有意义了!谢谢现在我知道greenareaid是inspections表中的外键,id是green_areas表的主键!是吗?是的,你是正确的,因为它的1-1关系,你也可以用其他方式定义,将InputId放在greenarea表中,任何一种选择都没有区别,它取决于你必须放在相应表中的情况
Inspection::find($id)->greenAreas()->assets()->all();
namespace App; //model class for inspection
use Illuminate\Database\Eloquent\Model;
class Inspection extends Model
{
protected $table = 'as_inspections';
public function greenArea()
{
return $this->belongsTo('App\GreenArea', 'greenareaid','id');
}
}
namespace App; //model for green area
use Illuminate\Database\Eloquent\Model;
class GreenArea extends Model
{
protected $table = 'as_green_areas';
public function inspection()
{
return $this->hasOne('App\Inspection', 'greenareaid','id');
}
public function assets()
{
return $this->hasMany('App\Asset', 'greenareaid','id');
}
}
namespace App; //model for asset
use Illuminate\Database\Eloquent\Model;
class Asset extends Model
{
protected $table = 'as_assets';
public function greenArea()
{
return $this->belongsTo('App\GreenArea', 'greenareaid','id');
}
}
Inspection:find(1)->with('greenArea.assets');