Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alewa's avatar
Level 2

Laravel event booking error

I want to insert event_id into my bookings database table events database table

public function up()
    {
        Schema::create('events', function (Blueprint $table) {
            $table->id();
            $table->BigInteger('user_id')->unsigned();
            $table->string('title');
            $table->string('slug');
            $table->string('location');
            $table->date('start_date');
            $table->date('end_date');
            $table->time('start_time');
            $table->time('end_time');
            $table->text('description')->nullable();
            $table->string('status');
            $table->string('image')->default('default.png');
            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');
            $table->timestamps();
        });
    }

bookings database table

public function up()
    {
        Schema::create('bookings', function (Blueprint $table) {
            $table->id();
            $table->BigInteger('user_id')->unsigned()->nullable();
            $table->string('user_name');
            $table->string('user_email');
            $table->BigInteger('approval_id')->unsigned()->nullable();
            $table->BigInteger('event_id')->unsigned();
            $table->string('event_name');
            $table->string('status');
            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');
            $table->foreign('approval_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');
            $table->timestamps();
        });
    }

Event.php Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
    use HasFactory;

    public function user(){
	     return $this->belongsTo('App\Models\User', 'user_id');
    }
}

Booking.php Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Booking extends Model
{
    use HasFactory;

    public function approval(){
	    return $this->belongsTo('App\Models\User', 'approval_id');
    }

    public function event(){
      return $this->belongsTo('App\Models\Event');
    }
}

web.php

Route::get('/','WelcomeController@index')->name('welcome');
Route::get('ongoing-event','EventController@index')->name('event.index');
Route::get('past-event','EventController@pastEvent')->name('event.past');
Route::get('event/{slug}','EventController@details')->name('event.details');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('booking','BookingController@store')->name('booking.store');

event-details.php

<section class = "rooms sec-width" id = "rooms">
            <div class = "title">
                <h2>event detail</h2>
            </div>
            <div class = "rooms-container">
                <!-- single room -->
                <article class = "room">
                    <div class = "room-image">
                        <img src = "{{ asset('public/storage/event/'.$event->image) }}" alt = "room image" style="width:1200px; height:400px;">
                    </div>
                    <div class = "room-text">
                        <h3>{{ $event->title }}</h3>
                        <ul>
                          <li>
                              <i class = "fas fa-arrow-alt-circle-right"></i>
                              Start Date: {{ $event->start_date }}
                          </li>
                          <li>
                              <i class = "fas fa-arrow-alt-circle-right"></i>
                              End Date: {{ $event->end_date }}
                          </li>
                          <li>
                              <i class = "fas fa-arrow-alt-circle-right"></i>
                              Start Time: {{ $event->start_time }}
                          </li>
                          <li>
                              <i class = "fas fa-arrow-alt-circle-right"></i>
                              End Time: {{ $event->end_time }}
                          </li>
                        </ul>
                        <p>{!! $event->description !!}</p>
                        <p class = "rate">
                            <span>.00 /</span> Per Night
                        </p>
                        @guest
                        <a href="#" class ="btn">Login</a>
                        @else
                        <form action="{{ route('booking.store') }}" method="post">
                          {{ csrf_field() }}
                          <input type="hidden" name="event_id" value="{{ $event->id }}">
                          <button type="submit" class="btn">book now</button>
                        </form>
                        @endguest
                    </div>
                </article>
                <!-- end of single room -->
                <!-- single room -->
            </div>
        </section>

BookingController

<?php

namespace App\Http\Controllers;

use App\Models\Booking;
use App\Models\Event;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class BookingController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $event = Event::where('event_id' => $event_id);

        $data = New Booking();
        $data['user_id'] = Auth::user()->id;
        $data['user_name'] = Auth::user()->name;
        $data['user_email'] = Auth::user()->email;
        $data['event_id'] = $event;
        $data['status'] = 'Pending';
        $data->save();

        return redirect()->back()->with('message', 'Event Booked Successfully');
    }
}

So how do i insert the id of event into event_id in bookings database, can some one help me on how to insert event_id in the BookingController?

0 likes
1 reply
Atoagustyn's avatar

@alewa you need to select the event_id from the event table from your create method and insert in to bookings table using $events = Event::all(); return view ('bookings.create', compact('events')) ;

                  <label for="events"> Event Title</label>
                  <select name="event_id">
                      <option value="">Please select Event</option>
                      @foreach ($events as $event)
                      <option value="{{ $events->id }}"  {{  (old('event_id') == $event->id)? 'selected' : '' }} >{{ $event->title }}
                      </option>
                      @endforeach
                  </select>
              </div>

Please or to participate in this conversation.