[英]Laravel 8: How to insert data into One To One relationship
I have a project in Laravel 8 and in this project, I have One To One relationship between movies
and imdbs
tables.我在 Laravel 8 中有一个项目,在这个项目中,
movies
和imdbs
表之间存在一对一的关系。
So I have added these to Models:所以我将这些添加到模型中:
Movie.php
: Movie.php
:
public function imdb()
{
return $this->hasOne(Imdb::class);
}
Imdb.php
: Imdb.php
:
public function movie()
{
return $this->belongsTo(Movie::class);
}
Then I created a form for adding movies.然后我创建了一个用于添加电影的表单。 Here is the
store
method:这是
store
方法:
public function store(Request $request)
{
$new = new Movie();
$new->name = $request->name;
$new->link = $request->link;
$new->year = $request->year;
$new->actor = $request->actor;
$new->director = $request->director;
$new->imdb = $request->imdb;
$new->starred = $request->starred;
$new->watched = $request->watched;
$new->save();
}
As you can see $new->imdb = $request->imdb;
如您所见
$new->imdb = $request->imdb;
is for inserting imdb rate but this data should be added to imdbs
table and not movies
table!用于插入 imdb 速率,但此数据应添加到
imdbs
表而不是movies
表!
So, how can I do that?那么,我该怎么做呢?
Here is also imdbs
table migration:这里也是
imdbs
表迁移:
public function up()
{
Schema::create('imdbs', function (Blueprint $table) {
$table->id();
$table->string('rate');
$table->integer('movie_id')->unsigned();
$table->timestamps();
});
}
@foreach($movies as $movie)
<tr>
<th scope="row">{{ $movie->id }}</th>
<td>{{ $movie->name }}</td>
<td>{{ $movie->year }}</td>
<td>@mdo</td>
<td>Mark</td>
<td>{{ $movie->imdb->rate }}</td>
<td>@mdo</td
<td><a href="">Edit</a> | <a href="">Delete</a></td>
</tr>
@endforeach
And then this gives me:然后这给了我:
ErrorException Trying to get property 'rate' of non-object
ErrorException试图获取非对象的属性“率”
And $movies
is coming from this method:而
$movies
来自这种方法:
public function index()
{
$movies = Movie::all();
return view('admin.movies.index', compact(['movies']));
}
delete column imbd from movies table then然后从电影表中删除列 imbd
public function store(Request $request)
{
$data = $request->only([
'name','link','year','actor','director','starred','watched'
]);
$movie=Movie::create($data);
$imdb=$request->only(['imbd']);
$imbd=$movie->imdb()->create($imbd);
}
On store of your Movie
model, just make an IMDB
model with the movie_id
from the model you've just created.在存储您的
Movie
model 时,只需使用您刚刚创建的movie_id
中的 movie_id 制作一个IMDB
model。
I would suggest doing some validation of your request data too, which would look something like:我建议也对您的请求数据进行一些验证,如下所示:
$data = $request->validate([
//validation fields
]);
$movie = Movie::create($data);
Otherwise, the solution to your issue is:否则,您的问题的解决方案是:
public function store(Request $request)
{
$movie = new Movie();
$movie->name = $request->name;
$movie->link = $request->link;
$movie->year = $request->year;
$movie->actor = $request->actor;
$movie->director = $request->director;
$movie->starred = $request->starred;
$movie->watched = $request->watched;
$movie->save();
if(!is_null($movie)) {
$imdb = new Imdb();
$imdb->rate = $request->imdb;
$imdb->movie_id = $movie->id;
$imdb->save();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.