Call to a member function where() on a non-object Laravel 4.2

元气小坏坏 提交于 2019-12-23 15:49:43

问题


I am attempting to use and id that being passed into the URL to edit the query but when attempting to run the code I get an error of:

Call to a member function where() on a non-object

Controller

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);
    }

}

I have also tried:

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);
    }

}

Only to be thrown the same error message and this is the only snippet that has not thrown the error but returns with a error of:

mysql_fetch_array() expects parameter 1 to be resource, object given

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

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

回答1:


You need to change the order of the chaining to put the where() function before the get() function. Anything after get() will be applied to an Collection rather than the Query Builder.




回答2:


If you want to pass a parameter through the URL try:

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

Also, the where() constraint should be before the get() method.

Now you should be able to accept the URL parameter as an argument into your HomeController#ShowWelcome function

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);
    }

}



回答3:


i was trying to pass an std Obj array to the view, but i had to set wab_id because if not i would get a undefined index of wab_id next i $arrays = json_decode(json_encode($results), true); in order to get the std array that i was getting with using the query into a multi array then looped through the array and that worked

thanks everyone for your help.

Controller

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:


You get a mysql_fetch_array error because when you messed up the order of the get() and where() functions

    //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();

the get() is ignored. that is why the result will not be an array;

EDIT :

If you use the toArray() helper function you wont need to encode and decode it as 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();


来源:https://stackoverflow.com/questions/28598629/call-to-a-member-function-where-on-a-non-object-laravel-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!