laravel - 我如何获得 updated_at 和 created_at

标签 laravel laravel-4

我正在制作我的第一个 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/

相关文章:

php - Laravel 使用 queue() 发送邮件不在服务器上工作

PHP "with"关键字 - "with"有什么作用?

php - Laravel 错误 - stdClass 类的对象无法转换为字符串

javascript - Laravel 4,jQuery .ajax,未收到帖子输入

php - Laravel 5 [PDOException] SQLSTATE[42000] : Syntax error or access violation:

PHP Lumen 在 null 上调用成员函数 connection()

laravel - 在 Laravel 应用程序中添加服务层的效果

php - Request->file 返回 null 为什么?

php - 分离未定义

laravel - Eloquent morphOne 关系不限于一种关系