Laravel 保存多对多关系
Laravel save many to many relationship
型号
public function categories()
{
return $this->belongsToMany('App\Category', 'post_categories');
}
public function posts()
{
return $this->belongsToMany('App\Post', 'post_categories');
}
控制器
public $DEFAULT_LANGUAGE = 1;
public function store(Request $request)
{
$titles = $request->PostTitle;
$post = new Post();
$post->PostTitle = $titles[$this->DEFAULT_LANGUAGE];
$post->save();
$post->categories()->attach($request->categoryID[$this->DEFAULT_LANGUAGE]);
unset($titles[$this->DEFAULT_LANGUAGE]);
foreach ($titles as $key => $title) {
$post = new Post();
$post->PostTitle = $request->PostTitle[$key];
$post->save();
$post->categories()->attach($request->categoryID[$key]);
}
return response()->json($post);
查看
<select class="form-control category-list" name="categoryID[]" multiple="multiple" data-placeholder="Pilih Kategori">
@foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->CategoryName }}
</option>
@endforeach
</select>
如果我有两个类别并且我想 select 所有类别,为什么我只得到最后一个类别?我也尝试使用 foreach,但我得到了错误偏移量。
任何的想法?
我有两个选项卡窗格..
我在选项卡 1 中存储两个 categoryID,在选项卡 2 中存储一个 categoryID。然后我单击提交按钮。header 在选项卡 1 中响应两个 categoryID,但在我的数据库中,我只得到最后一条记录
- 文章标题[1]:sadasd
- 类别ID[1]:1
- 类别ID[1]:2
- 文章标题[2]:asdasd
- 类别ID[2]:1
控制台显示两个CategoryID..
https://i.stack.imgur.com/jBl2B.png
你用这个模型做什么,它叫什么名字?
通常它是这样做的:
App\Category:
public function posts()
{
return $this->belongsToMany('App\Post', 'post_categories');
}
App\Post:
public function categories()
{
return $this->belongsToMany('App\Category', 'post_categories');
}
这两个关系不在同一个文件中,而是在连接的两个独立模型中。
兄弟,试试这个。
我只是在控制器中更改您的代码。
希望对您有所帮助:)
型号
public function categories()
{
return $this->BelongsToMany('App\Category', 'post_categories');
}
public function posts()
{
return $this->BelongsToMany('App\Post', 'post_categories');
}
控制器
$post = App\Post::findOrFail($id);
$post = $post->load('categories');
$attributes = $post->attributes->toArray();
查看
<select class="form-control category-list" name="categoryID[]" multiple="multiple" data-placeholder="Pilih Kategori">
@foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->CategoryName }}
</option>
@endforeach
</select>
在您看来,尝试将 multiple="multiple" 更改为 multiple
附加/分离
来自docs
Eloquent also provides a few additional helper methods to make working
with related models more convenient. For example, let's imagine a user
can have many roles and a role can have many users. To attach a role
to a user by inserting a record in the intermediate table that joins
the models, use the attach method:
您在附加中仅传递一个值,因此仅获取最后一条记录,以便获得传递所有类别 ID 所需的一切
public $DEFAULT_LANGUAGE = 1;
public function store(Request $request)
{
$titles = $request->PostTitle;
$post = new Post();
$post->PostTitle = $titles[$this->DEFAULT_LANGUAGE];
$post->save();
$post->categories()->attach($request->categoryID[$this->DEFAULT_LANGUAGE]);
unset($titles[$this->DEFAULT_LANGUAGE]);
$data= array();
foreach ($titles as $key => $title) {
$post = new Post();
$post->PostTitle = $request->PostTitle[$key];
$post->save();
$data[]= $post->categories()->attach($request->categoryID[$key]);
}
return response()->json($data);
`
我找到了解决方案..
我只需要改变视图
<select class="form-control category-list" name="categoryID[{{$language->id}}][]" multiple data-placeholder="Pilih Kategori">
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->CategoryName}}</option>
@endforeach
</select>
同时更换控制器
$post->categories()->attach($request->categoryID[$this->DEFAULT_LANGUAGE]);
型号
public function categories()
{
return $this->belongsToMany('App\Category', 'post_categories');
}
public function posts()
{
return $this->belongsToMany('App\Post', 'post_categories');
}
控制器
public $DEFAULT_LANGUAGE = 1;
public function store(Request $request)
{
$titles = $request->PostTitle;
$post = new Post();
$post->PostTitle = $titles[$this->DEFAULT_LANGUAGE];
$post->save();
$post->categories()->attach($request->categoryID[$this->DEFAULT_LANGUAGE]);
unset($titles[$this->DEFAULT_LANGUAGE]);
foreach ($titles as $key => $title) {
$post = new Post();
$post->PostTitle = $request->PostTitle[$key];
$post->save();
$post->categories()->attach($request->categoryID[$key]);
}
return response()->json($post);
查看
<select class="form-control category-list" name="categoryID[]" multiple="multiple" data-placeholder="Pilih Kategori">
@foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->CategoryName }}
</option>
@endforeach
</select>
如果我有两个类别并且我想 select 所有类别,为什么我只得到最后一个类别?我也尝试使用 foreach,但我得到了错误偏移量。 任何的想法?
我有两个选项卡窗格.. 我在选项卡 1 中存储两个 categoryID,在选项卡 2 中存储一个 categoryID。然后我单击提交按钮。header 在选项卡 1 中响应两个 categoryID,但在我的数据库中,我只得到最后一条记录
- 文章标题[1]:sadasd
- 类别ID[1]:1
- 类别ID[1]:2
- 文章标题[2]:asdasd
- 类别ID[2]:1
控制台显示两个CategoryID..
https://i.stack.imgur.com/jBl2B.png
你用这个模型做什么,它叫什么名字? 通常它是这样做的: App\Category:
public function posts()
{
return $this->belongsToMany('App\Post', 'post_categories');
}
App\Post:
public function categories()
{
return $this->belongsToMany('App\Category', 'post_categories');
}
这两个关系不在同一个文件中,而是在连接的两个独立模型中。
兄弟,试试这个。 我只是在控制器中更改您的代码。 希望对您有所帮助:)
型号
public function categories()
{
return $this->BelongsToMany('App\Category', 'post_categories');
}
public function posts()
{
return $this->BelongsToMany('App\Post', 'post_categories');
}
控制器
$post = App\Post::findOrFail($id);
$post = $post->load('categories');
$attributes = $post->attributes->toArray();
查看
<select class="form-control category-list" name="categoryID[]" multiple="multiple" data-placeholder="Pilih Kategori">
@foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->CategoryName }}
</option>
@endforeach
</select>
在您看来,尝试将 multiple="multiple" 更改为 multiple
附加/分离
来自docs
Eloquent also provides a few additional helper methods to make working with related models more convenient. For example, let's imagine a user can have many roles and a role can have many users. To attach a role to a user by inserting a record in the intermediate table that joins the models, use the attach method:
您在附加中仅传递一个值,因此仅获取最后一条记录,以便获得传递所有类别 ID 所需的一切
public $DEFAULT_LANGUAGE = 1;
public function store(Request $request)
{
$titles = $request->PostTitle;
$post = new Post();
$post->PostTitle = $titles[$this->DEFAULT_LANGUAGE];
$post->save();
$post->categories()->attach($request->categoryID[$this->DEFAULT_LANGUAGE]);
unset($titles[$this->DEFAULT_LANGUAGE]);
$data= array();
foreach ($titles as $key => $title) {
$post = new Post();
$post->PostTitle = $request->PostTitle[$key];
$post->save();
$data[]= $post->categories()->attach($request->categoryID[$key]);
}
return response()->json($data);
`
我找到了解决方案.. 我只需要改变视图
<select class="form-control category-list" name="categoryID[{{$language->id}}][]" multiple data-placeholder="Pilih Kategori">
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->CategoryName}}</option>
@endforeach
</select>
同时更换控制器
$post->categories()->attach($request->categoryID[$this->DEFAULT_LANGUAGE]);