phpmyadmin 将数据存储到数据透视表laravel

hgqdbh6s  于 2022-11-09  发布在  PHP
关注(0)|答案(2)|浏览(135)

第一个

zpgglvta

zpgglvta1#

对于存储数据,请使用

$Post->tags()->attach($request->tags);

有关更新,请使用

$Post->tags()-> sync($request->tags);

此处显示详细信息
也在这里

iszxjhcz

iszxjhcz2#

请尝试以下内容

public function store(Request $request) 
{

   // dd($request);
    //form validation
    $validatedAttributes = $this->validate($request,[
        'title' => 'required|max:60',
        'slug' => 'required|min:5|max:255|alpha_dash|unique:posts,slug',
        'category_id' => 'required|integer',
        'body' => 'required'
    ]);

    // store into database
    $Post = Post::create($validatedAttributes);

    $Post->tags()->sync($request->tags);

    Session::flash('success', 'The Blog Post was Successfully save');

    // to redirect to other page
    return redirect()->route('post.show',$Post->id);
}

相关问题