Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ErrorException in Grammar.php line 39: Array to string conversion

This is my store function in CategorieController.php

public function store(Request $request) {
  Categorie::create([
  'name'=>$request['name'],
  ]);
  return redirect::to('/categorie');
}

and this is what I have in my model Categorie.php

class Categorie extends Model { 
  protected $table =['categories'];
  protected $fillable=['name'];
}

but when I try to save my categorie into database I get this error:

ErrorException in Grammar.php line 39: Array to string conversion

like image 397
Diego Bsc Avatar asked Jul 16 '26 11:07

Diego Bsc


2 Answers

$table variable used in your model is of type string.

protected $table = 'categories';
like image 135
revo Avatar answered Jul 19 '26 00:07

revo


You should make changes in two places:

public function store(Request $request) {
    Categorie::create([
        'name' => $request->get('name'), // 1st place
    ]);

    return redirect::to('/categorie');
}

And:

class Categorie extends Model { 
    protected $table = 'categories'; // 2nd place
    protected $fillable = ['name'];
}

Explanation:

$table attribute of Model class which Categorie class extends from accepts a table name as string (Not Array) to link this model to it's corresponding table in database.

like image 26
Mustafa Ehsan Alokozay Avatar answered Jul 19 '26 02:07

Mustafa Ehsan Alokozay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!