JustGage
JustGage

Reputation: 259

how do I get the updated_at and created_at

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

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);

Upvotes: 3

Views: 7055

Answers (2)

JustGage
JustGage

Reputation: 259

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.

  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)

Upvotes: 2

user1669496
user1669496

Reputation: 33068

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.

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);.

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.

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');.

Upvotes: 1

Related Questions