Defining Laravel Relationship in Custom Primary Key and Foreign Key - php

I have 2 tables and It is One to One Relationship Model Type.
Students Table = id|nik|name|address. Accounts Table = id|nik|username|password.
In this case every student has one account and i took NIK as the $primaryKey in student model. How to define a relationship for that? Thanks in advance.
// Student Model
public function account()
{
return $this->hasOne(Account::class, 'nik', 'nik');
}
// Account Model
public function student()
{
return $this->belongsTo(Student::class, 'nik', 'nik');
}

In your students table, you should define a new column with name 'account_id' to represent the student account, and it should be nullable.
$table->unsignedBigInteger('account_id')->nullable();
$table->foreign('account_id')->references('nik')->on('accounts');
then , you can use it in your relation:
// Student Model
public function account()
{
return $this->hasOne(Account::class, 'account_id', 'nik');
}
// Account Model
public function student()
{
return $this->belongsTo(Student::class, 'account_id', 'nik');
}

You should not have one field (nik) repeated in two tables that's against database normalization. You should create student_id on Account table. That way you would be following Laravel's standard on One to One relationships.
// Student Model
public function account()
{
return $this->hasOne(Account::class);
}
// Account Model
public function student()
{
return $this->belongsTo(Student::class);
}

Related

how can I fetch data in data from 2 table using with hasOne relation in laravel model

I am trying to fetch all data from users and users_profile table with the corresponding id and user_id with using the hasOne relation, But I don't know how to make the condition to fetch data from the two tables with using the corresponding id and user_id
controller
public function index()
{
$profile = Userprofile::with('users')->get();
return view("admin.allusers",compact("profile"));
}
in user model
public function usersprofiles(){
return $this->hasOne(Userprofile::class);
}
in Userprofile model
public function users(){
return $this->belongsTo(User::class);
}
please help me to solve this problem
use the code below:
public function usersprofiles(){
return $this->hasOne(UserProfile::class, 'user_id', 'id');
}
That's assuming you have a field user_id in your users_profiles that correspond to field id in your users table.
But since you are fetching the profiles of users, your index() should be:
$profiles = User::with('usersprofiles')->get();
return view("admin.allusers", compact("profiles"));
And your User Model
public function usersprofiles(){
return $this->hasOne(Userprofile::class, 'id', 'user_id');
}
And your UserProfile Mode
public function users(){
return $this->belongsTo(User::class, 'id');
}
Hope it helps.

working on many to many table and more attributes are required in third table

i am trying to save data in third table in many to many relation but
data is not saving
user model
public function Jobs()
{
return $this->belongsToMany('App\Models\Job','App\Models\Job_User','user_id','job_id');
}
job model
public function Users()
{
return $this->belongsToMany('App\Models\User','App\Models\Job_User','job_id','user_id');
}
controller for saving data in third table is
public function JobApplied(Request $request){
$applied= new Job_User();
$applied->user_id = Auth::id();
$applied->job_id = $request->job_id;
$applied->cv = $request->cv;
$applied->current_salary = $request->current_salary;
$applied->expected_salary = $request->expected_salary;
$applied->save();
return redirect('searchjobs');
}
code of third table is
class Job_User extends Model
{
protected $fillable = [
'user_id','job_id','cv','current_salary','expected_salary','status',
];
protected $table = 'jobs_users';
}
You're using the many-to-many relation incorrectly. You don't need a model for the intermediate table as Eloquent will handle it for you.
First of all, you need to define the relation in your models in a correct way. The second argument should be the name of the intermediate table, not the model. As you're using the default values for table name and foreign key column names, you can skip them and just do:
public function Jobs()
{
return $this->belongsToMany('App\Models\Job');
}
public function Users()
{
return $this->belongsToMany('App\Models\User');
}
If you want to have additional fields in the intermediate column, you need to define it when you define a relationship using withPivot() method:
public function Jobs()
{
return $this->belongsToMany('App\Models\Job')->withPivot('cv','current_salary','expected_salary','status');
}
public function Users()
{
return $this->belongsToMany('App\Models\User')->withPivot('cv','current_salary','expected_salary','status');
}
Now, if you want to link a Job with a User and set the fields in the intermediate pivot table, you should use save() method on your relation:
$job->users()->save($user, ['cv' => $request->cv, 'current_salary' => $request->current_salary]);
or
$user->jobs()->save($job, ['cv' => $request->cv, 'current_salary' => $request->current_salary]);
Once you have data saved in your database you can retrieve data from intermediate pivot table using the pivot attribute of related model, e.g.:
foreach($user->jobs as $job) {
echo $job->pivot->current_salary;
}
or
foreach($job->users as $user) {
echo $user->pivot->current_salary;
}
Check the docs for more information about handling many-to-many relationship with Eloquent: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many

Laravel 5.1 eloquent query syntax

