3

初めての Larevel (4) アプリケーションを作成していて、それが作成された日付を表示したいのですが、この問題が発生しています:Unexpected data found. Unexpected data found. Unexpected data found. Data missing

ブレード テンプレートでこれを実行しようとすると

@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

そして私のコントローラー

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

4

2 に答える 2

2

それで、これを修正するために私がしたことは次のとおりです。

移行では、これを行いました:

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 を渡したと仮定します)

于 2013-10-01T17:39:47.040 に答える
1

ここで、おそらく修正する必要があることがいくつかあります。これは安らかなコントローラーであるため、Laravel は関数名が snake_case ではなく camelCase であることを期待します。

ビューに変数を渡す方法も正しくありません。$usersでビューに変数を渡してみてくださいreturn View::make('users')->with('users',$users);

もう 1 つのことは、ユーザーのコレクションをビューに渡すことです。つまり、ユーザー情報を単にエコーすることはできません。コレクションからユーザー情報を取得するには、ループを使用してコレクションを反復処理する必要があります。(複数のユーザーが参加すると、アプリはおそらく再び壊れるでしょう)

foreach($users as $user)
{
     echo $user->name;
     echo $user->email;
     echo $user->bio;
}

サイドバーとコンテンツ セクションにユーザー情報を表示する方法があるため、ユーザーを獲得するためにおそらく実際にやりたいことは、1 人のユーザーを返すという意味に沿ったものであり$user = User::find(Auth::user()->id);、ループを失う可能性があります。

さっき見たものをもう一つ。安らかなコントローラーを設定している場合、適切なプロパティはですが、基本的に で設定してpublic $restful = true;いるため、実際に使用されているかどうかはわかりません。routes.phpRoute::controller('user', 'UserController');

于 2013-10-01T17:30:45.983 に答える