我有以下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數據庫表中定義主鍵外鍵關係。
您可以添加郵政表的遷移類嗎? –
@ Allloush補充說。 –