laravel 我如何获得 updated_at 和 created_at
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19121188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do I get the updated_at and created_at
提问by JustGage
I'm making my first Larevel (4) application and I want to display the date that it was created and I'm getting this problem: Unexpected data found. Unexpected data found. Unexpected data found. Data missing
我正在制作我的第一个 Larevel (4) 应用程序,我想显示它的创建日期,但我遇到了这个问题: Unexpected data found. Unexpected data found. Unexpected data found. Data missing
when I try to do this in my blade template
当我尝试在我的刀片模板中执行此操作时
@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
and my 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;
}
}
?>
it works as soon as I take out :
一旦我取出它就会起作用:
<p><small>{{ $user->created_at }}</small></p>"
any ideas how to access those values, I checked and they DO exist in my table.
我检查了如何访问这些值的任何想法,它们确实存在于我的表中。
this is the schema of my table
这是我的表的架构
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);
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);
回答by JustGage
So here's what I did to fix it.
所以这就是我为修复它所做的。
in the migrations I did this:
在迁移中我这样做了:
class CreateTable extends Migration {
public function up()
{
Schema::create('users', function($table) {
$table->string('name');
$table->timestamps();
});
}
/* also need function down()*/
I had the insertions like this in my migrations
to add some users.
我有这样的插入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();
}
}
Now when you try to use {{ $user->updated_at }}
or {{ $user->created_at }}
it will work! (assuming that you passed $user to the view)
现在,当您尝试使用{{ $user->updated_at }}
或{{ $user->created_at }}
它会起作用!(假设您将 $user 传递给视图)
回答by user1669496
There are a few things going on here that should probably be fixed. Since this is a restful controller, Laravel expects your function names to be camelCase rather than snake_case.
这里发生了一些事情,可能应该修复。由于这是一个安静的控制器,Laravel 期望你的函数名称是驼峰式而不是蛇形。
The way you are passing variables to the view also is not looking right. Try passing the $users
variable to the view with return View::make('users')->with('users',$users);
.
您将变量传递给视图的方式看起来也不正确。尝试将$users
变量传递给视图return View::make('users')->with('users',$users);
。
Another thing is you are passing a collection of users to the view, which means you won't just be able to echo user information. To get the user information out of the collection, you must iterate through the collection with a loop. (Your app will probably break again once you get more than one user in)
另一件事是您将一组用户传递给视图,这意味着您不仅能够回显用户信息。要从集合中获取用户信息,您必须使用循环遍历集合。(一旦您获得多个用户,您的应用可能会再次崩溃)
foreach($users as $user)
{
echo $user->name;
echo $user->email;
echo $user->bio;
}
Because the way you have your sidebar and content sections showing user information, what you probably actually want to do to get your user is something along the lines of $user = User::find(Auth::user()->id);
meaning it would be returning one user and you'd be able to lose the loop.
因为你的侧边栏和内容部分显示用户信息的方式,你可能真正想要做的是让你的用户沿着这$user = User::find(Auth::user()->id);
意味着它会返回一个用户并且你可能会失去循环。
One more thing I just seen. If you are setting up a restful controller, the proper property is public $restful = true;
though I'm not sure that's actually being used anymore because you are basically setting that in routes.php
with Route::controller('user', 'UserController');
.
我刚刚看到的另一件事。如果你正在设置一个安静的控制器,正确的属性是public $restful = true;
虽然我不确定它是否真的被使用了,因为你基本上是routes.php
用Route::controller('user', 'UserController');
.