php - How to make userEmail the foreignkey connect to the emailId in Tbluser model
one text
Solution:
App\Models\Tbluser - https://laravel.com/docs/7.x/eloquent-relationships#one-to-many
use App\Models\Tblbooking;
class Tbluser
{
public function bookings()
{
return $this->hasMany(Tblbooking::class, 'userEmail', 'EmailId');
}
}
App\Models\Tbluser - https://laravel.com/docs/7.x/eloquent-relationships#one-to-many-inverse
use App\Models\Tbluser;
class Tblbooking
{
public function user()
{
return $this->belongsTo(Tbluser::class, 'EmailId', 'userEmail');
}
}
I would suggest refactoring your code and calling models User
and Booking
. You can use protected $table
to define table that model should use, i.e. protected $table = 'tbl_bookings';
.
use App\Models\User;
class Booking
{
protected $table = 'tbl_bookings';
public function user()
{
return $this->belongsTo(User::class, 'EmailId', 'userEmail');
}
}
Also it might be of benefit to have a separate table where you can store all your emails, to follow 3NF (https://en.wikipedia.org/wiki/Third_normal_form#:~:text=Third%20normal%20form%20(3NF)%20is,in%201971%20by%20Edgar%20F.). It's also a common practice to use snake case to name columns.
Source