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

Nicho's avatar
Level 1

Laravel : get the id from the different tables

Hi, i'm currently developing a website, and added a feature to becoming a member. The admin can check the member historical booking, from the user_id on the member's table and customer_id from booking's table, if the user_id is the same as customer_id, it can get all the booking history. How to do it

The MemberController :

public function memberHistory(){
        $booking = Booking::where('customer_id',$id)->first();
        $member = Member::where('user_id', '=', $booking->customer_id)->get();
        $bookings = Booking::all()

        return view('Member/member_history', ['booking'=>$booking, 'member'=>$member, 'bookings'=>$bookings, 'layout'=>'memberHistory']);
    }

The database structure of member :

public function up()
    {
        Schema::create('members', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('user_id');
            $table->string('name');
            $table->string('email');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('role')->default('member');
            $table->string('member_code');
            $table->timestamps();
        });
    }

The booking structure :

Schema::create('bravo_bookings', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('code',64)->nullable();

            $table->integer('vendor_id')->nullable();
            $table->integer('customer_id')->nullable();
            $table->integer('payment_id')->nullable();
            $table->string('gateway',50)->nullable();
            $table->integer('object_id')->nullable();
            $table->string('object_model',255)->nullable();

            $table->dateTime('start_date')->nullable();
            $table->dateTime('end_date')->nullable();


            $table->decimal('total',10,2)->nullable();
            $table->integer('total_guests')->nullable();
            $table->string('currency',5)->nullable();
            $table->string('status',30)->nullable();

            $table->decimal('deposit',10,2)->nullable();
            $table->string('deposit_type',30)->nullable();

            $table->decimal('commission',10,2)->nullable();
            $table->string('commission_type',150)->nullable();

            $table->string('email',255)->nullable();
            $table->string('first_name',255)->nullable();
            $table->string('last_name',255)->nullable();
            $table->string('phone',255)->nullable();
            $table->string('address',255)->nullable();
            $table->string('address2',255)->nullable();
            $table->string('city',255)->nullable();
            $table->string('state',255)->nullable();
            $table->string('zip_code',255)->nullable();
            $table->string('country',255)->nullable();
            $table->text('customer_notes')->nullable();

//            $table->integer('vendor_commission_percent')->nullable();
//            $table->integer('vendor_commission_amount')->nullable();


            $table->integer('create_user')->nullable();
            $table->integer('update_user')->nullable();
            $table->softDeletes();

            $table->timestamps();
        });
0 likes
3 replies
Lumethys's avatar

your question is THE MOST basic relational database feature. Learn database first.

Just to be clear, i'm NOT bashing you. Database is the most important thing of an application, and a poorly designed one will make your entire app crumble. Because database is the basis on which your entire app run

And there are several things wrong with your Schema

Here is a very nice source by @Tray2

https://tray2.se/posts/database-design

https://tray2.se/posts/database-design-part-2

1 like
ederson's avatar

Hi

You will need to connect the two tables using the id on the user table and customer_id on the bookings table.

The easy way is to check https://laravel.com/docs/9.x/eloquent-relationships and follow the naming conventions to make your life easier

Instead of customer_id rename it to user_id and in the User model add the relationship

public function BravoBookings()    {
        return $this->hasMany(BravoBookings::class);
}
Lumethys's avatar

@ederson first off, there is nothing wrong with naming the foreign column customer_id, you can just write

public function BravoBookings()    {
	return $this->hasMany(BravoBookings::class, 'customer_id');
}

and judging of the presence of the vendor_id, there most likely be multiple foreign key to the users table, so that naming scheme is appropriate

What isnt is the rest of the table structure.

Please or to participate in this conversation.