laravel 5 Relationship has one - laravel

User table
id | name | user_type | email | password
1 | John | 1 | john#gmail.com | something
2 | Roy | 1 | roy#gmail.com | something
3 | Rax | 1 | Rax#gmail.com | something
4 | Ren | 1 | ren#gmail.com | something
AssignUser Table
id | user_id | assign_to_math | assign_to_chemistry
1 | 3 | 2 | 1
2 | 4 | 1 | 2
So, here is users table where four users are avail
Now in assign table first row Rax is assign tuitoin for math to Roy and chemistry to John
I want to get the detail of john and roy with relation
I tried in User models
public function getInfoFormathTutor(){
return $this->hasOne('App\AssignUser', 'id', 'assign_to_math');
}
It return for me null value don't know why please help
Thanks in advance

try:
public function getInfoFormathTutor(){
return $this->hasOne('App\AssignUser', 'assign_to_math', 'id');
}

Related

Use a non-unique route key name?

We have rows which are sequenced and scoped to each organisation. So:
Processes tbl:
| id | org_id | sequence | name |
|---- |-------- |---------- |------------------- |
| 23 | 1 | 1 | New person |
| 56 | 1 | 2 | Person leaves |
| 84 | 1 | 3 | Person absent |
| 26 | 2 | 1 | Something happens |
| 34 | 2 | 2 | Something else |
| 77 | 2 | 3 | And another |
We do this so they can have a simple numbering system for their own records rather than a UUID, hash or something else. We prefer to have this in the url as well, so:
example.com/processes/1
Once logged in, we know their org_id, so for each query we can use org_id along with the sequence from the url.
Is there a clean laravel-esque kind of way to achieve this, maybe using the Model's getRouteKeyName?
I got the answer with some help from the laracasts forum:
/**
* Retrieve the model for a bound value.
*
* #param mixed $value
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this->where('sequence', $value)
->where('org_id', /* however you get your org_id */)
->first() ?? abort(404);
}

Use data gotten from one table to get data from another data

I have three tables
Job Model
+---------------------------+
| id | name |
+---------------------------+
| 1 | web design |
+---------------------------+
| 2 | desktop development |
+---------------------------+
Applicant Model
+----------------------------------------------------------+
| id | job_id | user_id | desc |
+----------------------------------------------------------+
| 1 | 2 | 1 | I am an expert developer |
+----------------------------------------------------------+
| 2 | 2 | 2 | I am good |
+----------------------------------------------------------+
User Model
+----------------+
| id | name |
+----------------+
| 1 | john |
+----------------+
| 2 | steve |
+----------------+
Using eloquent laravel,
How do I get applicants with job_id "2" and their user name together.
Tried a lot but unfortunately to no avail.
You would use a query something like this:
$jobs = Job::where('id', 2)
->join('applicants', 'jobs.id', '=', 'applicants.job_id')
->join('users', 'applicants.user_id', '=', 'users.id')
->select(['user.name', 'job.name', 'applicant.desc'])
->get();
You will have to change it to your requirements but should about what you need.
You can get more info here:
https://laravel.com/docs/5.7/queries
Hope that helps

Laravel: Proper Querying Many to Many Relationships

I have 2 tables and a third pivot table as follows:
Table 1: Files
id
title
Table 2: Pools
id
title
is_visible (1,0)
Table 3: file_pools
file_id
pool_id
All Models and Relationships are in place.
A File may belong to none, 1 or more pools
A Pool may have none,1 or more files
Now, i would like to create a scope visible in the Model File such that:
the query includes any File that belongs to none or 1,or more active Pools
should not include any File that has at least 1 invisible (is_visible=0) Pool, even if this File belongs to another visible Pool
Below is what I have tried but this includes files that are both in visible and invisible Pools
public function scopeVisible($query)
{
return $query->doesntHave('pools')->orWhereHas('pools', function ($query) {
$query->where('is_visible',1);
});
}
any help highly welcome
EDIT: Add Sample Data
table: files
| id | title |
|----------|---------------|
| 1 | File A |
| 2 | File B |
| 3 | File C |
| 4 | File D |
table: pools
| id | title | is_visible|
|----------|---------------|---------- |
| 1 | Pool 1 | 1 |
| 2 | Pool 2 | 0 |
table: file_pools
| file_id | pool_id |
|----------|---------------|
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
The Expected Result : (scope:visible)
| id | title |
|----------|---------------|
| 1 | File A |
| 4 | File D |
Hope this clarifies
If I understand your situation correctly:
public function scopeVisible($query)
{
return $query->whereDoesntHave('pools', function ($query) {
$query->where('is_visible', 0);
});
}

