2016-09-19 84 views
0

我有以下MySQL表结构:Laravel雄辩逆一对多返回空

posts表:

posts: {id(PK), title, content, slug, date, writer_id, created_at, updated_at}

writers表:

writers: {id(PK), name, type, created_at, updated_at}

迁移班database/migrations目录:

posts表:

class CreatePostsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('posts', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('title'); 
      $table->longText('content'); 
      $table->string('slug'); 
      $table->date('date'); 
      $table->date('modified_date'); 
      $table->integer('publish'); 
      $table->integer('trash'); 
      $table->integer('wid'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('posts'); 
    } 
} 

改变柱的类型:

class RenamePostColumn extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::table('posts', function ($table) { 
      $table->longText('content')->change(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::table('posts', function ($table) { 
      $table->longText('content')->change(); 
     }); 
    } 
} 

更名柱:

class RenamePostColumnWid extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::table('posts', function ($table) { 
      $table->renameColumn('wid', 'writer_id')->change(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::table('posts', function ($table) { 
      $table->renameColumn('writer_id', 'wid')->change(); 
     }); 
    } 
} 

位作家表:

class CreateWritersTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('writers', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->timestamps(); 
      $table->string('name'); 
      $table->string('type'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('writers'); 
    } 
} 

以下是我在app目录情态动词:

post.php中:

class Post extends Model 
{ 
    public function writer() 
    { 
     return $this->belongsTo(Writer::class); 
    } 
} 

Writer.php:

class Writer extends Model 
{ 
    public function posts() 
    { 
     return $this->hasMany(Post::class); 
    } 
} 

现在我已经在app/Repositories目录中创建了一个存储库类。

PostRepository.php:

class PostRepository 
{ 
    public function forSingle($slug) 
    { 
     return Post::whereSlug($slug)->get(); 
    } 
} 

我上面的查询与调试:

return Post::whereSlug($slug)->toSql(); 

它返回下面的查询:

select * from `posts` where `slug` = ? 

我的路线是routes/web.php文件。

web.php:

Route::get('/post/{slug}', '[email protected]'); 

最后,我有我的app/Http/Controllers目录控制器。

PostController。PHP:

use App\Repositories\PostRepository; 

class PostController extends Controller 
{ 
    protected $post; 

    function __construct(PostRepository $post) 
    { 
     $this->post = $post; 
    } 

    public function single($slug) 
    { 
     return view('single', [ 
      'post' => $this->post->forSingle($slug) 
     ]); 
    } 
} 

我做了一件视图文件,如下所示:

single.blade.php

@if (count($post) > 0) 
    @foreach ($post as $blog) 
     <h3><a href="#">{{$blog->title}}</a></h3> 
     <p>{!!$blog->content!!}</p> 
     @foreach($blog->writer as $writer) 
      <span>{{$writer->name}}</span> 
     @endforeach 
    @endforeach 
@endif 

这里是我的问题。一切工作正常,直到我添加

@foreach($blog->writer as $writer) 
    <span>{{$writer->name}}</span> 
@endforeach 

本节给了我错误说:

试图让非对象的属性(查看:\资源\意见\ single.blade.php)

我在{{$blog}}列印了$博客。它不返回任何作家属性。你能帮助我吗?

PS:我没有在MySQL数据库表中定义主键外键关系。

+0

您可以添加邮政表的迁移类吗? –

+0

@ Allloush补充说。 –

回答

1

当它与一对多反过来时,我们需要明确地告诉我们需要其他表数据。更改PostRepository.php中的以下内容修复了此问题。

class PostRepository 
{ 
    public function forSingle($slug) 
    { 
     return Post::whereSlug($slug)->with('writer')->get(); 
    } 
} 
0

你必须定义外键或索引

在我的项目,我用指数关系

,所以你必须做的是增加指数的关系在writer_id这样

public function up() 
{ 
    Schema::create('posts', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('title'); 
     $table->longText('content'); 
     $table->string('slug'); 
     $table->date('date'); 
     $table->date('modified_date'); 
     $table->integer('publish'); 
     $table->integer('trash'); 
     $table->integer('wid')->unsigned()->index(); // add this 
     $table->timestamps(); 
    }); 
} 

请尝试前面的

+0

这并没有解决问题。我得到同样的错误。 –