简体   繁体   中英

Laravel eloquent all() method gives error "Array to string conversion"

I'm just trying to do so:

$cattles = Cattle::all();
dd($cattles);

But it throws exception " ErrorException Array to string conversion ".
I can't figure out why... If I try

$cattles = DB::select('SELECT * FROM cattles');
dd($cattles);

It gives expected result. ie all rows.
Model:

class Cattle extends Model
{
    use HasFactory;

    protected $table = ['cattles'];

    protected $fillable = [
        'property_cattle_type_id',
        'property_id',
        'paddock_id',
        'cattle_type_id',
        'age',
        'visual_id',
        'animal_id',
        'sex',
        'weight',
    ];
}

It seems to me this is very strange. The first four columns - foreign keys.
In fact it's not a big problem for me, but I'm just interested about this behavior.

What's the reason can be?

The reason is passing an array to the table variable. You may take a look at the Laravel documentation for Eloquent table names .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM