Search code examples
phpmysqllaravel-5where-clauserelational

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


i'm trying to retrieve the matching values from 2 tables, in order to make an user record.

My code:

$states = ['1','2'];
$cities = DB::table('cities')->get();
$state = $faker->randomElement($states);
$city= $faker->randomElement($cities->where('state_id',$state)->value('id')); 

Current response:

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

Edit 1. Translated to english for better understanding.


Solution

  • It should be something like the following:

    $states = ['1','2'];
    $state = $faker->randomElement($states);
    $cities = DB::table('cities')->where('state_id', $state)->lists('id')->all();
    $city = $faker->randomElement($cities);