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.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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.
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!
How are you storing the new data for booking_guest_room?
here is model for booking_guest_info table https://pasteboard.co/JOU6MbQ.png here is my model for customer_info table https://pasteboard.co/JOU7Gjr.png here is my controller for booking_guest_info https://pasteboard.co/JOU86Ky.png also this is the guest_info controlloer https://pasteboard.co/JOU8rlQ.png
I don't use migrations as I need to learn it more so I use phpmyadmin
$guest = new Guest();
... (all other code here same)
$guest->guest_id = 1; (This should be the ID of the guest)
$guest->save();
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?
Need more info, perhaps try paste the code here in the forum and use markdown to highlight it
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.
Sorry I can't describe what I really need to do good it feels like the steps are not organized in my brain, here is the code here is model for booking_guest_info table https://pasteboard.co/JOU6MbQ.png here is my model for customer_info table https://pasteboard.co/JOU7Gjr.png here is my controller for booking_guest_info https://pasteboard.co/JOU86Ky.png also this is the guest_info controlloer https://pasteboard.co/JOU8rlQ.png
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
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');
}
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');
}```
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');
}
}
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;
}
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>
Shouldn't the rooms have many guests. And the room belongs to guest
Yes exactly but I don't how to translate this to the code
You can post your data with user_id as parameter
https://laravel.com/docs/8.x/routing#route-parameters
and insert this value in your table with your controller.
how to do it if use resources controller like Route::resource('guest', GuestController::class);
With a resource controller you'll have access to the 2 (or more) tables.
I really suggest you (re)read the docs, follow this course
https://laracasts.com/series/laravel-6-from-scratch
and maybe before https://laracasts.com/skills/laravel
and do a first simple example.
Please or to participate in this conversation.