I have 3 tables:
Category
id | name
-----------------------------------
1 | Men >80kg
-----------------------------------
2 | Women >80kg
-----------------------------------
3 | Team Men >80kg
-----------------------------------
Category_Tournament (List all categories available in a tournament)
id | category_id | tournament_id
---------------------------
1 | 1 | 2
---------------------------
2 | 2 | 2
---------------------------
3 | 3 | 2
---------------------------
4 | 4 | 2
---------------------------
Category_Tournament_User (categories registered by user)
id | category_tournament_id | user_id
-----------------------------------------------
1 | 1 | 201
-----------------------------------------------
2 | 3 | 202
-----------------------------------------------
I'm trying to sync Category_Tournament_User when User select category to participate
$categories = $request->get('categories_to_register');
$tournament->categories_user()->sync($categories);
Where my relation ManyToMany is
class Tournament extends Model
{
...
public function categories_user()
{
return $this->belongsToMany('App\Category', 'category_tournament_user', 'user_id','category_tournament_id');
}
}
It is syncing, but not well, instead of inserting user_id, it insert tournament_id, I guess it is because I call it from Tournament Model.
I tried changing the order of relations
user->tournament->category or
tournament->user->category
but when user has no registered categories,
user->tournament or
tournament->user
will be null, so I can't invoke ->category
How can I fix this mess???
Related
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);
});
}
How do I store data in users.sport_id:
User class:
public function sport() {
return $this->belongsToMany('App\Sport');
}
Where sports table has:
+----+------------+
| id | sport |
+----+------------+
| 1 | baseball |
+----+------------+
| 2 | basketball |
+----+------------+
Do I add to user.sport_id = 1,2 or save it as array [1,2] and use $casts = ['sport_id'=>'array'];
I am trying to retrieve database rows with their relationships. However, the local key is an array. Let me explain using an example.
Lets say I have a table of countries and a table of pages. Each country can have many pages. Each page can belong to multiple countries. I do not have the flexibility to change this schema.
pages
+-------------+-----------+
| id | name | countries |
+-------------+-----------+
| 1 | Page 1 | 1 |
+-------------+-----------+
| 2 | Page 2 | 1,2,3 |
+-------------+-----------+
| 3 | Page 3 | 4,5,6 |
+-------------+-----------+
countries
+----+----------------+
| id | name |
+----+----------------+
| 1 | United States |
+----+----------------+
| 2 | United Kingdom |
+----+----------------+
| 3 | Germany |
+----+----------------+
| 4 | France |
+----+----------------+
| 5 | Hong Kong |
+----+----------------+
| 6 | Thailand |
+----+----------------+
| 7 | Belgium |
+----+----------------+
| 8 | Singapore |
+----+----------------+
My model and controller look something like:
country
public function pages()
{
return $this->hasMany(Page::class, 'id', 'countries');
}
MemberController.php
$countries = Country::with('pages')->get();
This is returning all countries, but only Page 1 contains any relationships.
Is there a way to retrieve relationships using a whereIn approach so all three countries will return appropriate pages?
Thanks in advance
Since Page can belong to many Countries, you need to create a pivot table called country_page and remove the countries column.
Then define two belongsToMany() relationships in both models:
public function pages()
{
return $this->belongsToMany(Page::class);
}
If you're not following Laravel naming conventions listed in my repo and you gave the pivot name a custom name, define it too:
public function pages()
{
return $this->belongsToMany(Page::class, 'custom_pivot_table');
}
Something like this ?
$datas = Pages::where( ##your conditions### )->get()->inArray();
$countries = Countries::pluck('name','id'); // return array of id=>name
foreach($datas as $key=>$data) {
$c = [];
foreach(explode(',',$data['countries']) as $country_id) {
$c[]=$countries[$country_id];
//or
$c[]= ['id'=>$country_id,'name'=>$countries[$country_id]];
}
$datas[$key]['countries']= $c;
}
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');
}
I have a question table which has a type_id column. The value defined here relates to the QuestionTypes model.
Question Table:
--------------------------
| id | title | type_id |
--------------------------
| 1 | apple? | 2 |
| 3 | banana? | 2 |
| 4 | kiwi? | 2 |
| 5 | pear? | 3 |
--------------------------
QuestionTypes
----------------
| id | title |
----------------
| 1 | multi |
| 2 | single |
| 3 | free |
----------------
In the Questions Model I have:
public function type()
{
return $this->hasOne(QuestionType::class);
}
I would like to print the title from questiontypes table but when I try to output in the view using $question->type->title I get:
Column not found: 1054 Unknown column 'x__questiontypes.questions_id' in 'where clause'
(SQL: select * from `x__questiontypes` where `x__questiontypes`.`questions_id` = 2 and `x__questiontypes`.`questions_id` is not null limit 1
Have I mixed up the relationships?
Solution via Attaching a hasOne model to another Laravel/Eloquent model without specifying id
Updated to Questions model:
public function type()
{
return $this->belongsTo(QuestionType::class, 'type_id');
}
Added to QuestionType model:
public function questions()
{
return $this->hasMany(Question::class, 'type_id');
}