【问题标题】:Laravel get data from db array not workingLaravel 从数据库数组中获取数据不起作用
【发布时间】:2019-12-15 12:13:47
【问题描述】:

当我运行代码时,我没有收到任何错误,但我试图显示的数据没有显示,它只是空白。有人可以告诉我我做错了什么吗?

我的控制器:

public function openingPage($id) {

      $this->getGames();
      $games = $this->getGames();

      return view('caseopener')->with('games',$games);

    }

private function getGames() {
      $games = array();
      foreach ($this->data->items as $item) {
          $game = new Game($item);
          $games[] = array(
                'id' => $game['id'],
                'name' => $game['name'],
                'price' => $game['price'],
                'image' => $game['image'],
            );

      }

      return $games;
  }

“getGames 函数”中使用的“Game”模型:

class Game extends Model
{
  private $id;
  public $data;

  public function __construct($id) {
       parent::__construct();
       $this->id = $id;
       $this->data = $this->getData();
   }

   private function getData() {

       $game = DB::table('products')->where('id', 1)->first();

       if(empty($game)) return array();

       return $game;
   }
}

观点:

@foreach ($games as $game)

    <div class="gold">$ {{ $game['price'] }}</div>

@endforeach

【问题讨论】:

  • first() 只返回一个对象
  • $this-&gt;data-&gt;items 是否包含任何内容? $item 是否包含任何内容?
  • 是的,它包含这个private function getCaseData($id) { $items = DB::table('cases')-&gt;where('id', $id)-&gt;get(); $data = @$items[0] ? $items[0] : array(); if(isset($data-&gt;items)) { $data-&gt;items = json_decode($data-&gt;items, true); }
  • 你试过dd($this-&gt;data-&gt;items)dd($item)吗?
  • 我相信$game = DB::table('products')-&gt;where('id', 1)-&gt;first();一直在寻找id为1的游戏。

标签: php arrays database laravel laravel-blade


【解决方案1】:

我认为你把事情复杂化了。您可以像这样简化流程:

鉴于您提供的代码,您似乎在 Game 模型中使用了自定义表名 ('products')。所以我们先解决这个问题:

Game.php

class Game extends Model
{
    protected $table = 'products'; //
}

现在,您似乎正在搜索Game ids ($this-&gt;data-&gt;items) 的数组。如果是这样,您可以使用 Eloquent 进行查询,特别是 whereIn() 方法:

YourController.php

public function openingPage($id)
{
    $games = Game::whereIn('id', $this->data->items)->get();

    return view('caseopener')->with('games', $games);
}

或者,如果您想确保只返回每个Game/product 的idnamepriceimage,您可以使用API Resources 格式化响应:

php artisan make:resource GameResource

然后在你新创建的类中:

app/Http/Resources/GameResource.php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class GameResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'price' => $this->price,
            'image' => $this->image,
        ];
    }
}

所以现在只需更新您的控制器:

YourController.php

use App\Http\Resources\GameResource;

public function openingPage($id)
{
    $games = Game::whereIn('id', $this->data->items)->get();

    return view('caseopener')->with('games', GameResource::collection($games));
} //                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多