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);
}
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.