创建模型 post  and  user 以及 users , posts ,user_post(favorities)测试数据

    在此可以看上一篇中的数据,本次测试数据利用的上一篇的数据。detach and attach  method  在很多地方 都可以用到 而且非常方便,不用自己查询数据库后在判断是否存在,再添加数据laravel中的attach and detach  toggle method

attach  and  detach method

在关联表中添加数据 :

Route::get('/', function () {

    $post = \App\Post::find(2);
    $newUser = \App\User::find(1);
    $newUser->posts()->attach($post);
});

查看数据:

laravel中的attach and detach  toggle method

 

detach 清除数据库中的数据

Route::get('/', function () {

    $post = \App\Post::find(2);
    $newUser = \App\User::find(1);
    $newUser->posts()->detach($post);
});

laravel中的attach and detach  toggle method

toggle方法就是detach 与 attach 的结合,跟jQuery中的toggle方法差不多。

相关文章:

  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-07-08
  • 2022-12-23
  • 2021-10-28
猜你喜欢
  • 2021-06-14
  • 2022-02-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-02
  • 2022-01-16
相关资源
相似解决方案