Taking values from database without loop in Laravel

3.8k Views Asked by At

Is it possible to query table and show certain columns without looping of all the results?

So, I have this query

$shipping = Preferences::where('preferences_id', '=', 1)->get();

Now I'm trying to get this columns

$shipping->option_one
$shipping->option_two

The error is obviously

Undefined property: Illuminate\Database\Eloquent\Collection::$preferences_option_one

Undefined property: Illuminate\Database\Eloquent\Collection::$preferences_option_two

How can I do this?

print_r($shipping)

Array
(
    [0] => stdClass Object
        (
            [preferences_id] => 1
            [preferences_option_one] => Priority Mail
            [preferences_option_two] => Express Mail
        )

)
1

Error:

[2017-05-30 10:06:10] production.ERROR: Symfony\Component\Debug\Exception\FatalErrorException: Uncaught TypeError: Argument 1 passed to Illuminate\Exception\WhoopsDisplayer::display() must be an instance of Exception, instance of TypeError given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Exception/Handler.php on line 281 and defined in /var/www/html/vendor/laravel/framework/src/Illuminate/Exception/WhoopsDisplayer.php:43

/var/www/html/vendor/laravel/framework/src/Illuminate/Exception/Handler.php line 281

protected function displayException($exception)
{
    $displayer = $this->debug ? $this->debugDisplayer : $this->plainDisplayer;

    return $displayer->display($exception);   <--- line 281
}

/var/www/html/vendor/laravel/framework/src/Illuminate/Exception/WhoopsDisplayer.php:43

public function display(Exception $exception) <-- line 43
{
    $status = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;

    $headers = $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : array();

    return new Response($this->whoops->handleException($exception), $status, $headers);
}
5

There are 5 best solutions below

6
On BEST ANSWER

When you getting one row from the database then you should not use get() method at the end. You have to use first() method at the end of the query. So just change

From 
$shipping = Preferences::where('preferences_id', '=', 1)->get();
To 
$shipping = Preferences::where('preferences_id', '=', 1)->first();
or 
$shipping = Preferences::whereIn('preferences_id', 1)->first();

Now you can use

{{ $shipping->option_one }}
{{ $shipping->option_two }} 

Hope it will helpful.

1
On

Do it like below:

$shipping = Preferences::where('preferences_id', '=', 1)->get();//it return collection of objects

So to get value use it like:

$shipping[0]->option_one

Or change method from get() to first()

$shipping = Preferences::where('preferences_id', '=', 1)->first();//it returns single value object 

And get value like:

$shipping->option_one
11
On
$shipping[0]->preferences_option_one
$shipping[0]->preferences_option_two
0
On

You can use :

$pluck = $shipping->pluck('column', 'other')
$pluck->all()
0
On

the problem :

$shipping = Preferences::where('preferences_id', '=', 1)->get();

will return a collection and you want to have an object.

so try

$shipping = Preferences::where('preferences_id', '=', 1)->first();

or other ways to get 1 object from the collection of results where 'preferences_id', '=', 1