【问题标题】:Taking values from database without loop in Laravel在 Laravel 中从没有循环的数据库中获取值
【发布时间】:2020-01-16 20:43:58
【问题描述】:

是否可以在不循环所有结果的情况下查询表并显示某些列?

所以,我有这个问题

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

现在我正在尝试获取这些列

$shipping->option_one
$shipping->option_two

错误很明显

未定义属性:Illuminate\Database\Eloquent\Collection::$preferences_option_one

未定义属性:Illuminate\Database\Eloquent\Collection::$preferences_option_two

我该怎么做?

print_r($shipping)

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

)
1

错误:

[2017-05-30 10:06:10] production.ERROR: Symfony\Component\Debug\Exception\FatalErrorException: Uncaught TypeError: 传递给 Illuminate\Exception\WhoopsDisplayer::display() 的参数 1 必须是一个实例异常的,给定的 TypeError 实例,在第 281 行的 /var/www/html/vendor/laravel/framework/src/Illuminate/Exception/Handler.php 中调用并在 /var/www/html/vendor/laravel/framework 中定义/src/Illuminate/Exception/WhoopsDisplayer.php:43

/var/www/html/vendor/laravel/framework/src/Illuminate/Exception/Handler.php第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);
}

【问题讨论】:

    标签: php laravel laravel-4.2


    【解决方案1】:

    当您从数据库中获取一行时,最后不应使用 get() 方法。您必须在查询结束时使用 first() 方法。所以换个方式

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

    现在你可以使用了

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

    希望它会有所帮助。

    【讨论】:

    • 这返回了一些对我来说毫无意义的错误:::display() must be an instance of Exception, instance of Error given, called...
    • 只需将其设为dd($shipping) 并将结果显示为屏幕截图
    • print_r的输出是get()还是first()?
    • get()first() 两者的输出相同。
    • 再次Uncaught TypeError: Argument 1 passed to Illuminate\Exception\PlainDisplayer::display() must be an instance of Exception, instance of TypeError given,
    【解决方案2】:

    你可以使用:

    $pluck = $shipping->pluck('column', 'other') $采摘->所有()

    【讨论】:

      【解决方案3】:

      如下所示:

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

      所以要获得价值,像这样使用它:

      $shipping[0]->option_one
      

      或者将方法从get()更改为first()

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

      并获得如下价值:

      $shipping->option_one
      

      【讨论】:

      • 当我使用 -&gt;first(); 时,它会返回此 PlainDisplayer::display() 错误
      【解决方案4】:
      $shipping[0]->preferences_option_one
      $shipping[0]->preferences_option_two
      

      【讨论】:

      • 现在我得到了ErrorException: Undefined offset: 1
      • 现在没有错误但是这个This page isn’t working. localhost didn’t send any data. ERR_EMPTY_RESPONSE
      • 做一件事可以显示这个 print_r($shipping );
      • 又是这个错误Uncaught TypeError: Argument 1 passed to Illuminate\Exception\PlainDisplayer::display() must be an instance of Exception, instance of TypeError given,我什至不知道这是什么意思
      • in logs/laravel.log on page 只是空白页
      【解决方案5】:

      问题:

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

      会返回一个集合并且你想要一个对象。

      那就试试吧

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

      或从'preferences_id', '=', 1的结果集合中获取1个对象的其他方法

      【讨论】:

        猜你喜欢
        • 2013-01-18
        • 1970-01-01
        • 2021-08-31
        • 1970-01-01
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-04
        相关资源
        最近更新 更多