35

I'm updating to laravel 5.3, and I get this message:

[2016-08-23 23:12:39] local.ERROR: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::lists() in /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2431
Stack trace:
#0 [internal function]: Illuminate\Database\Query\Builder->__call('lists', Array)
#1 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1423): call_user_func_array(Array, Array)
#2 /home/vagrant/Code/vendor/cviebrock/eloquent-sluggable/src/SluggableTrait.php(254): Illuminate\Database\Eloquent\Builder->__call('lists', Array)
#3 /home/vagrant/Code/vendor/cviebrock/eloquent-sluggable/src/SluggableTrait.php(170): App\User->getExistingSlugs('nouseratnouser-...')
#4 /home/vagrant/Code/vendor/cviebrock/eloquent-sluggable/src/SluggableTrait.php(312): App\User->makeSlugUnique('nouseratnouser-...')
#5 /home/vagrant/Code/vendor/cviebrock/eloquent-sluggable/src/SluggableServiceProvider.php(72): App\User->sluggify()
#6 [internal function]: Cviebrock\EloquentSluggable\SluggableServiceProvider->Cviebrock\EloquentSluggable\{closure}(Object(App\User))
#7 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(221): call_user_func_array(Object(Closure), Array)
#8 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(164): Illuminate\Events\Dispatcher->fire('eloquent.saving...', Array, true)
#9 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1664): Illuminate\Events\Dispatcher->until('eloquent.saving...', Object(App\User))
#10 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1456): Illuminate\Database\Eloquent\Model->fireModelEvent('eloquent.saving...')
#11 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(562): Illuminate\Database\Eloquent\Model->save()
#12 /home/vagrant/Code/database/seeds/UserSeeder.php(34): Illuminate\Database\Eloquent\Model::create(Array)
#13 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(39): UserSeeder->run()
#14 /home/vagrant/Code/database/seeds/DatabaseSeeder.php(37): Illuminate\Database\Seeder->call('UserSeeder')
#15 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(63): DatabaseSeeder->run()
#16 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2279): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#17 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(64): Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
#18 [internal function]: Illuminate\Database\Console\Seeds\SeedCommand->fire()
#19 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#20 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
#21 /home/vagrant/Code/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 /home/vagrant/Code/vendor/symfony/console/Application.php(818): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 /home/vagrant/Code/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Seeds\SeedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#25 /home/vagrant/Code/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#26 /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(111): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#27 /home/vagrant/Code/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#28 {main}  

I checked the code that cause it:

        User::create([
        'name' => 'No User',
        'email' => 'nouser@nouser.com',
        'password' => bcrypt('0'),
        'provider' => '0',
    ]);

I don't understand why.

If I comment it, I get the same problem in another file:

        Tournament::create([
        'user_id' => 1,
        'name' => "name",
        'dateIni' =>  $dateIni,
        'dateFin' =>  $dateIni,
    ]);

I've made a global search of "lists" without success...

Any idea why is it happening???

5 Answers   正确答案 

76

You can use method pluck. Method lists is removed in Laravel 5.3. I changed 

lists('key')->all() to pluck('key')->all() 

( 加上->all()  ( 或者->toArray() )结果就是数组了,就不是 collections 了)  and it's working now.

  • Hello Sang Where you replaced pluck with lists, I am having same problem with create method of eloquent class but don't understand where exactly I have to replace pluck with lists – Junaid Ali Jan 9 '17 at 13:59
  • Hi Junaid, You need to replace where you use lists method. – Sang Nguyen Jan 11 '17 at 2:47
  • thanks for your reply I actually fixed my problem by upgrading eloquent-sluggable library – Junaid Ali Jan 11 '17 at 7:35
27

Laravel 5.3, the lists() method is removed in favour of the pluck() method. For Example:

 正确答案 

     加上 ->all()   ( 或者->toArray() 就变成了数组 ,不加上 就是collections 集合

$roles = Role::pluck('name','id');
dd(
$roles);

//结果如下

image.png

代码 
$roles = Role::pluck('name','id')->all();
dd($roles);
结果

image.png


代码
$roles = Role::pluck('id');
dd($roles);

结果
image.png


代码  
$roles = Role::pluck('id')->all();
dd($roles);
结果

image.png



$userList = App\User::pluck('name', 'id');
6

lists() was deprecated. Use pluck() instead.

The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.

https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0

1

yeah i just found it, but the funny thing its mentiond under https://laravel.com/docs/5.3/upgrade#upgrade-5.2.0 which it not correct as it was working until i upgraded to v5.3

0
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
class ApiController extends Controller
{
    public function data()
    {
        $data = team::all();
        return $data;    
    }
}
  • 1
    Thanks for trying to help, can you try to provide any explanation so that users can understand better how this is a good answer to you? – P. Ellul Apr 25 '19 at 6:53

 来自  https://stackoverflow.com/questions/39114249/call-to-undefined-method-illuminate-database-query-builderlists-when-seeding