how to show all child data on group by laravel eloquent.?
i have try to using
Model::groupBy('something')->get()
but it just return one row from the same group...
Model::groupBy('something')->get()
Related
I have two tables, Parent and child in my MySQL database. I want to make a dropdown list in my child form in Laravel so I can select a parent that already exist in my database table and I want it to be added in the child table as well. Help me please
As you have asked a question without any code, I believe you are only looking for a right nudge so here you go.
First of all, you will need to make one foreign key inside your child table for your parent table. That way parent and child tables will be connected.
Then from Controller, pass all parent ids/name pair when loading blade of child form and then loop through that data and make a dropdown with value as parent id and label as parent name, that way you can catch the parent id easily of the selected dropdown.
Next just save the data with the foreign key as the selected parent id inside your child table.
I have a table named advertisments which has views column.
I want to fetch the data from advertisments table and increment each row's views in a single query is it possible ?.
The following code is what I wrote to achieve the desired result but the views aren't updating.
Advertisement::with('sponsor')
->whereDeleted(false)
->whereHeader(false)
->orderBy('views')
->raw("update views ".DB::raw('views + 1'))
->get();
I have two tables one is "ITEM" and second one is "Composite Item" in mysql database. I am trying to insert multiple items id in composite item table for one single record in laravel.
One of logic I have tried to save multiple items id as comma separated value for example "1,2,3,4" and also update that one field as same concept. but I am having issue when i delete any one item from item table. if i delete any of the item from item table how can i delete that same item from string item ids in composite item table. for example from item table i have deleted item id 3.
also i have think if i create new table for one to many relation then how can i update record when i update record.
You are definitely looking for many-to-many relationships, not one-to-many. Laravel has excellent features for generating and maintaining a many-to-many relationship.
The attach/detach methods are used for single or multiple insertion or deletion in a relation. For example, if you want to add an array of items([1,2]) to a composite item, you can use attach like:
$compositeItem->items()->attach([1,2]);
Same goes for detaching:
$compositeItem->items()->detach([1,2]);
The sync method is used for updating existing records.
Sync is a bit tricky and work exactly like:
The sync method accepts an array of IDs to place on the intermediate
table. Any IDs that are not in the given array will be removed from
the intermediate table.
Like if you want to remove 2 and insert 3 you can use sync like this:
$compositeItem->items()->sync([1,3]);
I have pic related parent/child tables. For every one row in the parent table, there can be like 100 or 200 child rows in the child table. When I upgrade the child table; I also give it's parent row's tID number to child's row under thread ID column. So, there isn't more than one parent row to each child row.
If I had a column in the parent table like "LastChildTimestamp" could I upgrade related row with the last child timestamp? If so can you show me an example?
What I want to achieve is to keep the most recently updated parents upper when I list out the parents and I want to make it easy. So if you have a better solution or method I'm open to it too.
There are two different approaches you could use to solve this problem.
Create the parent.LastChildTimestamp field, then use a database trigger to update it.
See: http://www.techonthenet.com/mysql/triggers/after_update.php
In particular, you would set up after insert and after update triggers in the child table that would update the associated parent row.
Alternately, you could instead simply aggregate the data from the child rows when you need to read it. Your query would looks something like:
SELECT MAX(TIMESTAMP)
FROM child
WHERE child.threadID = (thread ID)
I have a doubt doing pagination in CakePHP.
Well, I will try to be concise.
I have three tables, one table is which I paginate (1st table), another table (2nd table) dependent of this one (1st table have FK to 2nd table), and the most important, 3rd table, that has FK to 1st table. So: 3rd table-> 1st table-> 2nd table.
I do pagination of 1st table (model) from his controller, so far everything ok, the pagination list all the records from table bd and no problems.
I need also to get records from 1st table but ordering by a record from 2nd table. No problem. The problem IS: I WANT not to get in pagination, records from 1st table (which i paginate it) that arent present in 3rd table.
The problem is that CAKEPHP first make a query getting records from 1st table + 2nd table. After this, it get the records from 3rd table according to the records (PK) obtained from the first query, so I cant say it: not take records from 1st table that arent present in 3rd table.
¿How can I do this?
I hope I explained.
Thank you very much.
Regards.
what you'll want to use is a Counter Cache. Go to http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html and search for 'counterCache,' and you'll find instructions.
Basically, you want to implement a "counter cache" field in model 1, that automatically keeps count of the number of linked records in model 3. Then, to exclude records from Model 1 that have no related records in Model 3, you just add a pagination condition to say that the counter cache field must be greater than 0.
UPDATE:
If you can't use CakePHP's counter cache, then you should emulate the behaviour of the counter cache using a virtual field - see http://book.cakephp.org/2.0/en/models/virtual-fields.html