honeyBear
1 week ago
145
11
Laravel

Register a Team to a Post

Posted 1 week ago by honeyBear

Currently, I am trying to register Team to a Post. Team belongstoMany Post and Post belongstoMany Team. But the data is not stored into the pivot table, am I doing it the wrong way? Below is my code. Assume that user has already created his/her Team before registering his/her Team to desire Post which is a tournament event.

(blade file posts.show)

<form method="post" action="/team/register">
   @csrf
   <a class="btn btn-outline-dark btn-sm">
          {{ __('Register') }}
   </a>
</form>

(routes file)

Route::post('/team/register', '[email protected]');

([email protected])

    public function register()
    {
        $post = Post::find('id', '=', $post->id);
        $team = Team::where('id', '=', $team->id);
        $post->teams()->attach($team);

        return redirect()->back()->with('success', 'Team Registered');
    }

([email protected])

    public function show(Post $post, Bracket $bracket)
    {
        return view('posts.show', [
            'post' => $post,
            'bracket' => $bracket,
        ]);
    }

(pivot table migration file)

    public function up()
    {
        Schema::create('post_team', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('team_id');
            $table->unsignedBigInteger('post_id');
            $table->timestamps();
        });
    }

Please sign in or create an account to participate in this conversation.