在 Laravel Livewire 中使用外键从另一个 table 获取特定数据
Get specific data from another table using foreign key in Laravel Livewire
我是 Livewire 的新用户,我想在我的博客 table 中使用用户的外键从用户 table 获取特定名称。我不确定如何将代码放入 render()
和 Blade 模板中。我见过这样的事情:{{ blogs->id->name }}
。我怎样才能想出解决方案?
用户table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
博客table
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('title');
$table->text('heading')->nullable();
$table->text('paragraph');
$table->text('featured_img');
$table->timestamps();
});
在声明的用户模型中
public function blogs()
{
return $this->hasMany(Blogs::class);
}
在博客模型中声明
public function user()
{
return $this->belongsTo(User::class);
}
博客中的 render() 方法 class
public function render()
{
$blogs = Blogs::where('user_id', Auth::id())->orderBy('created_at', 'desc')->paginate(3);
$user = User::where('id', Auth::id())->get();
return view('livewire.admin.blogs', [
'blogs' => $blogs,
'user' => $user
])->layout('layouts.admin');
}
看起来您已经认识该用户并想要 return 该特定用户的所有博客 post?如果是这种情况,您可以对您定义的关系执行以下操作。我还假设您想要登录用户的博客 post。
public function render()
{
return view('livewire.admin.blogs', [
// No need to add the posts here, get them from the user model
'user' => auth()->user(),
])->layout('layouts.admin');
}
然后在您的 blade 中为用户遍历博客 post:
<p>The blog posts for {{ $user->name }}</p>
@foreach ($user->blogs()->paginate(3) as $post)
<h1>{{ $post->title }}</h1>
<p>{{ $post->heading }}</p>
@endforeach
更新以获取用户
如果您想获取特定博客的用户 post,您可以在博客模型本身上使用用户关系。
// From the Blog model access the User relationship and return
// the `name` for the specific user who created the blog post.
$post->user->name
我是 Livewire 的新用户,我想在我的博客 table 中使用用户的外键从用户 table 获取特定名称。我不确定如何将代码放入 render()
和 Blade 模板中。我见过这样的事情:{{ blogs->id->name }}
。我怎样才能想出解决方案?
用户table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
博客table
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('title');
$table->text('heading')->nullable();
$table->text('paragraph');
$table->text('featured_img');
$table->timestamps();
});
在声明的用户模型中
public function blogs()
{
return $this->hasMany(Blogs::class);
}
在博客模型中声明
public function user()
{
return $this->belongsTo(User::class);
}
博客中的 render() 方法 class
public function render()
{
$blogs = Blogs::where('user_id', Auth::id())->orderBy('created_at', 'desc')->paginate(3);
$user = User::where('id', Auth::id())->get();
return view('livewire.admin.blogs', [
'blogs' => $blogs,
'user' => $user
])->layout('layouts.admin');
}
看起来您已经认识该用户并想要 return 该特定用户的所有博客 post?如果是这种情况,您可以对您定义的关系执行以下操作。我还假设您想要登录用户的博客 post。
public function render()
{
return view('livewire.admin.blogs', [
// No need to add the posts here, get them from the user model
'user' => auth()->user(),
])->layout('layouts.admin');
}
然后在您的 blade 中为用户遍历博客 post:
<p>The blog posts for {{ $user->name }}</p>
@foreach ($user->blogs()->paginate(3) as $post)
<h1>{{ $post->title }}</h1>
<p>{{ $post->heading }}</p>
@endforeach
更新以获取用户
如果您想获取特定博客的用户 post,您可以在博客模型本身上使用用户关系。
// From the Blog model access the User relationship and return
// the `name` for the specific user who created the blog post.
$post->user->name