Mysql Laravel无法将类型为Eloquent/Builder的对象用作数组

Mysql Laravel无法将类型为Eloquent/Builder的对象用作数组,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,由于我现在正在使用laravel,我必须在界面上显示一些数据。 但是Laravel安装在计算机X上,数据库位于一个遥远的Y服务器上。 出于测试目的,我试图显示一个表中的简单数据,即下表的第一个信息: 表站点: 编码\u站点varchar(255)主键 我就是这样做的: database.php 'mysql' => array( 'driver' => 'mysql', 'host' => '126.x.x.x', 'database'

由于我现在正在使用laravel,我必须在界面上显示一些数据。 但是Laravel安装在计算机X上,数据库位于一个遥远的Y服务器上。 出于测试目的,我试图显示一个表中的简单数据,即下表的第一个信息:

站点编码\u站点varchar(255)主键

我就是这样做的:

database.php

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => '126.x.x.x',
    'database'  => 'MYNET',
    'username'  => 'mynet',
    'password'  => 'mypassword',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'port'      => '3308',
),
<?php

class Site extends Eloquent {
<empty because i don't need relations for now> }
Route::model('site', 'Site');
Route::get('liste_sites', array('uses' => 'HomeController@liste_sites', 'as' => 'liste_sites'));
@for ($i = 0 ; $i < count($sites); $i ++)
    <p>{{$sites[$i]->code_site}}</p>
@endfor
public function liste_sites() 
    {
        $sites = Site::select('code_site')
        ->orderBy('code_site','desc');

        return View::make('liste_sites', array( 'sites' => $sites, 'which_actif' => 1));
    }
model/Site.php

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => '126.x.x.x',
    'database'  => 'MYNET',
    'username'  => 'mynet',
    'password'  => 'mypassword',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'port'      => '3308',
),
<?php

class Site extends Eloquent {
<empty because i don't need relations for now> }
Route::model('site', 'Site');
Route::get('liste_sites', array('uses' => 'HomeController@liste_sites', 'as' => 'liste_sites'));
@for ($i = 0 ; $i < count($sites); $i ++)
    <p>{{$sites[$i]->code_site}}</p>
@endfor
public function liste_sites() 
    {
        $sites = Site::select('code_site')
        ->orderBy('code_site','desc');

        return View::make('liste_sites', array( 'sites' => $sites, 'which_actif' => 1));
    }
liste_sites.blade.php

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => '126.x.x.x',
    'database'  => 'MYNET',
    'username'  => 'mynet',
    'password'  => 'mypassword',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'port'      => '3308',
),
<?php

class Site extends Eloquent {
<empty because i don't need relations for now> }
Route::model('site', 'Site');
Route::get('liste_sites', array('uses' => 'HomeController@liste_sites', 'as' => 'liste_sites'));
@for ($i = 0 ; $i < count($sites); $i ++)
    <p>{{$sites[$i]->code_site}}</p>
@endfor
public function liste_sites() 
    {
        $sites = Site::select('code_site')
        ->orderBy('code_site','desc');

        return View::make('liste_sites', array( 'sites' => $sites, 'which_actif' => 1));
    }
但是我得到了错误

Cannot use object of type Illuminate\Database\Eloquent\Builder as array
是因为我的数据不可读吗?

更改此行:

$sites = Site::select('code_site')->orderBy('code_site','desc');
为此:

$sites = Site::select('code_site')->orderBy('code_site','desc')->get();
我很肯定这会解决的


基本上,在调用一个关闭函数之前,
->get()
->paginate(10)
->first()
$sites将被视为一个查询,而不是
Site
对象的数组,因此不能在视图中使用。希望有帮助

$sites变量包含一个Lightlight\Database\Eloquent\Builder实例,因为您尚未调用该实例上的方法来检索查询生成器所涉及的数据。正如Tim在下面所说的,您可以对其调用get(),然后返回结果。从语句中可以看到,调用select()然后调用orderBy()。select chain返回的对象允许您调用后续方法以生成查询。例如顺序、限制等。get()方法调用将最终执行生成的查询。它正在工作。我不知道paginate执行请求。谢谢没问题!我在很多查询中都遇到了这个问题,直到我发现这个问题:P。