9

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!

8
  • Is there anything out of the ordinary in the Model? Commented Dec 17, 2014 at 11:55
  • I've got a feeling it's because I put the $table before $fillable, all my other models have $fillable first - giving it a shot now Commented Dec 17, 2014 at 11:57
  • Nope wasn't it - didn't make any sense for it to be that I guess? I'll post the model code Commented Dec 17, 2014 at 11:58
  • 2
    The table property needs to be a string not an array! Commented Dec 17, 2014 at 12:10
  • 1
    @lukasgeiter Good spot. Commented Dec 17, 2014 at 12:18

3 Answers 3

27

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.

Sign up to request clarification or add additional context in comments.

Comments

2

$table should be a string not array

<?php

   class AdvisorCheck extends \Eloquent {

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

   protected $table = 'advisor_check';
}

Comments

0

if we give table name in laravel model like array:

 protected $table = ['thoughts'];

then it will generate error.

so you should give table name as string like :

protected $table = 'thoughts';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.