1

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 Answers 5

0

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.

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

6 Comments

This return some meaningless to me error: ::display() must be an instance of Exception, instance of Error given, called...
just make it dd($shipping) and show me the result as a screenshot
the output of print_r is by get() or first() ?
get() and first() same output for both.
Again this Uncaught TypeError: Argument 1 passed to Illuminate\Exception\PlainDisplayer::display() must be an instance of Exception, instance of TypeError given,
|
0

You can use :

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

Comments

0

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

1 Comment

when i use ->first(); it's return this PlainDisplayer::display() error
0
$shipping[0]->preferences_option_one
$shipping[0]->preferences_option_two

11 Comments

Now I got ErrorException: Undefined offset: 1
No errors now but this This page isn’t working. localhost didn’t send any data. ERR_EMPTY_RESPONSE
do one thing can you show the output of this print_r($shipping );
Again this error Uncaught TypeError: Argument 1 passed to Illuminate\Exception\PlainDisplayer::display() must be an instance of Exception, instance of TypeError given, I don't even know what this means
in logs/laravel.log on page is just blank white page
|
0

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

Comments

Your Answer

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