Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php L5 |关系修补程序查询_Php_Laravel_Laravel 5 - Fatal编程技术网

Php L5 |关系修补程序查询

Php L5 |关系修补程序查询,php,laravel,laravel-5,Php,Laravel,Laravel 5,我已经使用以下模型和步骤建立了一对多关系。一个作业可以有许多步骤,步骤属于作业。当我去查询与Job::with('steps')->find(1)的关系时,结果与集合的预期结果一样显示 <MariasApp\Job #000000002b07ae180000000036b76e17> { id: 1, number: 59221, customer_id: 5, user_id: 17, created_at: "

我已经使用以下模型和步骤建立了一对多关系。一个作业可以有许多步骤,步骤属于作业。当我去查询与
Job::with('steps')->find(1)
的关系时,结果与集合的预期结果一样显示

<MariasApp\Job #000000002b07ae180000000036b76e17> {
       id: 1,
       number: 59221,
       customer_id: 5,
       user_id: 17,
       created_at: "2015-03-24 01:32:20",
       updated_at: "2015-03-24 01:32:20",
       steps: <Illuminate\Database\Eloquent\Collection #000000002b07ae070000000036b76e17> [
           <MariasApp\Step #000000002b07ae7b0000000036b76e17> {
               id: 37,
               job_id: 1,
               body: "Eligendi reiciendis ratione labore sed.",
               created_at: "2015-03-24 01:32:21",
               updated_at: "2015-03-24 01:32:21"
           }
       ]
   }
{
id:1,
电话:59221,
客户识别号:5,
用户id:17,
创建时间:“2015-03-2401:32:20”,
更新日期:“2015-03-2401:32:20”,
步骤:[
{
身份证号码:37,
工作编号:1,
正文:“劳动合理化”,
创建时间:“2015-03-2401:32:21”,
更新地址:“2015-03-2401:32:21”
}
]
}

但是当我运行
Job::with('steps')->find(1)->body
时,我得到一个响应
null
。我有没有办法只从关系中提取
正文

我假设您正在尝试从步骤集合中获取
正文
。您需要指定要使用的步骤。要么使用第一个

Job::with('steps')->find(1)->steps->first()->body

或者像这样的东西在它们之间循环

$steps= Job::with('steps')->find(1)->steps->all();

foreach($steps as $step){
    echo step->body;
 }
或者循环完成许多工作,并获得每个工作的第一步:

$jobs = Job::with('steps')->get();

foreach($jobs as $job){
  echo $jobs->steps->first()->body;
}

就是这样,但我有一个包含50个作业的表,所以我不想将结果限制为一个作业ID。这似乎不起作用
Job::with('steps')->get()->steps->first()->body