如何使用 laravel 7 在数据库中传递数组

How to pass array in db using laravel 7

我正在尝试在数据库中发送数组数据,但出现错误 这是我在 db

中发送数据的函数
 public function create(Request $request)
  {
      $tags = explode(",", $request->labels);
      $posts = new Post();
    $posts->user_id = $request->id;
    $posts->title = $request->title;
    $posts->description = $request->description;
    $tags = explode(",", $request->labels);

    $posts->tags = $tags; // error is here in this line
    $posts->save();
      return redirect()->back()->with('message' , 'post created!!!!!');

  }

如果 $posts->tags 是相关的 - 即 Post hasMany Tag,Tag belongsToMany Post - 那么你应该能够像这样将标签附加到 post:

$posts->tags()->attach($tags);
$posts->save();