Data value retrieve is not the same as in database - Laravel - php

I've created 3 tables. User, student and lecture. The user could be a student or a lecture depending on the type on the user table. Student type = "std" while lecture type = "lct". The problem is, when i log in as a lecture, the type value will always be "std" eventhough in the database it is "lct". New in Laravel. Thanks
User table
user_id | password | type
a123 | 1234 | std
b345 | 1234 | lct
Student table
student_id | name
a123 | david
Lecture table
lecture_id | name
b345 | Mr Steve
User model
protected $primaryKey = 'user_id';
public function student(){
return $this->hasOne(student::class,'student_id');
}
public function lecture(){
return $this->hasOne(lecture::class,'lecture_id');
}
Student model (Lecture model also has this but with PK = lecture_id)
protected $primaryKey = 'student_id';
public function User()
{
return $this->belongsTo(User::class,'user_id');
}
HomeController
public function index()
{
$type = Auth::user()->type;
dd($type);
}

Check this. Just assuming
protected $primaryKey = 'user_id';
public function student(){
return $this->hasOne(Student::class,'student_id'); // It should be Student not student I guess (Capital S)
}
public function lecture(){
return $this->hasOne(Lecture::class,'lecture_id'); // Lecture not lecture
}
Check you model class name once.

Related

Laravel Eloquent for pivot table with 2 foreign keys to a table and 1 foreign key to another table

