Laravel get last row for each items - php

I use Laravel Eloquent and I have this table:
id client_id project_id status_id
1 1 1 1
2 2 1 1
3 1 1 2
4 2 1 2
I want take the last row which insert for each client. How can i do?

Since Laravel offers method 'first' to take the very first matching record, how about first you order by descending and take the first row?
$affectedRow = TableName::where(condition)->orderBy('id', 'desc')->first();

Related

Re assign foreign ID without conflict

I have a main table with a primary key (ÍD) and a linked one.
Existing Data
Main
ID | name
=========
1 | foo
2 | bar
3 | loo
4 | zoo
Linked
main_id
=======
1
1
2
2
There are connections to the first entries of main (1,2) in the linked table.
Now new data gets imported from the same structure:
Import Data
ID | name
=========
1 | new_foo
2 | new_bar
3 | new_loo
4 | new_zoo
and
main_id
=======
3
4
3
1
During the import process the IDs of the main table will get new ids (done by a script)
ID | name
=========
1 | foo
2 | bar
3 | loo
4 | zoo
5 | new_foo
6 | new_bar
7 | new_loo
8 | new_zoo
but the main_id will still have the ids from the imported data:
main_id
=======
1
1
2
2
3 => should be 7
4 => should be 8
3 => should be 7
1 => should be 5 => * comment below
*I cannot simple update linked like:
UPDATE linked SET main_id = 5 WHERE main_id = 1
as it would update the first two rows as well.
So how can I map these field with the new primary ID of main?
I could add a high number to the main_id before import like
UPDATE linked SET main_id = main_id + 10000000
do the import
apply the real ID
UPDATE linked SET main_id = %realID WHERE main_id = %importedID
return my temporary ID back to the original.
UPDATE linked SET main_id = main_id - 10000000 WHERE main_id > 10000000
The problem is pretty obvious: This doesn't work well (or at all) if the IDs are higher than 10.000.000 or the temporary id is higher than BIGINT (9223372036854775807).
It could work with a clone of the table but this may cause a problem in memory consumption as the linked table can get pretty big.
I'm sure there's a "best practice" way of doing this.
The steps are:
Pick last ID in Main table: select #lastId := max(ID) from Main;
Just shift all IDs, that are to be written in Linked table, by #lastId.
Something like this: insert into Linked select ID + #lastId from Imported_linked_data
No extra steps needed.
You can add the new entries in the Main table and let the AUTO_INCREMENT column generate the Id of the new row (as it should). For each row you cache the generated Id in an associative array, which maps the old Id to the new generated Id.
After that you insert the entries for the linked table. Before adding the new row you replace the old foreign key Id to the new generated id based on your created associative array.

how to get the records from the user table on the basis of the decreasing number of matching records using active record codeigniter

i am having two tables one is the user answers in which users answers some questions about himself
user_id question_id answer_id
1 2 3
1 4 2
1 2 1
1 2 3
1 4 1
2 2 3
2 4 2
2 2 1
2 2 3
2 4 1
the other one is the prefernce table in which the user answers about his partner
user_id question_id answer_id
1 2 3
1 4 2
1 2 1
what i need to do is first i have to take question_id and answer_id for a particular user from the second table and match the question_id and answer_id
to the first table and we need to fetch the user detail from the third table on the basis of decreasing number of answer matched for that user to the other.

after multiple delete maintain order column

in my project i have to maintain a order of every record and if i delete in between i have to maintain order in a form that order don't have any gape like after 3 next is 4 only... not 5 or 6......my display like.
if i delete multiple record that have Rule ID are 1,3 and 4...then my rule ID 0 remain 0, 2 becomes 1, and 5 becomes 3...like that i want. i am deleting record base on id.
id rileid
1 0
3 1
5 2
6 3
11 4
14 5
how to do that? please help me.

How to synchronize pivot table in plain PHP

Assume we have these tables
Users table
-----------
id name
-----------
1 xxx
2 yyy
3 ccc
4 bbb
5 aaa
Location table
-------------
id name
-------------
6 Spain
7 Russia
8 Germany
9 USA
Pivot table
------------------------
id user_id location_id
------------------------
1 1 6
2 2 8
3 1 8
4 1 9
5 3 8
What I want to achieve is to sync the data in the pivot table.
So exactly I have post request with array of user ids = [1,5,4] and location_id = 8. So I would get the following result
[updated] Pivot table
-------------------------
id user_id location_id
-------------------------
1 1 6 <-- This one stays
3 1 8
4 1 9
6 5 8 <-- Added
7 4 8 <-- Added
...we deleted the row with location_id=8 and user_id=2 and user=3 because those are not in the users array
How can I do add the new ones, delete the ones that are not in the request, and leave the one that already exists with some functionality, just like Laravel has done it in sync function.
I know that the easyest way is to get all the users that has that specific location_id, delete all, and than insert once again. Is there some workaround or should I do the newbie way :D
Thank you
Not sure if this is the best way to go (actually deleting everything and then inserting the values seems easier and cleaner), but here's one way to do it, if for some reason you want to keep the pivot table ID:
Define a unique index for the table, considering both pivot fields (user_id,location_id).
Insert all data using ON DUPLICATE KEY UPDATE user_id = user_id and location_id = location_id, so that if there's duplicate values, mysql will keep them without modifications and will not throw error.
Implode the Array IDs from the Request and execute a delete from the table, where data is not in the specified values.

How to deal with a large mySQL tree

I am working on a project which user registration multiples like this.
1st Month -> 1 user
2nd Month -> 4 users comes under the above user
3rd Month -> 16 users (i.e, 4 users comes under each 4 users above )
4th Month -> 64 users (i.e, 4 users comes under each 16 users above )
eg:
1
2 | 2
8 | 8
32| 32
and continues...
Please give me an advice, how to store this in database.
Thanks in advance.
Assuming user_id is your primary key, create a parent column that contains the user_id of the parent user. For example:
user_id parent
1 NULL
2 1
3 1
4 1
5 1
6 2
...
You will also want to create an index on the parent column so that you can quickly do a reverse lookup (i.e. find all children of a given user).

Categories