Laravel 用户 post 不工作

Laravel user post not working

我在用户尝试 post 状态更新时遇到问题。

这是代码:

// Route

Route::post('/add', 'PostsController@postAdd');

//Post 型号

class Post extends Eloquent {

protected $table = 'posts';

protected $fillable = array('body', 'user_id');

public function user()
{
return $this->belongsTo('User');
}

}

class PostsController extends BaseController {

    public function postAdd() {

        $user = Sentry::getUser();
        $body = array( 'body' => htmlspecialchars(Input::get('body'));
        $body['user_id'] = $user()->id;
        $post = new Post($body);
        $posts->save();
        return View::make('stream');
    }
}

//查看

@extends('dashboard')

@section('content')
<div class="post-box">
<div class="container">
<div class="row">
<div class="col-md-6">
    <form action="/add" method="POST" class="facebook-share-box">
        <div class="share">
            <div class="panel panel-primary">
                  <div class="panel-heading"><i class="fa fa-file"></i> Update Status</div>
                  <div class="panel-body">
                        <textarea required = "" name="body" cols="40" rows="10" id="body" class="form-control message" style="height: 62px; overflow: hidden;" placeholder="What's on your mind ?"></textarea>
                  </div>
                    <div class="panel-footer">
                            <div class="row">
                                <div class="col-md-12 col-md-offset-11">
                                    <div class="form-group">
                                        <button type="submit" name="submit" class="btn btn-lg btn-primary"><i class="fa fa-paper-plane"></i> Post</button>                              
                                    </div>
                                </div>
                            </div>
                    </div>
                </div>
        </div>
        </div>
    </form>
</div>
</div>
</div>
</div>
@stop

当用户更新状态时: 显示以下消息: 哎呀,看起来像出事了。 link 停在 http://localhost:8000/add

显示 "Whoops" 消息是因为抛出了异常,但您的 'debug' 配置设置为 false。如果您打开 app/config/app.php 文件并将 debug 键设置为 true,您将能够看到抛出的错误。不过,这应该只在开发中完成;您不希望在您的生产环境中将 debug 设置为 true。

话虽如此,这可能是您问题中的错字,但在您的 postAdd 方法中,您有代码:

$post = new Post($body);
$posts->save();

但应该是:

$post = new Post($body);
$post->save(); // $post, not $posts