Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Array to string conversion error on Model::create or Model->save()

I'm performing a simple insert which I've done many times without any issues and for some odd reason it's not working and I get this error message:

error: {type: "ErrorException", message: "Array to string conversion",…}
file: "C:\wamp\www\studentreg2\vendor\laravel\framework\src\Illuminate\Database\Grammar.php"
  line: 33
  message: "Array to string conversion"
  type: "ErrorException"

Here's my code:

$advisorCheck = AdvisorCheck::create([
            'status'         => Input::get('status'),
            'application_id' => Input::get('id'),
            'user_id'        => Auth::id()
        ]);

The migration for the advisor_check table which AdvisorCheck model uses seems fine, all foreign keys are unsigned and show the relations correctly in phpmyadmin, all values from the Input::get are strings, the model has the correct fields set as fillable (status, application_id, user_id).

I've even tried doing this in php artisan tinker like this:

AdvisorCheck::create([ 'status' => 'returned', 'application_id' => '3', 'user_id' => '4']);

and I get this response: Array to string conversion

I've also tried this method and get the same error:

$advisorCheck                 = new AdvisorCheck;
$advisorCheck->status         = Input::get('status');
$advisorCheck->application_id = Input::get('id');
$advisorCheck->user_id        = Auth::id();
$advisorCheck->save();

Model code:

<?php

class AdvisorCheck extends \Eloquent {

    protected $fillable = ['status', 'application_id', 'user_id'];

    protected $table = ['advisor_check'];
}

If you need to see more code please ask.

Many thanks to anyone who can help!

like image 254
haakym Avatar asked Dec 17 '14 11:12

haakym


2 Answers

As you can see in the example that's shown in the Laravel docs the table property is a string and not an array

protected $table = 'advisor_check';

Which makes total sense if you think about it, since Eloquent models don't support multiple tables natively.

like image 199
lukasgeiter Avatar answered Oct 12 '22 06:10

lukasgeiter


$table should be a string not array

<?php

   class AdvisorCheck extends \Eloquent {

   protected $fillable = ['status', 'application_id', 'user_id'];

   protected $table = 'advisor_check';
}
like image 28
nilobarp Avatar answered Oct 12 '22 07:10

nilobarp