视图未返回存储在数据库中的属性
View is not returning attributes stored in database
我正在尝试 return 哪个用户发表了评论,以及他们发表评论的时间。
我有模型求评论
use Illuminate\Database\Eloquent\Model;
class comments extends Model
{
protected $guarded = [];
public function adjustments()
{
return $this->belongsToMany(User::class, 'adjustments')
->withTimestamps();
}
}
跟踪哪些用户发布了哪些评论的枢轴table
public function up()
{
Schema::create('adjustments', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->unsignedInteger('comments_id')->index();
$table->timestamps();
});
}
空调整模型
use Illuminate\Database\Eloquent\Model;
class adjustment extends Model
{
//
}
在 php artisan tinker
中,当 $comment = App\comment::first();
和 $user = App\user::first();
我能够使用 $comment->adjustments()->attach($user->id)
成功地将 user_id 附加到 comment_id 并且调用 App\adjustments::all();
将正确 return
=> Illuminate\Database\Eloquent\Collection {#2935
all: [
App\adjustment {#2941
id: 1,
user_id: 1,
comments_id: 1,
created_at: "2019-11-16 17:05:10",
updated_at: "2019-11-16 17:05:10",
},
],
}
当我试图在我的视图中显示调整时,我得到一个空列表。
@foreach ($comment->adjustments as $user)
<li>{{$user->name}} on {{$user->pivot->updated_at}}</li>
@endforeach
在我的产品控制器中(用户对产品发表评论)我的展示功能中有以下代码
public function show(products $product, comments $comment, User $user)
{
return view ('products.show', compact('product'), compact('comment'));
}
这里不需要枢轴table。因为在这里你有一对多的关系。用户可以创建许多评论。 & 一个评论属于一个 user.In 用户模型添加这个
public function comments()
{
return $this->hasMany(Comment::class);
}
& 评论 table 你有外键 user_id.
在评论模型中
public function user()
{
return $this->belongsTo(User::class,'user_id','id')
}
public function show(products $product, comments $comment, User $user)
{
$comments=Comment::with('user')->all();
return view ('products.show', compact(['product','comments']));
}
@foreach ($comments as $comment)
<li>{{$comment->user->name}} on {{$comment->updated_at}}</li>
@endforeach
然后你可以访问一个用户的所有评论table数据
我正在尝试 return 哪个用户发表了评论,以及他们发表评论的时间。
我有模型求评论
use Illuminate\Database\Eloquent\Model;
class comments extends Model
{
protected $guarded = [];
public function adjustments()
{
return $this->belongsToMany(User::class, 'adjustments')
->withTimestamps();
}
}
跟踪哪些用户发布了哪些评论的枢轴table
public function up()
{
Schema::create('adjustments', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->unsignedInteger('comments_id')->index();
$table->timestamps();
});
}
空调整模型
use Illuminate\Database\Eloquent\Model;
class adjustment extends Model
{
//
}
在 php artisan tinker
中,当 $comment = App\comment::first();
和 $user = App\user::first();
我能够使用 $comment->adjustments()->attach($user->id)
成功地将 user_id 附加到 comment_id 并且调用 App\adjustments::all();
将正确 return
=> Illuminate\Database\Eloquent\Collection {#2935 all: [ App\adjustment {#2941 id: 1, user_id: 1, comments_id: 1, created_at: "2019-11-16 17:05:10", updated_at: "2019-11-16 17:05:10", }, ], }
当我试图在我的视图中显示调整时,我得到一个空列表。
@foreach ($comment->adjustments as $user)
<li>{{$user->name}} on {{$user->pivot->updated_at}}</li>
@endforeach
在我的产品控制器中(用户对产品发表评论)我的展示功能中有以下代码
public function show(products $product, comments $comment, User $user)
{
return view ('products.show', compact('product'), compact('comment'));
}
这里不需要枢轴table。因为在这里你有一对多的关系。用户可以创建许多评论。 & 一个评论属于一个 user.In 用户模型添加这个
public function comments()
{
return $this->hasMany(Comment::class);
}
& 评论 table 你有外键 user_id.
在评论模型中
public function user()
{
return $this->belongsTo(User::class,'user_id','id')
}
public function show(products $product, comments $comment, User $user)
{
$comments=Comment::with('user')->all();
return view ('products.show', compact(['product','comments']));
}
@foreach ($comments as $comment)
<li>{{$comment->user->name}} on {{$comment->updated_at}}</li>
@endforeach
然后你可以访问一个用户的所有评论table数据