[英]Laravel eloquent inverse one to many returns empty
我有以下MySQL表結構:
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
目錄中的遷移類:
帖子表:
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}', 'PostController@single');
最后,我將控制器放在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
這部分給我錯誤提示:
嘗試獲取非對象的屬性(視圖:\\ resources \\ views \\ single.blade.php)
我已經在{{$blog}}
視圖中打印了$ blog。 它不返回任何writer屬性。 你能幫我嗎?
PS:我尚未在MySQL數據庫表中定義主鍵外鍵關系。
當它是一對多的雄辯的逆時,我們需要明確地告訴我們我們需要其他表數據。 在PostRepository.php中更改以下內容解決了該問題。
class PostRepository
{
public function forSingle($slug)
{
return Post::whereSlug($slug)->with('writer')->get();
}
}
您必須定義外鍵或索引
在我的項目中,我使用索引關系
所以你要做的是像這樣在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();
});
}
請嘗試以前的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.