2016-09-19 143 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

這並沒有解決問題。我得到同樣的錯誤。 –