Support the ongoing development of Laravel.io →
Database Forms Views

I am practising Laravel and building a blog for Activity holidays.

I have a posts table which describes the holidays, then an activities table which just has the activity name. I have a joins table that links between them. The two tables share a many-to-many relationship. What I am struggling to achieve is that I would like to add 1 or more activities in the to the table using the select form when adding the posts. Currently it is not registering the activities table but creating the posts to my blog.

I would be grateful for any advise and assistance

See my relevant code below:


// App\ActivityController

namespace App\Http\Controllers;

use App\Activity;
use Illuminate\Http\Request;

class ActivityController extends Controller
{
    protected $fillable = [
      'activity_name'
    ];

    public function posts()
    {
        return $this->belongsToMany('App\Post')->using('App\activityPost')->withTimestamps();

    }
   
    public function create()
    {

        return view('activities.create');
    }

    public function store()
    {
      $activity = new Activity;
      $activity->activity_name = request('activity_name');
      $activity->save();
      return redirect('activities/create');
    }

 
}



// App\PostController

namespace App\Http\Controllers;

use App\Post;
use Carbon\Carbon;
use Illuminate\Http\Request;


class PostController extends Controller
{

    public function __contruct()
    {
        $this->middleware('auth')->except(['index', 'show']);
    }
    

   

    public function create()
    {
      $activities = \App\Activity::pluck('activity_name', 'activity_name');
        return view('posts.create', compact('activities'));


    }

    public function store(Request $request)
    {
        $this->validate(request(), [
            'title' => 'required',
            'body' => 'required',

        ]);
      Post::create([
        'title' =>request('title'),
        'body' =>request('body'),
        'user_id' => auth()->id()
      ]);



        return redirect('/');
    }
        public function activities()
    {
        return $this->belongsToMany('App\Activity')->using('App\activityPost');
    }
 



}


//App\Post.php

namespace App;

class Post extends Model

{
    public function activities()
    {
        return $this->belongsToMany('App\Activity');
    }
    

}




// App\Activity.php

namespace App;


class Activity extends Model
{
    protected $table = 'activities';
    protected $fillable = ['activity_name'];

    public function posts()
    {
        return $this->belongsToMany('App\Post');
    }
    public function index()
    {
        $activities = Activity::orderBy('activity_name', 'asc')->paginate(10);
        return view('activities.index', ['activities'=>$activities]);
    }
    public function store()
    {
        return $this->belongsToMany('App\Post');
    }

    public function create()
    {
        return view ('activities.create');
    }


}



// Routes\web.php

<?php


Route::get('/', 'PostController@index')->name('home');
Route::get('/posts/create', 'PostController@create');
Route::post('/posts', 'PostController@store');

Route::get('/posts/activities/{activity}', 'ActivityController@index');
Route::get('/activities/create', 'ActivityController@create');
Route::post('/activities', 'ActivityController@store');

// resources\views\posts.create.blade.php

<form method="POST" action="/posts">

{{csrf_field() }}

<div class="form-group">
    <label for="title">Name</label>
    <input type="text" class="form-control" id="title" name="title" required>
</div>

<div class="form-group">
    <label for="body">Description</label>
    <textarea name="body" id="body" class="form-control" required></textarea>
</div>


<!-- create form with select multiple
convert activity into array -->


<div class="form-group">
  <label for="activities">Select your Activity</label>
  <select class="form-control" name="activities" size="{{$activities->count()}}" required multiple>
    @foreach ($activities as $activity)
    <option value="activities">
      {{$activity}}
    </option>
        @endforeach
  </select>
</div>



<div class="form-group">
    <button type="submit" class="btn btn-primary">Publish</button>
</div>

</form>


//ActivityPost Table

  public function up()
    {
        if(!Schema::hasTable('activity_post')) {
            Schema::create('activity_post', function (Blueprint $table) {
                $table->integer('activity_id')->unsigned()->index();
                $table->foreign('activity_id')->references('id')->on('activities')->onDelete('cascade');

                $table->integer('post_id')->unsigned()->index();
                $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');

            });
        }
    }

//Activity Table

    public function up()
    {
        if(!Schema::hasTable('activities')) {
            Schema::create('activities', function (Blueprint $table) {
                $table->increments('id');
                $table->string('activity_name');
                $table->timestamps();
            });
        }
    }


//Posts Table

    public function up()
    {
            if(!Schema::hasTable('posts')){
            Schema::create('posts', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('user_id');
                $table->string('title');
                $table->text('body');
                $table->timestamps();
                });
            }
    }
Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.