public function store(Request $request)
{
$article = Article::create($request->only(['content']));
$categories = explode(",",$request->get('categories'));
$category_ids =[];
foreach ($categories as $category) {
$category_db = Category::where('name', trim($category))->firstOrCreate(['name'=> trim($category)]);
$category_ids [] = $category_db->id;
}
$article->category()->attach($category_ids);
return response("Successfully Added to Database");
}
最后没有逗号也没有问题 , 在数据库中一切都很好
现在的问题是,当我在末尾放一个逗号时,数据库中会创建一个带有空格的单元格
我知道为什么会这样。包含值的分解数组的最后一个键是空格。但我想省略或留下那个空钥匙。我怎样才能做到这一点?
慕侠2389804