我正在制作我的第一个 Larevel (4) 应用程序,我想显示它的创建日期,但我遇到了这个问题:发现意外数据。发现意外数据。发现意外数据。数据丢失
当我尝试在我的 Blade 模板中执行此操作时
@extends('layout')
@section('content')
<h3>Name: {{ $user->name }}</h3>
<p>Email: {{ $user->email }}</p>
<p>Bio: {{ $user->bio }}</p>
@endsection()
@section('sidebar')
<p><small>{{ $user->created_at }}</small></p>
@endsection()
@stop
和我的 Controller
<?php
class UserController extends BaseController
{
public $restfull = true;
public function get_index() {
//$users = User::all();// gets them in the order of the database
$users = User::orderBy("name")->get(); // gets alphabetic by name
$v = View::make('users');
$v->users = $users;
$v->title = "list of users";
return $v;
}
public function get_view($id) {
$user = User::find($id);
$v = View::make('user');
$v->user = $user;
$v->title = "Viewing " . $user->name;
return $v;
}
}
?>
我一取出它就开始工作了:
<p><small>{{ $user->created_at }}</small></p>"
关于如何访问这些值的任何想法,我检查过它们确实存在于我的表中。
这是我的表的模式
CREATE TABLE "users"("id"integer null primary key autoincrement, "email"varchar null, "name"varchar null, "bio"varchar null, "created_at"datetime null, "updated_at"datetime null );
最佳答案
所以这就是我为修复它所做的工作。
在迁移中我这样做了:
class CreateTable extends Migration {
public function up()
{
Schema::create('users', function($table) {
$table->string('name');
$table->timestamps();
});
}
/* also need function down()*/
我在我的 migrations
中插入了这样的内容来添加一些用户。
class AddRows extends Migration {
/* BAD: this does NOT! update the timestamps */
public function up()
{
DB::table('users')->insert( array('name' => 'Person') );
}
/* GOOD: this auto updates the timestamps */
public function up()
{
$user = new User;
$user->name = "Jhon Doe";
$user->save();
}
}
现在,当您尝试使用 {{ $user->updated_at }}
或 {{ $user->created_at }}
时,它将起作用! (假设您将 $user 传递给 View )
关于laravel - 我如何获得 updated_at 和 created_at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19121188/