【问题标题】:Laravel save many to many relationshipLaravel 保存多对多关系
【发布时间】:2017-09-20 08:33:30
【问题描述】:

型号

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>

如果我有两个类别并且我想选择所有类别,为什么我只得到最后一个类别?我也尝试使用 foreach,但我得到了错误偏移。 任何的想法?

我有两个选项卡窗格.. 我在选项卡 1 中存储了两个 categoryID,在选项卡 2 中存储了一个 categoryID。然后我点击提交按钮。在选项卡 1 中,标题响应两个 categoryID,但在我的数据库中,我只得到了最后一条记录

  • PostTitle[1]:sadasd
  • 类别ID[1]:1
  • 类别ID[1]:2
  • PostTitle[2]:asdasd
  • 类别ID[2]:1

当我控制台时,它显示两个 CategoryID..

https://i.stack.imgur.com/jBl2B.png

【问题讨论】:

  • 是错字吗? BelongsToMany 应该是 belongsToMany
  • 你可能想要使用 sync() 而不是 attach();
  • 您是如何在视图中获得$categories 的??
  • @linktoahref 也一样。。我换成了belongsToMany,还是只得到最后一条记录。。
  • @Maraboc 我把 $categories = Category::all();在创建控制器

标签: php laravel model laravel-eloquent


【解决方案1】:

你用这个模型做什么,它叫什么名字? 通常它是这样做的: 应用\类别:

public function posts()
{
    return $this->belongsToMany('App\Post', 'post_categories');
}

应用\发布:

public function categories()
{
    return $this->belongsToMany('App\Category', 'post_categories');
}

这两个关系不在同一个文件中,而是在连接的两个独立模型中。

【讨论】:

  • 我在我的项目中分离文件
  • 好的,先尝试找到类别,然后像这样附加它:$category = Category::find($request-&gt;categoryID); 然后$post-&gt;categories()-&gt;attach($category);
  • 我只得到了最后一条记录
【解决方案2】:

附加/分离

来自docs

Eloquent 还提供了一些额外的辅助方法来使工作 搭配相关机型更方便。例如,让我们想象一个用户 可以有很多角色,一个角色可以有很多用户。附加角色 通过在连接的中间表中插入一条记录给用户 模型,使用 attach 方法:

您在 attach 中只传递一个值,因此只获取最后一条记录,以便获取传递所有 categoryID 所需的一切

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);

`

【讨论】:

  • 我已经按照你的方法了,但还是一样。。它只得到最后一条记录。。你有什么想法吗?
  • 就像我发布的,CategoryID[1] = 1, CategoryID[1] = 2.. 在数据库中,我只得到 2.. 我认为第一个值被最后一个值覆盖。跨度>
  • 使用 print_r($title)dd() 检查数组中的内容,这将消除您对第一个值是否被最后一个值覆盖的疑问
  • dd($title);我得到了 $title 的值
  • 好的,你检查了print_r($title) 你得到的是两个值还是最后一个值??
【解决方案3】:

我找到了解决方案.. 我只需要改变视图

<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]);

【讨论】:

  • 太好了,你终于得到了你想要的
【解决方案4】:

在您看来,尝试将 multiple="multiple" 更改为只是 multiple

【讨论】:

  • 问题不在于视图中的选择框。他无法根据他的所作所为获得所有类别。
  • @MortezaRajabi yap
  • 他无法获取所有类别,因为只有最后选择的类别传递给请求,这通常是因为视图未正确发送它们
【解决方案5】:

试试这个兄弟。 我只是在控制器中更改您的代码。 我希望它可以帮助你:)

型号

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>

【讨论】:

  • 我收到这个错误“调用一个成员函数 toArray() on null”
  • 请检查我编辑了我的答案,查看控制器中的代码
  • 为什么要使用 find?如果我使用 $post = App\Post::findOrFail($id);, 我的 $post = new Post(); 怎么样?.. 我也想在 post 表中存储数据
猜你喜欢
  • 2015-03-05
  • 2014-09-02
  • 2017-07-17
  • 1970-01-01
  • 2014-05-20
  • 2014-05-31
  • 2015-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多