2014-01-11 42 views
0

我正在序列化一个表单,并成功获取我想要发送的对象,但是当发出POST请求时,浏览器只传递playerName属性,只有该属性,即使我更改了输入名称并传递了另一个属性playerName它不会通过,除非它是playerName attr。序列化一个对象然后发送到服务器,laravel?

这里是我的backbone.js javascript代码来保存我的表单作为一个对象。

savePlayer: function(e) { 
    e.preventDefault(); 
    var playerDetails = $(e.currentTarget).serializeObject(); 

    console.log(playerDetails); // this returns perfectly with all attributes 

    // I tried this with a model, saving it 
    var player = new App.Models.Player(); 
    player.save(playerDetails); 

    // and with a collection creating it 
    var playersCollection = new App.Collections.PlayersCollection(); 
    playersCollection.create(playerDetails); 

    return false; 
} 

现在我想救两个属性playerNameteamID,这样的形式连载适当喜欢这个{playerName: 'mike', teamID: '2'}时保存/创建被称为是有POST方法和处理是下面的控制器。

// First attempt 
public function store() 
{ 
    return Player::create(array( 
     'playerName' => Input::get('playerName'), 
     'teamID' => Input::get('teamID')  
    )); 
} 

//Second try 
public function store() 
{ 
    // I used this 
    $input = Input::json(); 
    // And this (not same time lol) 
    $input = Input::all(); 

    return Player::create(array( 
     'playerName' => $input->playerName, 
     'teamID' => $input->teamID  
    )); 

} 

在它看来,属性也传递到Post选项卡中的POST响应,但在ResponseJSON他们不露面只playerName ATTR。

//source 
{"playerName":"Joesph","teamID":"1"} // Good 

但在

响应

//It gets an id for the table, a smart feature from backbone.js 
{"playerName":"Joesph","id":23} // Missing "teamID":"1"? 

只是额外的措施,这里是我的形式。

新玩家

<label>First & Last Name</label> 
    <input name="playerName" type="text" value=""> 
    <hr /> 
    // Here I tried dynamic like below and static, just passing in a number like "2" 
    <input type="hidden" name="teamID" value="<%= team.teamID %>" /> 
    <button type="submit" class="btn">Create</button>     

与上面的 playerName我使用 teamID试过的地方的形式,并在模型上其他属性

而且,没有什么工作,但attr属性?

回答

0

好吧,这让我之前对我没有多大意义,但是当发送数据到服务器时,我需要使用$fillable变量。我想我需要阅读更多关于它的内容。

// I needed to add it to this array 
protected $fillable = array('playerID', 'playerName', 'teamID'); 

所以这个代码位于我的类名是Player车型里面,这里是该模型的完整代码。

class Player extends Eloquent { 
    public $timestamps = false; 
    protected $fillable = array('playerID', 'playerName', 'teamID'); 
} 

现在我的原始文章中的代码工作,当讨厌像这样的小挫败颠簸。希望我没有浪费任何时间,我希望这可以帮助使用backbone.js和laravel的人!

相关问题