Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
    ));
like image 584
Charles Avatar asked Nov 27 '25 12:11

Charles


2 Answers

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.

like image 162
iavery Avatar answered Nov 30 '25 01:11

iavery


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

}
like image 34
Michael Coleman Avatar answered Nov 30 '25 00:11

Michael Coleman