[英]Laravel Relationship between two tables error after submitting the first post
Post Model:发布 Model:
class Post extends Model
{
protected $fillable = [
'name',
'image',
'genders_id',
'categories_id',
'sizes_id',
'price'
];
public function category()
{
return $this->hasOne(Category::class, 'id');
}
public function gender()
{
return $this->hasOne(Gender::class, 'id');
}
public function size()
{
return $this->hasOne(Size::class, 'id');
}
}
index.blade.php: index.blade.php:
@foreach ($posts as $post)
<td><img src="{{ url('storage/'. $post->image) }}" width="100" height="50"></td>
<td>{{ $post->name }}</td>
<td>{{ $post->size->name }}</td>
<td>{{ $post->category->name }}</td>
<td>{{ $post->gender->name }}</td>
<td>{{ $post->price }} $</td>
<td></td>
</tbody>
@endforeach
Posts table:帖子表:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('genders_id');
$table->string('sizes_id');
$table->string('categories_id');
$table->integer('price');
$table->string('image');
$table->timestamps();
});
}
PostsController:帖子控制器:
public function store(PostValidation $request)
{
$image = $request->file('image')->store('image');
Post::create([
'name' => $request->name,
'image' => $image,
'genders_id' => $request->gender,
'categories_id' => $request->categories,
'sizes_id' => $request->size,
'price' => $request->price
]);
return redirect(route('post.index'));
}
The issue is {{ $post->size->name }}
{{ $post->gender->name }}
{{ $post->category->name }}
works after my first post, whenever I add a second post it gives me the following error:问题是
{{ $post->size->name }}
{{ $post->gender->name }}
{{ $post->category->name }}
在我的第一篇文章之后工作,每当我添加第二个发布它给了我以下错误:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\PracticeOnly\resources\views\posts\index.blade.php) {{ $post->size->name }}
试图获取非对象的属性“名称”(查看:C:\xampp\htdocs\PracticeOnly\resources\views\posts\index.blade.php){{ $post->size->name }}
Try to update your model like following, you mixed up the order of parameters in hasOne()
:尝试更新您的 model 如下所示,您混淆了
hasOne()
中的参数顺序:
class Post extends Model
{
protected $fillable = [
'name',
'image',
'genders_id',
'categories_id',
'sizes_id',
'price'
];
public function category()
{
return $this->hasOne(Category::class,'categories_id');
}
public function gender()
{
return $this->hasOne(Gender::class,'genders_id');
}
public function size()
{
return $this->hasOne(Size::class,'sizes_id');
}
}
The standard naming scheme for 1 to many relation is singular column names:一对多关系的标准命名方案是单数列名:
'genders_id' => 'gender_id'
'categories_id' => 'category_id'
'sizes_id' => 'size_id'
Run php artisan tinker
find your Post via $post = Post::find({{id}})
and then try and access its size property via $size = $post->size
.运行
php artisan tinker
通过$post = Post::find({{id}})
找到您的帖子,然后尝试通过$size = $post->size
访问其大小属性。
You should receive null
, ie your Post/Size models relationship was never made.您应该收到
null
,即您的 Post/Size 模型关系从未建立。
Once that is confirmed, run $post->size_id
to check whether this was even set in the request, if not you are not passing the size property in your request properly.确认后,运行
$post->size_id
以检查是否在请求中设置了此项,如果没有,则您没有在请求中正确传递 size 属性。
Otherwise, I would suggest you check the foreign keys in your relationships match your table names (especially in size
), you may have overwritten the table name in the Model using the $table
property in the Size class.否则,我建议您检查关系中的外键是否匹配您的表名(尤其是在
size
中),您可能已经使用 Size class 中的$table
属性覆盖了 Model 中的表名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.