I have a table for comments, each comment could have a decision from a dropdown.
If the specific decision is selected the user then could select one/many services.
If a specific service is selected only one grade is selected.
So the result comments section could look like these examples:
This is a comment without a decision
This is a comment with a decision
decision: This is decision 1
This is a comment with a decision and services
decision: This is decision 2
services: ['This is service 1', 'This is service 2']
This is a comment with a decision, services, and grade
decision: This is decision 2
services: ['this is service 1', 'This is service 3 grade: 2']
The decision is only a single selection. The services are multiple checkboxes. The grade is only a single selection.
The form looks like that:
<form>
<textarea name="comment"></textarea>
<select name="decision">
<option value="1">Decision 1</option>
<option value="2">Decision 2</option>
</select>
<!-- Show if decision 2 is selected -->
<div class="services">
<input type="checkbox" name="services[]" value="1"/>
<input type="checkbox" name="services[]" value="2"/>
</div>
<!-- Show if service 2 is checked -->
<select name="grade">
<option value="1">1</option>
<option value="2">2</option>
</select>
</form>
DB structure:
comments table:
id | comment
1 | This is comment 1
2 | This is comment 2
decisions table:
id | decision
1 | This is decision 1
2 | This is decision 2
services table:
id | service
1 | This is service 1
2 | This is service 2
grades table:
id | grade
1 | 1
2 | 2
3 | 3
4 | 4
Finally, these tables are related using 3 tables:
comment_decisions table:
id | decision_id | comment_id
comment_services table:
id | service_id | comment_id
comment_grades table:
id | comment_services_id | grade_id
I did that to avoid duplication and NULL values in the case of one table:
id | comment | decision_id (nullable) | service_id (nullable) | grade_id (nullable)
This issue is when I try to update the decision, services, and grade. When I use updateOrCreate new records are created not updated. attach and sync not working either.
Is there a better structure or I'm missing the right functions to update the tables?
Related
I am using laravel eloquent to get the query results. I have two tables below:
users table:
| id | department_id
| 1 | 1
| 2 | 3
| 3 | 2
department table:
| id | name
| 1 | A
| 2 | B
| 3 | C
| 4 | D
| 5 | E
How to get one unassigned ID, not existing department ID, into the users table? Example, 4 & 5 are not yet existing in users table, so how can I get 4 or 5 using an eloquent?
I am thinking of this but this is not correct.
Department::select('department.id as id')
->leftJoin('users', 'users.department_id' ,'department.id')
->pluck('id');
Does anybody know?
Try this
//here you first got all the department which is assigned to user
$assigned_dept = Users::pluck('department_id')->toArray();
$department = array_values($assigned_dept); //output:['1','3','2']
//here you can select department which is not assigned to user with limit
$user = Department::whereNotIn('id',$department)
->limit(1)->get();
hope it works for you..
You can do it like this:
Department::whereNotIn('id', User::pluck('department_id'))->get();
I believe below code will work for you :
Department::select('department.id as id')
->whereNotIn('id', User::whereNotNull('department_id')->pluck('department_id'))
->pluck('id');
From the answers others, there is a problem with the array if department_id is NULL. So, I added whereNotNull and also last() and then the problem is solved. Let me post the answer here:
Department::select('department.id as id')
->whereNotIn('id', User::whereNotNull('department_id')->pluck('department_id'))
->pluck('id')
->last(); // since I only need one row
Background - I'm creating a system where administrators can create arbitrary fields, which are then combined into a form. Users then complete this form, and the values input against each field are stored in a table. However, rather than overwrite the previous value, I plan on keeping each past value as individual rows in the table. I then want to be able to display the contents submitted in each form, but only the most recently submitted value.
Problem
I have a model, Service, that features a belongsToMany relationship with another model, Field. This relationship is defined as:
public function fields()
{
return $this->belongsToMany('App\Field')->withPivot('id', 'value', 'date')->withTimestamps();
}
The intermediary table has 3 values I wish to retrieve, id, value and date.
A Service may have 1 or more Fields, and for each field it may also have more than 1 pivot row. That is, a single Service/Field pairing may have multiple entries in the pivot table with different pivot values. For example:
Table field_service
id | service_id | field_id | value | created_at
------------------------------------------------------
1 | 1 | 1 | lorem | 2018-02-01
2 | 1 | 1 | ipsum | 2018-01-01
3 | 1 | 1 | dolor | 2017-12-01
4 | 1 | 2 | est | 2018-03-10
5 | 1 | 2 | sicum | 2018-03-09
6 | 1 | 2 | hoci | 2018-03-08
What I want is to get either:
A specific row from the pivot table for each Field associated with the Service, or
A specific value from the pivot table for each Field associated with the Service.
For example - in the table above, I would like the Service with ID 1 to have 2 Fields in the relationship, with each Field containing an attribute for the corresponding pivot value. The Fields attached would be specified by the corresponding pivot table entry having the most recent date. Something akin to:
$service->fields()[0]->value = "lorem"
$service->fields()[1]->value = "est"
I feel there's an obvious, 'Laravel'ly solution out there, but it eludes me...
Update
Somewhat unbelievably this is another case of me not understanding windowing functions. I asked a question 7 years ago that is basically this exactly problem, but with raw MySQL. The following raw MySQL basically gives me what I want, I just don't know how to Laravelise it:
SELECT services.name, fields.name, field_service.value, field_service.created_at, field_service.field_id
FROM field_service
INNER JOIN
(SELECT field_id, max(created_at) as ts
FROM field_service
WHERE service_id = X
GROUP BY field_id) maxt
ON (field_service.field_id = maxt.field_id and field_service.created_at = maxt.ts)
JOIN fields ON fields.id = field_service.field_id
JOIN services ON services.id = field_service.service_id
Try this:
public function fields()
{
$join = DB::table('field_service')
->select('field_id')->selectRaw('max(`created_at`) as `ts`')
->where('service_id', DB::raw($this->id))->groupBy('field_id');
$sql = '(' . $join->toSql() . ') `maxt`';
return $this->belongsToMany(Field::class)->withPivot('id', 'value', 'created_at')
->join(DB::raw($sql), function($join) {
$join->on('field_service.field_id', '=', 'maxt.field_id')
->on('field_service.created_at', '=', 'maxt.ts');
});
}
Then use it like this:
$service->fields[0]->pivot->value // "lorem"
$service->fields[1]->pivot->value // "est"
I'm relatively new to both php and mysql and here's my first question:
I have 3 tables. One as the "main table" and two as basically lists/categories of which I can choose and relate them to my main table. So:
Table 1
id name hobby color
1 Peter 1 2
2 Simon 3 2
3 Lisa 2 3
Table 2 would then be hobbies
id name
1 Swimming
2 Football
3 Piano
Same with table 3 and colors.
id name
1 red
2 blue
3 green
Now, I have managed to "relate" the tables with phpmyadmin, but how can I do the following:
I wanna insert data into the first table. But the thing is, I wanna insert a simple number in hobbies or colors. But within the html form, there should be the text related to the is.
So if i choose 'green'(as a string), it should insert 3 into Table 1.
I hope it's clear what i want :D
Thanks for every help in advance.
Simon
Create your html like this:
<select name="color">
<option value="1">red</option>
<option value="2">blue</option>
</select>
When an user select "blue" the value of input.color is "2". So an end user selects an string/color and you can insert the int/id
I want to create a table structure of the following requirement :
I have some main categories(e.g Electronics, Dress etc.). I want to add subcategories under these categories upto three level.
Suppose main category is Electronics. It's child category is computer. Computer's child category is Laptop and Laptop's child category is Dell.
( Electronics => computer => Laptop => Dell )
Like the above example there are many categories and sub categories.
So, what is the appropriate table structure of it?
I will suggest to create one table with column parent_id.
For example:
TABLE category
category_id,
category_name
category_parent_id
parent_id will contain the id of parent_id.
For ex.
category_id | category_name | category_parent_id
1 | Electronics | 0
2 | Dress | 0
3 | Computer | 1
4 | T-shirt | 2
5 | Llaptop | 3
6 | Dell | 5
I am trying to achieve the following result (the first line is header)
Level 1 | Level 2 | Level 3 | Level 4 | Person
Technicals | Development | Software | Team leader | Eric
Technicals | Development | Software | Team leader | Steven
Technicals | Development | Software | Team leader | Jana
How can I do so? I tried to use the following code. The first part is to create the hierarchy which works fine. The second part is to have the date in the above mentioned table is a pretty painful.
SELECT * FROM ( /* level2 */
SELECT * FROM ( /* level1 */
SELECT * FROM arc.localnode /*create hierarchy */
WHERE tree_id = 2408362
CONNECT BY PRIOR node_id = parent_id
START WITH parent_id IS NULL ) l1node
LEFT JOIN names on l1node.prent_id = names.name_id ) l2node
At this point, I am quite lost. A bit of guidance and suggestion would be a lot of help :-)
There are two tables. The first table has data like this:
NODE_ID | PREV_ID | NEXT_ID | PARENT_ID
1421864 3482917 1421768
3482981 3482917 1421866 1421768
3482911 3060402 3482913 1421768
3482917 1421864 3482981 1421768
This is a complicated because it is in hieraracy. So obviously a PARENT_ID can be the NODE_ID of some other PARENT_ID. Similarly the parent_ID can be the PREV_ID and NEXT_ID.
The names are in seperate table with name_id. The name ID in this table is similar to NODE_ID of the main table in hieraracy.
You can use the Stragg Package mentioned in AskTom in the below link
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2196162600402
Your can also refer the below link in oracle forum
https://forums.oracle.com/forums/thread.jspa?threadID=2258996
Kindly post create and insert statements for your requirement so that we can test it and confirm