Storing password in Users table or Employees/Admin table - mysql

Let's say I have tables:
employees
administrators
(maybe) users
Employees and Administrators have different columns (e.g. Department, Salary column only in Employees).
Where should I store the password? I'm using Laravel, so the Users table is already created when I initialized the project. I'm torn between storing the password in both the Employees and Administrators, or only in the Users table, but I have no idea how I should implement the relationships of the tables if I were to add the users table.

I would probably keep the authentication associated with the users table, as per the default for laravel.
Here is an example of adding those relationships you would need.
// User model
public function employee()
{
return $this->hasOne(Employee::class, 'user_id', 'id');
}
public function administrator()
{
return $this->hasOne(Employee::class, 'user_id', 'id');
}
// Employee model
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
// Administrator model
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}

Related

How can I resolve this problem of eloquant display data?

I want to do this request with laravel ( i have 3 tables users , userprojet and projets ) users hasmany projets and projet hasmany users that's why i do another table userprojet to join the two table and now i try to extract the projet of a specific user with this request
in model projei do this method:
public function users()
{
return $this->belongsToMany(User::class, 'user_projet');
}
//model user
public function projets()
{
return $this->belongsToMany(Projet::class, 'user_projet');
}
//in the controller projet i do this method
public function getProjectsUserConnecte(){
$user = User::with('projets')->first();
return $user->projets;
}
but any thing dosen't display and i have no error
please someone know the problem ?

eloquent relation with three tables in laravel 5.4

I have three Models named as Job , JobDetail and Customer.Job has many relation with JobDetail and Customer has many relation with Job.
Below is the structure of tables.
Job
id customer_id jobname
JobDetail
id job_id days
Customer
id name
Below are the models:
class Job extends Model
{
public function job_details()
{
return $this->hasMany('App\JobDetail','job_id','id');
}
}
class Customer extends Model
{
public function customer()
{
return $this->hasMany('App\Job','customer_id','id');
}
}
class JobDetail extends Model
{
//
}
i was trying to execute the below query but its throwing error like Call to undefined relationship [customer] on model [App\Job].
My query :
$data = Job::with(['job_details','customer'])->get();
Can some body suggest me how do i connect these models and get the data ?
Thank You !
You did not specify a relationship for customer in your Job model.
So, something like this should do the trick:
public function customer()
{
return $this->belongsTo('App\Customer','job_id','id');
}

Yii2 relations with junction table

I have free tables: user, book, user_book, offers
user table has method:
public function getBooks()
{
return $this->hasMany(UserBook::className(), ['user_id' => 'id']);
}
user_book table has two fields: user_id, book_id; and methods
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getBook()
{
return $this->hasOne(Book::className(), ['id' => 'book_id']);
}
table offer have method like: getUser(), getBook(),
and now I would like show Books which user don't have. I try do something like
$query = Offer::find()
->with('user')
->andWhere([
'offer.status' => Offer::STATUS_ACTIVE,
]);
$query->andWhere(['not in', 'offer.book_id', 'user.books.book_id']);
but it doesn't work. Do you have some ideas how can I make it?
Yii2 docs, relation via junction table
In database modelling, when the multiplicity between two related
tables is many-to-many, a junction table is usually introduced. For
example, the order table and the item table may be related via a
junction table named order_item. One order will then correspond to
multiple order items, while one product item will also correspond to
multiple order items.
When declaring such relations, you would call either via() or
viaTable() to specify the junction table. The difference between via()
and viaTable() is that the former specifies the junction table in
terms of an existing relation name while the latter directly uses the
junction table. For example,
class Order extends ActiveRecord
{
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->viaTable('order_item', ['order_id' => 'id']);
}
}
or alternatively,
class Order extends ActiveRecord
{
public function getOrderItems()
{
return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems');
}
}

Yii2 How to create a globally accessible variable that converts user's id to employee's id

I have two tables that are related directly in a one-to-one relationship. One is the standard Yii2 user table (abbreviated field list here for clarity) and the other is the employee table that contains user_id. How can I create a globally accessible variable (and the actual code to access the employee id) that I can use anywhere in my application that will give me the logged in user's employee id and how would I call that variable? I wish I could say that I've tried a few things, but unfortunately I am relatively new to Yii2 and have no idea where to start with global variables like this. Thanks for any help.
user table:
id
username
password
etc
employee table:
id
user_id (related in a one-to-one relationship to the user table)
The Employee Model:
<?php
namespace frontend\models\base;
use Yii;
/**
* This is the base model class for table "employee".
*
* #property integer $id
* #property integer $user_id
*
* #property \common\models\User $user
*/
class Employee extends \yii\db\ActiveRecord
{
public function rules()
{
return [
[['user_id', 'required'],
[['user_id'], 'integer'],
[['user_id'], 'unique']
];
}
public static function tableName()
{
return 'employee';
}
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'user_id' => Yii::t('app', 'User ID'),
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(\common\models\User::className(), ['id' => 'user_id']);
}
}
A very simple way is the use of $param array
You can initially config the default value in
your_App\config\param.php
and accessing using
\Yii::$app->params['your_param_key']
Looking to your Employee model (for me ) you don't need a global var you could simply use the getUser
$myUser = Employee::user();
but you need the param you can assign using
\Yii::$app->params['my_user'] = Employee::user();
or in user
\Yii::$app->params['my_user'] = Yii::$app->user->id
or for retrive the model related to actual user from table
$myEmpModel = Employee::find()->where['user_id' => Yii::$app->user->id]->one();
I believe proper way is to use relations in your User model. First method is proper relation with activerecord, second one will get id using relation defined above it. so You will add these methods in your User model:
/**
* #return \yii\db\ActiveQuery
*/
public function getEmployee()
{
return $this->hasOne(Employee::className(), ['user_id' => 'id']);
}
public function getEmployeeId()
{
return $this->employee ? $this->employee->id : NULL; // set to NULL or anything you expect to be if record is not found
}
Then you can call it like this from everywhere in your app:
$employee_id = Yii::$app->user->identity->employeeid;
This will only work for User model because it implements Identity, otherwise you would need to instantiate model class first, lets say like this:
$user_id = 5; // 5 is id of user record in DB
$user = User::findOne($user_id);
$employee_id = $user->employeeid;
// or using first of 2 relations ...
$employee_id = $user->employee->id;

Optimal table structure for message system in Laravel 5

I'm trying to figure out how to make the most efficient messaging system for users.
My idea is that (very similar to Facebook) when a user sends a message to another user, then it creates a thread where all messages send between the users are displayed. It is important that new messages are flagged as unread for the recipient.
I want to leverage Laravels relations.
If I create a pivot table that contains the recipient id and sender id, how can I get Laravel to differentiate the 2 users using Laravels relations, so that I can easily get a list of threads where the user is involved, whether he's just the recipient, sender or both.
A thread does not need a title/subject.
I would do something like this:
class User extends Model {
public function threads() { return $this->belongsToMany(Thread::class)->withPivot('last_read_message_id'); }
}
class Thread extends Model {
public function users() { return $this->belongsToMany(User::class)->withPivot('last_read_message_id'); }
public function messages() { return $this->hasMany(ThreadMessage::class); }
}
class ThreadMessage extends Model {
public function thread() { return $this->belongsTo(Thread::class); }
public function author() { return $this->belongsTo(User::class); }
}
// List of threads where a user is involved:
Auth::user()->threads;
With this design, you can also handle groups of discussion, which is quite nice.