1

私は2つのモデルUser.phpBlog.phpコンテンツを持っています。

モデルUser.php

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $softDelete = true;

    protected $table = 'users';

    protected $hidden = array('password');

    //-----
    public function blogs()
    {
       return $this->has_many('Blog');
    }
    //----

モデルBlog.php

<?php

class Blog extends Eloquent {

   protected $guarded = array();

   public static $rules = array();

   public function author()
   {
     return $this->belongs_to('User', 'author_id');
   }
}

コントローラー、BlogsController.php

<?php

class BlogsController extends BaseController {

    public function index()
    {
           $posts = Blog::with('author')->get();

           return Response::json(array(
              'status'   => 'success',
              'message'  => 'Posts successfully loaded!',
              'posts'    => $posts->toArray()),
              200
       );
    }
    //-----

ブログスキーマ、

Schema::create('blogs', function(Blueprint $table) {
     $table->increments('id');
     $table->string('title');
     $table->text('body');
     $table->integer('author_id');
     $table->timestamps();
     $table->softDeletes();
});

そしてユーザースキーマ

Schema::create('users', function(Blueprint $table) {
   $table->integer('id', true);
   $table->string('name');
   $table->string('username')->unique();
   $table->string('email')->unique();
   $table->string('password');
   $table->timestamps();
   $table->softDeletes();
});

Blog::with('author')->get();から呼び出すとBlogsController、次のエラーが表示されます:-

"type":"BadMethodCallException","message":"Call to undefined method Illuminate\\Database\\Query\\Builder::belongs_to()"

そして、に変更Blog::with('author')->get();するBlog::with('author')->all();と、エラーは次のようになります:-

"type":"BadMethodCallException","message":"Call to undefined method Illuminate\\Database\\Query\\Builder::all()"

Laravel 4 の最新のアップデートを使用しています。私のコードの何が問題なのですか?

4

1 に答える 1