首页 > 解决方案 > My instance doesn't fetch me the table that I want

问题描述

So I am trying to attach an object full of information from the MYSQL DB, but the outcome isn't what I am expecting.

Controller -

public function index()
{
    $location = Location::orderBy('created_at', 'desc');
    return view('location')->with('locations', $location);
}

Model -

class Location extends Model
{
    // Primary Key
    public $primaryKey = 'id';
    // Timestamps
    public $timestamps = true;
}

Result -

  Builder {#486 ▼
  #query: Builder {#485 ▶}
  #model: Location {#484 ▶}
  #eagerLoad: []
  #localMacros: []
  #onDelete: null
  #passthru: array:12 [▶]
  #scopes: []
  #removedScopes: []
}

标签: phpmysqllaravel

解决方案


改变这个

$location = Location::orderBy('created_at', 'desc');

$location = Location::orderBy('created_at', 'desc')->get();

推荐阅读