如何从Laravel 5中的集合中获取项目?

时间:2015-06-10 11:20:21

标签: php laravel-5

我是Laravel的新手,我无法弄清楚如何从返回的集合中获取项目的值。这是我的代码:

$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
        ->select('aircraft.*', 'shared_aircraft.*')
        ->where('aircraft.owner_id',Auth::user()->owner_id)
        ->where('registration',$reg)
        ->get();

我想从aircraft.aircraft_id获取价值,但那是我遇到的问题。我试过了:

    $id = $aircraft->aircraft_id;

我试过了:

    $id = $aircraft->aircraft.aircraft_id;

我也尝试过:

    $id = array_get($aircraft, 'aircraft_id');

但我将null作为一个值。当我执行var_dump($aircraft)dd($aircraft)我能够确认数据存在时。

在我看来,我可以通过循环访问该值,但我需要在我的控制器中使用这些数据在另一个表上执行第二次查询。

希望这是有道理的。欢呼声。

修改

运行dd($aircraft);时我得到了这个:

Collection {#239 ▼
#items: array:1 [▼
    0 => Aircraft {#240 ▼
            #fillable: array:8 [▶]
            #table: "aircraft"
            #primaryKey: "aircraft_id"
            #connection: null
            #perPage: 15
            +incrementing: true
            +timestamps: true
            #attributes: array:16 [▼
                "aircraft_id" => 5
                "owner_id" => 2
                "shared_aircraft_id" => 67443
                "year" => 2012
                "serial_number" => "C127RG3"
                "registration" => "C-SMTH"
                "created_by" => 2
                "modified_by" => 2
                "created_at" => "0000-00-00 00:00:00"
                "updated_at" => "0000-00-00 00:00:00"
                "aircraft_code" => "2072438"
                "aircraft_mfr" => "CESSNA"
                "aircraft_model" => "172RG"
                "aircraft_type" => 4
                "aircraft_eng" => 1
                "aircraft_cat" => 1
                ]
            #original: array:16 [▶]
            #relations: []
            #hidden: []
            #visible: []
            #appends: []
            #guarded: array:1 [▶]
            #dates: []
            #casts: []
            #touches: []
            #observables: []
            #with: []
            #morphClass: null
            +exists: true
        }
    ]
}

2 个答案:

答案 0 :(得分:4)

当您在get()上使用query builder方法时,无论获得多少记录,它都会返回一个数组。因此,您可以通过索引指向它来访问它。顺便说一下,不要忘记检查空虚以克服可能的错误。

$id = 0;
$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
        ->select('aircraft.*', 'shared_aircraft.*')
        ->where('aircraft.owner_id',Auth::user()->owner_id)
        ->where('registration',$reg)
        ->get();
// now you can access properties
if(count($aircraft) > 0) $id = $aircraft[0]->aircraft_id;

答案 1 :(得分:2)

如果使用get()执行查询,您将始终获得一个集合作为结果。即使你只需要一个型号。最简单的方法是使用first()

$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
    ->select('aircraft.*', 'shared_aircraft.*')
    ->where('aircraft.owner_id',Auth::user()->owner_id)
    ->where('registration',$reg)
    ->first();

$id = $aircraft->aircraft_id;