amcardwell's avatar

array_flip() error when calling Model::all()

Hey everyone. I'm having an issue with listing records from my "Market" model using

$markets = App\Models\Market::all();

Here is the error I receive when executing this using artisan tinker:

PHP warning:  array_flip() expects parameter 1 to be array, string given in /home/vagrant/www/trust dale/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 458

App\Models\Market.php:


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Market extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'markets';

    /**
     * A featured_id belongs to one business.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToOne
     */
    public function featured()
    {
        return $this->belongsToOne('App\Models\Business', 'id', 'featured_id');
    }
    
}

0 likes
4 replies
tomopongrac's avatar

does you variable fillable or hiden in Market are strings?

amcardwell's avatar

Sorry, I'm still learning...

I have 4 columns in my Market table:


id->integer(10)->primary
name->varchar(255)
featured_id->integer(10)->foreign
updated_at->timestamp

Should I be setting any of these attributes in my model?

is the name column causing the issue because its a string and/or because its not set in my model?

thefuzzy0ne's avatar
Level 6

It looks like you might be overriding $fillable in your model, and you've set it to a string instead of an array. Simply remove that (or change it to an array) and things should work again.

Also, you don't need to set the value for $table as you're naming conforms to the convention. It's only necessary if you don't follow the convention of:

Singular model names: i.e SomeEntity Plural table names: i.e some_entities

amcardwell's avatar

Ahh... okay. I dont know what happened... but when I deleted the model and generated a new one using

php artisan crud::model Markets

it worked. I do believe it had to do with the $fillable array. For reference, here is my (working) updated model:


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Markets extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'markets';

    /**
    * The database primary key value.
    *
    * @var string
    */
    protected $primaryKey = 'id';

    /**
     * Attributes that should be mass-assignable.
     *
     * @var array
     */
    protected $fillable = [];
}

Thanks!

Please or to participate in this conversation.