Laravel many-to-many relationship with varying options

I am new to Laravel and am attempting to set up a many to many relationship (at least I think that's what I need)
Essentially each user have preferences there are 7 types of preferences some preferences can have multiple values and some have only one value
Here are the user_pref_options
project_type | ( will be checkboxes on view and have multiple options)
favorite | (a user can have multiple this is a foreign key to another table)
tagline | ( will be checkboxes on view and have multiple options)
bedroom_min | (a single value per user)
bedroom_max | (a single value per user)
sf_min | (a single value per user)
sf_max | (a single value per user)
and here is what the user_pref table has
| id | user_id | pref_option_id | value |
| --- | --- | --- | --- |
| 1 | 1 | 1 | 5 |
| 2 | 1 | 1 | 3 |
| 3 | 1 | 2 | 364 |
| 4 | 1 | 2 | 201 |
| 5 | 1 | 2 | 9 |
| 6 | 1 | 2 | 244 |
| 7 | 1 | 2 | 192 |
| 8 | 1 | 2 | 292 |
| 9 | 1 | 4 | 1 |
| 10 | 1 | 11 | 1 |
| 11 | 1 | 12 | 6 |
| 12 | 1 | 13 | 1628 |
| 13 | 1 | 14 | 9134 |
so that would be 1 users notice how some option types have multiples
sorry I had to lay a lot of background before I ask my question.
I would like to be able to connect the user prefs to the user using Eloquent and am having trouble figuring out which way it should be done..
here is what I have tried
//User model
public function prefs(){
return $this->belongsToMany('App\Pref', 'pref_user', 'user_id', 'pref_id');
}
and
//Pref Model
public function user()
{
return $this->belongsToMany('App\Pref', 'pref_user', 'user_id', 'pref_id');
}
I have taken a look here
I would like to be able to get all the preferences and display them on a view
(I won't bore you by pasting that in here now)
my struggle is that some of the preferences have multiple options and some have single options. How do I retrieve the values
maybe I need to separate each preference type to its own model but that seems like overkill I am open to suggestions
user_pref table makes it difficult to distinguish between your prefs and also sets a lot of overhead data.
So, you can set the single value prefs (bedroom_min, bedroom_max, sf_min and sf_max) in user model or in separated model and retrieve them with user. And for other prefs, many-to-many relation is better and more flexible.
Finally, When you fetch User model, you will get the model and its relations.

Display record count in listbox using multiple tables and fields

i need help with a query, can't get it to work correctly. What i'm trying to achieve is to have a select box displaying the number of records associated with a particular theme, for some theme it works well for some it displays (0) when infact there are 2 records, I'm wondering if someone could help me on this, your help would be greatly appreciated, please see below my actual query + table structure :
SELECT theme.id_theme, theme.theme, calender.start_date,
calender.id_theme1,calender.id_theme2, calender.id_theme3, COUNT(*) AS total
FROM theme, calender
WHERE (YEAR(calender.start_date) = YEAR(CURDATE())
AND MONTH(calender.start_date) > MONTH(CURDATE()) )
AND (theme.id_theme=calender.id_theme1)
OR (theme.id_theme=calender.id_theme2)
OR (theme.id_theme=calender.id_theme3)
GROUP BY theme.id_theme
ORDER BY theme.theme ASC
THEME table
|---------------------|
| id_theme | theme |
|----------|----------|
| 1 | Yoga |
| 2 | Music |
| 3 | Taichi |
| 4 | Dance |
| 5 | Coaching |
|---------------------|
CALENDAR table
|---------------------------------------------------------------------------|
| id_calender | id_theme1 | id_theme2 | id_theme3 | start_date | end_date |
|-------------|-----------|-----------|-----------|------------|------------|
| 1 | 2 | 4 | | 2015-07-24 | 2015-08-02 |
| 2 | 4 | 1 | 5 | 2015-08-06 | 2015-08-22 |
| 3 | 1 | 3 | 2 | 2014-10-11 | 2015-10-28 |
|---------------------------------------------------------------------------|
LISTBOX
|----------------|
| |
| Yoga (1) |
| Music (1) |
| Taichi (0) |
| Dance (2) |
| Coaching (1) |
|----------------|
Thanking you in advance
I think that themes conditions should be into brackets
((theme.id_theme=calender.id_theme1)
OR (theme.id_theme=calender.id_theme2)
OR (theme.id_theme=calender.id_theme3))
Hope this help

Resources