我在这段代码上遇到错误,所以我从刀片开始,然后是控制器,然后modle请给我解决方案,为什么会出现这个问题。
刀片:
@extends ('lay')
@section('content')
<h1>Write a New Article</h1>
<hr>
{!!Form::open(['url'=>'articles'])!!}
<div class="form-group">
{!!Form::label('title','Title:')!!}
{!!Form::text('title','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('body','Body:')!!}
{!!Form::textarea('body','',['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::label('published_at','Published on:')!!}
{!!Form::input('date','published_at',date('Y-m-d'),['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!!Form::submit('Add Article',['class'=>'btn btn-primary form-control','name'=>'submit'])!!}
</div>
{!!Form::close()!!}
@stop
控制器:这是程序的控制器,所有这些都是在laravel中完成的
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Articles;
use Request;
use Carbon\Carbon;
class ArticlesController extends Controller {
public function index()
{
/*$art=[
'title'=>'ashwani',
'body'=>'rathi',
'published_at'=>'Carbon\Carbon::now()'
];
Articles::create($art);*/
$articles=Articles::latest()->get();
return view('articles.index', compact('articles'));
}
public function show($id)
{
$article=Articles::findorFail($id);
return view('articles.show',compact('article'));
}
public function create(){
return view('articles.create');
}
public function store()
{
//$input=Request::all();
//$input['published_at']=Carbon::now();
//Articles::create($input);
Articles::create(Request::all());
return redirect('articles');
}
}
模型:这是我使用日期而不工作的模型
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Articles extends Model {
protected $fillable= [ 'title', 'body', 'published_at' ];
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d|', $date);
}
}
答案 0 :(得分:2)
看起来这是一个小错字。
您已将createFormFormat
放在拼写Form
而不是From
的位置。
您只需在功能中更正此内容:
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
答案 1 :(得分:0)
您可以使用
public function getPublishedAtAttribute($date){
return Carbon::parse($date)->format('Y-m-d');
}
public function setPublishedAtAttribute($date){
$this->attributes['published_at'] = Carbon::parse($date);
}
取代
public function setPublishedAtAttribute($date){
//$this->attributes['published_at'] = Carbon::createFormFormat('Y-m-d', $date);
return $this->attributes['published_at']->format('Y-m-d');
}