雄辩创造不在laravel工作

时间:2014-05-31 09:58:28

标签: laravel eloquent

我是laravel的新手,所以我在创建帖子时遇到了问题。

这是我的路线:

Route::post('savepost', 'PostsController@savepost');

这是我的createpost.blade.php

@include('header')

  <div class="container">

      <!-- Main component for a primary marketing message or call to action -->
      <div class="jumbotron">
        <h3>Create New Post</h3>

            <form action="savepost" class="form-horizontal" method="post" role="form">
              <div class="form-group">
                <label for="inputEmail3" class="col-sm-4 control-label">Post Title</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" name="title" id="inputEmail3" placeholder="Enter New Title">
                </div>
                @if($errors->has('title'))
                    <p class="alert alert-danger"><strong>oopps! </strong>{{ $errors->first('title') }} </p>
                @endif
              </div>
              <div class="form-group">
                <label for="inputPassword3" class="col-sm-4 control-label">Post Description</label>
                <div class="col-sm-8">
                  <textarea name="description"></textarea>
                </div>

                @if($errors->has('description'))
                    <p class="alert alert-danger"><strong>oopps! </strong>{{ $errors->first('description') }} </p>
                @endif

              </div>

              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-8">
                  <input type="submit" class="btn btn-default" value="Save Post">
                </div>

              </div>
            </form>

      </div>

    </div> <!-- /container -->

@include('footer')

这是我的PostsController

class PostsController extends BaseController{

    public function createpost(){
        return View::make('createpost');
    }   


    public function savepost(){

        $input = Input::all();
        $validation = Validator::make($input, Posts::$rules);

        if ($validation->passes())
        {


        Posts::create(array(
            'title'=>Input::get('title'),
            'description'=>Input::get('description')
        ));

        }
        else{
        return Redirect::route('createpost')->withErrors($validation);
        }

    }

}

以下是我的模特:

class Posts extends Eloquent {

protected $guarded = array();
protected $fillable = array('name', 'description');


    protected $table = 'posts';

    public static $rules = array(
        'title' => 'required|min:5',
        'description' => 'required'
    );
}

这是我的帖子数据库

id    title   description   url   date

请帮帮我

2 个答案:

答案 0 :(得分:0)

您的帖子表缺少默认的laravel时间戳。通过执行以下操作在模型中关闭它们:

public $timestamps = false;

或者将它们添加到您的帖子表中:

alter table posts add created_at datetime NOT NULL
alter table posts add updated_at datetime

或者在模型中创建自己的时间戳。我看到你有一个日期栏,所以你可以这样做:

const CREATED_AT = 'date';

答案 1 :(得分:-1)

您不能同时使用可填充的和受保护的。

  

当然,您应该使用$ fillable或$ guarded-不能同时使用两者。

https://laravel.com/docs/5.3/eloquent#mass-assignment