I have tables as follows where role_id is the foreign key of the roles table and user_id and setter_id are the foreign key of the users table.
table 1:
+---------------------+
| users |
+---------------------+
| id |
| name |
| email |
| password |
+---------------------+
table 2:
+---------------------+
| roles |
+---------------------+
| id |
| name |
+---------------------+
pivot table:
+---------------------+
| role_user |
+---------------------+
| role_id |
| user_id |
| setter_id |
+---------------------+
The models I have defined:
User Model:
class User extends Model
{
public $timestamps = false;
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
Role Model:
class Role extends Model
{
public $timestamps = false;
public function users()
{
return $this->belongsToMany(User::class);
}
}
How do I change my models so that I can get the data as shown below?
user -> roles -> setter : The user and its roles and the setter of each role for user
Thank you...
You will never be able to access the setter by calling it in the roles collection.
This is wrong:
$user->roles->setter
Let's see an example that will work:
foreach($user->roles as $role)
{
dd($role->pivot->setter)
}
To be able to do that, you need to change your models to reflect something like this:
User
class User extends Model
{
public $timestamps = false;
public function roles()
{
return $this->belongsToMany(Role::class)
->using(UserRolePivot::class)
->withPivot([
'role_id',
'user_id',
'setter_id',
]);
}
}
Role
class Role extends Model
{
public $timestamps = false;
public function users()
{
return $this->belongsToMany(User::class)
->using(UserRolePivot::class)
->withPivot([
'role_id',
'user_id',
'setter_id',
]);
}
}
Pivot
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserRolePivot extends Pivot
{
protected $fillable = [
'role_id',
'user_id',
'setter_id',
];
public function role()
{
return $this->belongsTo(Role::class , 'role_id');
}
public function user()
{
return $this->belongsTo(User::class , 'user_id');
}
public function setter()
{
return $this->belongsTo(User::class , 'setter_id);
}
}
You can either update the belongsToMany calls to include the setter_id on the pivot aswell, and then access it via ->pivot->setter_id and retrieve a model using that id.
return $this->belongsToMany(Role::class)->withPivot('setter_id');
Or (and what I would personally go for) you could define a custom pivot model, and create a setter() relationship there, so you could retrieve the model directly from the pivot.

Laravel user model with multiple one to one relationship

I want to set the User model to have a one-to-one relationship with the Student model as well as the Lecture model also.
I'm login in using the user table and from then it will map to the student or lecture table depending on the user type.
The problem is when I run app\user::find('a123')->lecture it will return the b345(Mr Steven) data.
And when I run app\user::find('b345')->student, it returns the a123(david) data.
Both result should have produce NULL because it doesnt matches the id.
New in Laravel thanks.
User table
id | password | type
a123 | 1234 | student
b345 | 1234 | lecture
Lecture table
lecture_id | id | name
b345 | b345 | Mr Steve
Student table
student_id | id | name
a123 | a123 | david
User model
public function student(){
return $this->hasOne(student::class,'student_id');
}
public function lecture(){
return $this->hasOne(lecture::class,'lecture_id');
}
Lecture model
protected $primaryKey = 'lecture_id';
public function User()
{
return $this->belongsTo(User::class,'id');
}
Student model
protected $primaryKey = 'student_id';
public function User()
{
return $this->belongsTo(User::class,'id');
}

Laravel eloquent has hasmany of hasmany

How to show all company who has photo attachment?
company:
id | name
user:
id | name | company_id
post:
id | user_id | text
post_attachment:
id | post_id | path | type
company model:
public function users() {
return $this->hasMany('App\User')->orderBy('id', 'ASC');
}
user model:
public function posts() {
return $this->hasMany('App\Post');
}
posts model:
public function images(){
return $this->hasMany('App\PostsAttachment')->where("type", "image");
}
I want to get all company who have user(s) that have post(s) with minimal 2 images attachment. Anyone can help me?
I tried Company::has('users.posts.images', '<', 2)->get(); but it also give companies who has user image below 2.
Using eloquent you could write your logic as
$companies = Company::has('users.posts.images')->get();
See Querying Relationship Existence

Laravel 5.4 | Fetching data from Pivot Table

I have 4 tables in my database:
Table 1: Category
---|------
id | name
---|------
1 | Cars
In 'Category' model class I have defined the following relationship:
class Category {
public function fields() {
return $this->belongsToMany('App\Field');
}
}
Table 2: Field
id | name
---|-------
1 | Make
In 'Field' model class I have defined the following relationship:
class Field {
public function categories() {
return $this->belongsToMany('App\Category');
}
}
Table 3: Field_Options
field_id | value
---------|-------
1 | Audi
1 | BMW
In 'FieldOption' model class I have defined the following relationship:
class FieldOption extends Model
{
public function field() {
return $this->belongsTo('App\Field');
}
}
Table 4: Category_Field
category_id | field_id
------------|-------
1 | 1
Now I need to fetch all the fields and field_options for category_id=1. How can I achieve this using Laravel?
Thanks!
First define relationship between Field and FieldOptions
public function options() {
return $this->hasMany('App\FieldOption');
}
Then you can eager load all relationships like this
$category = Category::with('fields.options')->find(1);
//Get category 1, with all fields and their respectif fieldOptions

Laravel - model modifications

I need to refactor project and I have problem. Below is old, working model, where 'active' column is in "people" table. I need to move 'active' column into "people_translations" table.
Do you have any Idea to modify scopeActive method?
Thanks a lot!
Old working model:
class BaseModel extends Eloquent
{
public function scopeActive($query)
{
return $query->where($this->table . '.active', '=', 1);
}
}
class People extends BaseModel
{
protected $table = 'peoples';
protected $translationModel = 'PeopleTranslation';
}
class PeopleTranslation extends Eloquent
{
public $timestamps = false;
protected $table = 'peoples_translations';
}
Old tables structure:
Table: peoples
id | type | date | active
-------------------------
7 | .... | ... | 1
Table: peoples_translations
id | people_id | language_id | name
-----------------------------------
1 | 7 | 1 | Ann
Old query:
$peoples = \People::active()->get();
New tables structure:
Table: peoples
id | type | date
----------------
7 | .... | ...
Table: peoples_translations
id | people_id | language_id | name | active
--------------------------------------------
1 | 7 | 1 | Ann | 1
Create a relation for translations inside People Model
public function translations()
{
return $this->hasMany('PeopleTranslation', 'people_id');
}
Create active scope in People model
public function scopeActive($query)
{
return $query->whereHas('translations', function($query) {
$query->where('active', 1);
});
}
It will make subquery for this table and as a result it will get where (count of translations with active = 1) > 0.
If you have one-to-one relation - look for hasOne relation method instead of hasMany.

Categories