I have the following model relationships. If a user logs in as an employee, I want them to be able to get a list of employees for a their company and the roles they have been assigned:
class User {
// A user can be of an employee user type
public function employee()
{
return $this->hasOne('App\Employee');
}
//
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
class Employee {
// employee profile belong to a user
public function user()
{
return $this->belongsTo('App\User');
}
// employee belongs to a company
public function company()
{
return $this->belongsTo('App\Company');
}
}
class Company {
public function employees()
{
return $this->hasMany('App\Employee');
}
}
But the following query doesnt work. I get error Column not found: 1054 Unknown column companies.id in WHERE clause:
$employee = Auth::user()->employee;
$companyEmployees = Company::with(['employees.user.roles' => function ($query) use ($employee) {
$query->where('companies.id', '=', $employee->company_id)
->orderBy('users.created_at', 'desc');
}])->get();
The users and the employees table have a one to one relationship.
All employees have a base role type of employee in addition they may also have other roles such as manager, supervisor etc.
How do I write a query that gives me a company with all its employees and their roles?
I've tried to add a hasManyThrough relation to the Company model but that doesn't work either?
public function users()
{
return $this->hasManyThrough('App\User', 'App\Employee');
}
I think you're ring to get a list of coworkers for the current user and eager load the user and role?
$employee = Auth::user()->employee;
$companyEmployees = Company::with(['employees.user.roles')->find($employee->company_id);
Or perhaps:
$companyEmployees = Company::find($employee->company_id)->employees()->with('user.roles')->get();
$sorted = $companyEmployees->sortBy(function($employee){ return $employee->user->created_at; });
That might be a more direct route. Is your employee id in the user table or vice versa? The eloquent relationships are easy to set backwards.
Users::select('table_users.id')->with('roles')->join('table_employes', function($join) use ($employee) {
$join->on('table_employes.user_id','=','table_users.id')->where('table_employes.company_id', '=', $employee->company_id);
})->orderBy('tables_users.created_at')->get();
1. Create relationship for database table columns in migrtaion :
User Role
$table->foreign('user_id')->references('id')->on('users');
Users
$table->increments('id');
2. Create a model for each database table to define relationship
User.php (model)
public function userRoles()
{
return $this->hasOne('App\UserRoles', 'user_id', 'id');
}
Userroles.php (model)
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
3. Let controller handle database calls recommended to use REST api
Controller
use App\User;
use App\UserRoles;
class UserController extends Controller
{
public function index()
{
return User::with('userRoles')->orderBy('users.created_at', 'desc')->paginate(50);
}
}

Laravel, Eloquent table relating to itself through pivot table

I have users class (table):
id
username
And relations table:
user_id
subscribed_by
Idea: each User can subscribe for each other user.
Last variant was (don't work):
/* class User extends Eloquent ... */
public function followers() {
return $this->belongsToMany('User', 'rel_table')->withPivot('user_id');
}
public function following() {
return $this->belongsToMany('User', 'rel_table')->withPivot('subscribed_by');
}
Need help: how to set up this?
First, an advice, you should rename the fields in your pivot table follower_id and followed_id (or something like that). That's more clear.
Then you have to define relations like that :
public function followers() {
return $this->belongsToMany('User', 'rel_table', 'followed_id', 'follower_id');
}
public function following() {
return $this->belongsToMany('User', 'rel_table', 'follower_id', 'followed_id');
}
The withPivot method is used to define other attributes on the relation than the two foreign keys.

Laravel Pivot Table and indirect relationship

I'm writing a survey with Laravel 4 and need the ability for users to be able to take the same survey multiple times (and have their answers saved as different instances.)
I currently have a pivot table called survey_user that links a user to an invited survey. A potentially positive side effect of the pivot table is that its primary key could be used to have unique survey instances.
My problem is figuring out how to get answers, specifically through the user model. Answers table would contain a foreign key to the primary of the pivot table.
My User model:
class User extends Eloquent {
public function surveys() {
return $this->belongsToMany('Survey', 'survey_user')
->withPivot('id', 'completed_at');
}
public function answers() {
// This should return all of the user's answers, irrespective of
// survey id's.
}
}
Tables:
surveys: id
users: id
survey_user: id, survey_id, user_id, completed_at
answers: survey_user_id, answer_text, ...
How might I accomplish this psuedo-relationship or perhaps a better way to structure?
Use relationships! Here's how I would do it:
class User extends Eloquent {
public function surveys() {
return $this->belongsToMany('Survey');
}
public function answers() {
return $this->hasMany('Answer');
}
}
class Survey extends Eloquent {
public function surveys() {
return $this->belongsToMany('User');
}
public function answers() {
return $this->hasMany('Answer');
}
}
class Answer extends Eloquent {
public function survey() {
return $this->belongsTo('Survey');
}
public function user() {
return $this->belongsTo('User');
}
}

Categories