Question

I am trying to store data to the server with laravel. I am following a tutorial and I feel like it may be slightly out of date because I was getting a 500 error earlier with an update method.

public function store()
{
    $input = Input::json();

    return Player::create(array( 
        'teamName' => $input->teamName, // this is line 35
        'teamColor' => $input->teamColor
    ));
}

The above is what the tutorial's syntax is like, I tried the below as well.

public function store()
{
    $input = Input::all();

    return Player::create(array( 
        'teamName' => $input['teamName'], // this is line 35
        'teamColor' => $input['teamColor']
    ));
}

Inside the browser I get this error.

{"error":{"type":"ErrorException","message":"Undefined property: Symfony\\Component\\HttpFoundation\\ParameterBag::$teamName","file":"C:\\wamp\\www\\basketball-app-framework\\app\\controllers\\PlayersController.php","line":35}}

So I feel like these issues I should be able to figure out in a matter of seconds, but I am new and really don't know where to find a clear answer. I tried searching the docs, but I can't find what I am looking for, maybe I am being blind?

Was it helpful?

Solution

Try to use:

public function store()
{
    return Player::create(array( 
        'teamName' => Input::get('teamName'),
        'teamColor' => Input::get('teamColor')
    ));
}

Getting a mass assignment error, means that you need to edit your model and add the $fillable variable to it:

class Player extends Eloquent {

   protected $fillable = array('teamName', 'teamColor');

}

Laravel tries to protect you from mass assignments, so you have to tell it which columns are ok to mass assign.

Request (Input) docs: http://laravel.com/docs/requests.

CheatSheet: http://cheats.jesse-obrien.ca/.

Mass assignment: http://laravel.com/docs/eloquent#mass-assignment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top