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

Alewa's avatar
Level 2

Laravel Booking Code

I have created a booking event website and i want to write the booking.store code, but i don't know how to write it? 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('client_id')->unsigned();
            $table->BigInteger('approval_id')->unsigned()->nullable();
            $table->string('status');
            $table->foreign('client_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');
            $table->foreign('approval_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');
            $table->timestamps();
        });
    }

event_booking database table

public function up()
    {
        Schema::create('event_booking', function (Blueprint $table) {
            $table->id();
            $table->integer('booking_id');
            $table->integer('event_id');
            $table->timestamps();
        });
    }

web.php file

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.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 client(){
	     return $this->belongsTo('App\Models\User', 'client_id');
    }

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

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

On my event-details.blade.php file, i have a form for booking eg. <form action="{{ route('booking.store') }}" method="post"> and what i want is that, when i click on the booking form, my code should insert user_id, event_id automatically. Any help on how to write my booking code in my BookingCotroller?

0 likes
2 replies
Alewa's avatar
Level 2

BookingController.php

<?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)
    {
        $this->validate($request,[
          'status' => 'required',
          'events' => 'required',
        ]);

        $booking = New Booking();
        $booking->client_id = Auth::id();
        $booking->status = 'Pending';
        $booking->save();
        $booking->events()->attach($request->events);

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

@alewa you should be able to do:

  $booking->client()->attach(Auth::id());

Please or to participate in this conversation.