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

amrcodes's avatar

Foreign Key doesn't have a default value

Can I get help with the foreign key problem?

I have two tables that I created on PHPMyAdmin the first table is customer_info the second table is booking_guest_room I created a foreign key [user_id] in the booking_guest_room I added fillable for user_id but when I try to store new data for booking_guest_room it throws an error "user_is doesn't have a default value"

I'm struggling with this problem for 3 days and I'm not sure what I'm doing wrong. Sorry for my weak English https://cdn.discordapp.com/attachments/486650445297877003/811889719243440128/unknown.png

0 likes
20 replies
sr57's avatar

user_is or user_id ? :-)

Assuming user_id, the error just tells you that you don't input a value for that field

Nothing to do with foreign key, but of course the value you'll enter should exist in users due this time to foreign key.

amrcodes's avatar

sorry user_id, but I'm a bit confused I'm not sure how to insert the value of the foreign id to the new data!

devingray_'s avatar

How are you storing the new data for booking_guest_room?

devingray_'s avatar
$guest = new Guest();

... (all other code here same)

$guest->guest_id = 1; (This should be the ID of the guest)

$guest->save();

amrcodes's avatar

Thank you but, this is inserting the guest_id to the controller manually! How can I pass it automatically to match the entered data, what I'm trying to say is if I want add new row inside the booking_guest_room that relates to a user inside the customer_info table what should so that once I insert the data inside the booking_guest_room it creates automatically a new id inside the customer_info?

devingray_'s avatar

Need more info, perhaps try paste the code here in the forum and use markdown to highlight it

sr57's avatar

I'm not sure how to insert the value of the foreign id to the new data!

and we don't understand how you try to do it?

Please share your code.

sr57's avatar

You are welcome, one step after the other

First, it's better (easy to read) to share code here, put it between 3 `

first code :

...
blabla

second one

blabla2
amrcodes's avatar

This is the GuestRoom controller which contains the user room info

`public function store(Request $request) {

    $room = new Room;
    $room->room_nums = $request->room_num;
    $room->room_type = $request->room_type;
    $room->adults = $request->adults;
    $room->child = $request->child;
    
    dd($room->id);
    $room->save();
    return redirect('/guest');
    
}
amrcodes's avatar

This is the guest_info controller

    {
        $guest = new Guest;
        $guest->guest_name = $request->name;
        $guest->nationality = $request->nationality;
        $guest->email = $request->email;
        $guest->mobile_num = $request->mobile;
        $guest->save();
        return redirect('/res');
    }```
amrcodes's avatar

This is the room info model

class Room extends Model
{
    use HasFactory;
    public $timestamps = false;
    protected $table = 'booking_guest_room';
    protected $fillable = ['user_id', 'id'];

    public function rooms(){
        return $this->hasMany(Room::class, 'user_id');
    }
}
amrcodes's avatar

This is the guest info model

namespace App\Models;

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

class Guest extends Model
{
    
    use HasFactory;
    protected $table = 'customer_info';
    public $timestamps = false;
    


   
}
amrcodes's avatar

This is the blade where I enter the room info and submit it

                <div class="step">
                    <form  method="post" action="{{route('res.store')}}">            
                        @method('POST')
                        @csrf
                    <div class="row">
                        <div class="col-md-12 col-sm-12">
                            <div class="form-group">
                                <table class="table table-striped table-inverse table-responsive form" >
                                    <thead class="thead-inverse">
                                        <tr class="w-5">
                                            <!-- <th>Room NO.</th> -->
                                            <th>نوع الغرفة</th>
                                            <th>الكبار</th>
                                            <th>الاطفال</th>
                                            <!-- <th>Child Ages</th> -->
                                            <th>احذف الغرفة</th>
                                        </tr>
                                        </thead>
                                    <tbody id='myTable'>
                                    {{-- here goes the table info --}}
                                    </tbody>
                                    <button class="add rounded btn btn-danger " type="button">اضف الغرفة</button>
                                    <!-- <button class="remove">Remove Room</button> -->
                                    <input type="number" class="numbers" value="" name="room_num" min="0" placeholder="عدد الغرف">
                                </table>
                            </div>
                        </div>
                        
                    </div>
                    <input type="number" name="user_id" id="" value="{{$room->id}}">

                    <hr>

                    <button class="btn btn-info" >Book now</button>
EricZwart's avatar

Shouldn't the rooms have many guests. And the room belongs to guest

amrcodes's avatar

Yes exactly but I don't how to translate this to the code

amrcodes's avatar

how to do it if use resources controller like Route::resource('guest', GuestController::class);

Please or to participate in this conversation.