Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an item from a collection in Laravel 5?

Tags:

php

laravel-5

I'm new to Laravel and I cannot figure out how to get the value of an item from within the returned collection. Here is my code:

$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();

I want to get the value from aircraft.aircraft_id but that's where I get stuck. I've tried:

    $id = $aircraft->aircraft_id;

I've tried:

    $id = $aircraft->aircraft.aircraft_id;

I've also tried:

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

But I'm getting null as a value. When I perform either a var_dump($aircraft) or dd($aircraft) I'm able to confirm that the data is there.

In my view I am able to access the value by looping through, but I need this data in my controller to perform a second query on another table.

Hope that makes sense. Cheers.

EDIT

When running dd($aircraft); I get this:

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
        }
    ]
}
like image 293
Andrew Fox Avatar asked Jun 10 '15 11:06

Andrew Fox


2 Answers

When you use get() method on a query builder it would return an array no matter how many record it gets. So you can access it by pointing it by index. By the way do not forget to check for emptiness to overcome possible errors.

$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;
like image 170
iamawebgeek Avatar answered Oct 14 '22 17:10

iamawebgeek


If you execute the query with get() you will always get a collection as result. Even if you just need one model. The simplest way to change that is to use 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;
like image 22
lukasgeiter Avatar answered Oct 14 '22 19:10

lukasgeiter