0

I'm trying something really simple and yet doesn't work. I have one controller where selecting from one database to show some info for user. Now I'm trying in this same controller to select from second table to show some other info but I get Undefined variable...

This is the part of controller which is problematic

   public function orderView( $orderId, $userId ) {

    $order = self::$user->orders()->where('order_id', $orderId)->first();
    $keys = Keys::all();

    if (!$order) {
        App::abort(404);
    }

    $userID         = $order['user_id'];
    $orderID        = $order['order_id'];
    $public_key     = $keys['public_key'];
    $private_key    = $keys['private_key'];                 

    $options = array(
            "public_key"  => $public_key,
            "private_key" => $private_key,             
            "orderID"     => $orderID,      
            "userID"      => $userID    
    );

What I have added here is

 $keys = Keys::all();
 $public_key        = $keys['public_key'];
 $private_key   = $keys['private_key'];
 ....
 "public_key"  => $public_key,
 "private_key" => $private_key,

The error is Undefined index: public_key

1
  • Because your Keys::all returns an array of eloquent objects. In your case it would be $keys[0]['public_key']. Assuming it's the first result. You should instead use a different query to return only the key that you need. Commented Jun 30, 2016 at 12:28

1 Answer 1

2

Keys::all() returns an Illuminate\Database\Eloquent\Collection.

In order to access a single item of the Collection, you either must iterate the collection and access them singularly, or take one item specifically with Collection's functions, such as first().

For example:

public function orderView($orderId, $userId)
{
    $order = self::$user->orders()->where('order_id', $orderId)->first();
    $keys = Keys::all();

    if (! $order) {
        App::abort(404);
    }

    $options = [];

    foreach ($keys as $key)
    {
        $options[] = [
            'public_key'  => $key->public_key,
            'private_key' => $key->private_key,
            'userID'      => $order->user_id,
            'orderID'     => $order->order_id
        ];
    }

    return $options;
}

You can find more information about Illuminate Collection methods here.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, thank's for the answer. This work like this and return $options return correct info but I use this option right after this in a new class $box = new Box ($options); but I get that Box PUBLIC KEY - cannot be empty
I'd need to see the code of the Box class, otherwise it's going to be hard for me helping you out on that.
It is very huge 1000+ lines. Problem is that the Box class also return public key value but I don't need it.. but can't exclude it which is annoying.. I want to display the public key from database not from this class..
Anyway. Thank's for the help. At least I understand why I've got undefined error and how to fix it. Will think about other issue how to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.