【问题标题】:Call to a member function where() on a non-object Laravel 4.2在非对象 Laravel 4.2 上调用成员函数 where()
【发布时间】:2015-02-19 04:31:34
【问题描述】:

我正在尝试使用传递给URLid 来编辑查询,但是在尝试运行代码时出现以下错误:

在非对象上调用成员函数 where()

控制器

class HomeController extends BaseController {

    public function showWelcome() {
        $id = intval($_GET['wab_id']);
        $results = DB::Table('films')->get()->where('wab_id','=', $id);
        print_r($results);
        while ($row=mysql_fetch_array($results)) {       
            $url = $row['url'];

        } 
         return View::make('hello')->with('row', $url);
    }

}

我也试过了:

class HomeController extends BaseController {

    public function showWelcome() {
        $id = intval($_GET['wab_id']);
        $results = DB::Table('films')->get()->where('wab_id', $id);
        print_r($results);
        while ($row=mysql_fetch_array($results)) {       
            $url = $row['url'];

        } 
         return View::make('hello')->with('row', $url);
    }

}

只抛出相同的错误信息,这是唯一没有抛出错误但返回错误的sn-p:

mysql_fetch_array() 期望参数 1 是资源,给定对象

class HomeController extends BaseController {

    public function showWelcome() {
        $id = intval($_GET['wab_id']);
        $results = DB::Table("SELECT * FROM next WHERE wab_id=$id");
        while ($row=mysql_fetch_array($results)) {       
            $url = $row['url'];

        } 
         return View::make('hello')->with('row', $url);
    }

}

路线

Route::get('/', array(
    'as' => 'home',
    'uses' => 'HomeController@showWelcome'
    ));

【问题讨论】:

  • 你能显示你将参数传递到的路由的代码吗?

标签: php model-view-controller laravel-4 laravel-routing


【解决方案1】:

您需要更改链接的顺序,将 where() 函数放在 get() 函数之前。 get() 之后的任何内容都将应用于 Collection 而不是 Query Builder。

【讨论】:

  • 谢谢你,但你知道为什么我给了mysql_fetch_array() expects parameter 1 to be resource, array given
  • 你不使用 Eloquent 的 $results->toArray() 函数有什么原因吗?
  • 因为它基本上给了我同样的错误Call to a member function toArray() on a non-object
【解决方案2】:

如果你想通过 URL 传递参数试试:

// routes.php
Route::get('/{wab_id}', array(
    'as' => 'home',
    'uses' => 'HomeController@showWelcome'
));

另外,where() 约束应该在get() 方法之前。

现在您应该能够接受 URL 参数作为 HomeController#ShowWelcome 函数的参数

class HomeController extends BaseController {

    public function showWelcome($wab_id) {
        $id = $wab_id;

        // where() constraint before the `get()` method.
        $results = DB::Table('films')->where('wab_id','=', $id)->get();
        print_r($results);
        while ($row=mysql_fetch_array($results)) {       
            $url = $row['url'];

        } 
         return View::make('hello')->with('row', $url);
    }

}

【讨论】:

  • Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
  • 您是否将 id 传递到 URL 中? ,例如/23id 值是您想要传递到 URL 中的任何值
  • 是的,我将 id 传递到 Url "blah.com?id=266 和 blah.com/266 并且都返回了一个错误,我认为这与路由有关。
【解决方案3】:

我试图将 std Obj 数组传递给视图,但我必须设置 wab_id 因为如果不是,我接下来会得到 wab_id 的未定义索引 $arrays = json_decode(json_encode($results), true); 为了将我使用查询获得的 std 数组放入一个多数组中,然后循环遍历该数组并且工作正常

感谢大家的帮助。

控制器

class HomeController extends BaseController {

        public function showWelcome(){
            $id = isset($_GET['wab_id'])?$_GET['wab_id']:"";

            $results = DB::Table('films')->where('wab_id', $id)->get();

            $arrays = json_decode(json_encode($results), true);

            foreach ( $arrays as $film ) {

          }
            return View::make('hello')->with('film', $film);

        }

    }

【讨论】:

    【解决方案4】:

    你得到一个 mysql_fetch_array 错误,因为当你弄乱了 get() 和 where() 函数的顺序时

        //you did it like this 
        DB::table('films')->get()->where('wab_id','=', $id);
        //you did it like this, instead of
        DB::table('films')->where('wab_id','=', $id)->get();
    

    get() 被忽略。这就是结果不是数组的原因;

    编辑:

    如果您使用 toArray() 辅助函数,则不需要将其编码和解码为 Json。

        //you can do so like this
        DB::Table('films')->where('wab_id','=', $id)->toArray();
        //or this if thats too long
        $result = DB::table('films')->where('wab_id','=', $id);
        $array = $result->toArray();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多