Laravel, Get row with highest version column from database - laravel

At the moment i have a 1 to many relationship between 2 models. A Machine model and a Document Model.
A document can have multiple versions and i always related to my machine. Now i only want to show the documents with the latest or highest version.
I've tried to get the version from the document but then i can only return 1 document to my view instead of returning multiple documents. I've also tried to get the documents the following way but i get stuck with the steps i need to take after creating the loop:
$machine = Machine::find($id);
$documents = $machine->documents;
foreach($documents as $document)
{
}
dd($documents);
return view('machine.detail', compact('machine'));
So to specify my question: How can i return only the unique documents with the latest version to my view. So if file 1 has versions 1,2,3,10 and file 2 has versions 1,2,3 i want to return version 10 of file 1 and version 3 of file 2.

If I understand your question correctly you can do the following.
// Add this relation also on your model where your hasMany is on properly Machine.php
public function latestDocument()
{
return $this->hasOne(Document::class)
->latest('version_column');
}
// Controller
$machine = Machine::with(['latestDocument'])->findOrFail($id);
$document = $machine->latestDocument;
return view('machine.detail', compact('machine'));

If you have a unique key for each document that is shared for all the versions something like this should work.
$documents = $machine->documents->groupBy('name of your unique key')->map(function($documents) {
$latestVersion = $documents->max('version');
return $documents->firstWhere('version', $latestVersion);
});
Explanation: The get the latest version from each document we first need to group all documents by a key to separate all the unqiue documents into multiple collections that contain the versions.
Then we need to map them into a new collection where we only get the document with the latest version.
Note: Dont forget to change $machine = Machine::find($id); to $machine = Machine::with('documents')->find($id); so you load the documents before hand. This will prevent the n+1 query problem

Related

Laravel 5.5 - Check if only one column will be updated

Using Laravel 5.5 and models, I would like to know, before updated if only one column, statut, will be updated.
The interest is to just updated the statut if there are no changes or, if there are some columns to be updated, create a new row. Because each modification need to be approved before published.
Actually, I have a big if like :
if($request->statut != $product->statut && $request->title == $product ...)
Is there a quickest way ?
Thanks.
You can utilize isDirty('field') method to check if the field was changed (and not yet saved). getDirty() will return you array of all such fields and their values.
If I understood your case correctly, then you can do:
if ($product->isDirty('statut')) {
if (count($product->getDirty()) == 1) {
...
}
}
First we check if field statut was changed for your $product. Then we just check if number of changed fields is 1 (meaning that there are no more changed fields than just statut).

select certain columns from eloquent collection after the query has executed

Using Laravel 5.3, I have a model with the following function
public function myData() {
return $this->hasMany(MyData::class);
}
and in my collection I have the following
$my_data = MyModel->myData()->get();
All good so far. If I return $my_data I get an eloquent collection with three items.
What I need now though is to create a duplicate of that collection but containing only three of the fields.
I have tried several different things, each of which return an error. The following is the closest I have got, but this returns an empty array - I assume because the fields are located one level deeper than the collection object.
$new_collection = $my_data->only(['field_1', 'field_2', 'field_3']);
What would be the correct way to create a new collection containing all three items, each with only the three selected fields?
Thanks for your help
You could use map:
$slimmed_down = $collection->map(function ($item, $key) {
return [
'field_1' => $item->field_1,
'field_2' => $item->field_2,
'field_3' => $item->field_3
];
});
This will return a new Collection with just the values you want. As far as I know there isn't any other method that does what you want, so iterating over every item and selecting the fields this way is one of the few solutions.
The advantage of using map instead of a standard foreach loop is that when you use map it returns a new instance of Collection.
Edit:
After some thoughts and research about this, the problem you'll have created is that the all the values in the Collection aren't instances of anything anymore. If you don't mind this effect, an even prettier and faster way would be to do this:
$slimmed_down = $collection->toArray()->only(['field_1', 'field_2', 'field_3']);
This basically has the same result.
Using Laravel 9, I just had the same issue :
$my_data->only(['field_1', 'field_2', 'field_3']);
returning an empty array.
I solved it with :
$my_data->map->only(['field_1', 'field_2', 'field_3']);

Magento search queries yielding empty results in API

I have this chunk of code:
//to-do
public function searchVehicles($terms, $offset=1, $order='ASC')
{
if (trim($terms) == '') {
return array();
}
$query = $this->_getQuery($terms);
$query->setStoreId(1);
if ($query->getId()) {
$query->setPopularity($query->getPopularity()+1);
}
else {
$query->setPopularity(1);
}
$query->prepare();
$query->save();
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->getSelect()->joinInner(
array('search_result' => $collection->getTable('catalogsearch/result')),
$collection->getConnection()->quoteInto(
'search_result.product_id=e.entity_id AND search_result.query_id=?',
$query->getId()
),
array('relevance' => 'relevance')
);
$collection->setStore(1);
//Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
//Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
return $this->_listProductCollection($collection, $offset, $order);
}
Which is inside a Resource class and reachable via SOAP.
Before we start: Yes, I remember to do the cache flushing and recompiling process - I clarify because this is an usual issue to newbies like me xDDD.
Now: I can access such method but it returns [].
SPECIAL NOTE: $this->_listProductCollection($collection, $offset, $order); WORKS since i'm using the same method in other collections fetched from other methods in the same resource, and have no trouble at all.
Let me review the intention of my code since I'm a newbie at Magento (I'm using version 1.6.2).
The code is based on the CatalogSearch/ResultController controller's indexAction() method, and tried to learn about it.
An empty query will yield an empty result and will not bother the Magento search engine.
There's only a Store (id = 1) in the site and the search query is created like this:
private function _getQuery($terms)
{
$query = Mage::getModel('catalogsearch/query')->loadByQuery($terms);
if (!$query->getId()) {
$query->setQueryText($terms);
}
return $query;
}
The query increases it's popularity (I took this code from the controller. I assume this is for statistical purposes only).
The query is prepared (I think this means: the MySQL internal query is prepared) so I can fetch it later.
The query is saved - AFAIK this means that the query results are iterated and cached so a subsequent same query will only fetch the stored results instead of processing the search again.
At this point the query will have an ID.
I get the whole Product collection, and join it with the search result table. SEEMS that the results table has - at least (queryId, matchedProductId). I only keep the products having IDs in the matched results, and from store 1.
I list the products.
Note that the filters are currently commented.
However, the returned list is [] (an empty list) when I hit this API entry point, althought searching in the usual search bar gives me the expected result.
Question: What am I missing? What did I misunderstood in the process?

datamapper: get object order by relationships fields

I use Codeigniter with datamapper orm and have a problem
this are my models:
mailing -> has many row
row -> has many cell
cell -> has many version
version has one created and one updated field.
I want to get the last 10 mailings order by last version changes created or updated..
I thought to do it like this:
$versions = new Version();
now get last 10 versions order by created or updated
and distinct by mailing_id and now get all mailings to show...
like this: ?
foreach ($version as $v)
{
$v->mailing->get();
}
thx for helping
Yes, you can call ->get() on every related model inside a loop but this would generate a n+1 query scenario and be slow if you are looping over lots of version rows.
You can use the include_related to get full Mailing instances loaded with data when you query Versions in one step (with a join behind a curtain) like this:
$versions = new Version;
$versions->order_by(...)->limit(...); // add your ordering and limiting as before
$versions->include_related('mailing', null, true, true);
// include related mailings, with of their fields and create instances, see
$versions->get();
foreach ($versions as $version) {
// now the $version->mailing is a Mailing instance loaded with the related data
print $version->mailing->id
}

How can I retrieve the latest question of each thread in Propel 1.6?

I want to get the newest entries for each of my threads (private messaging system) with Propel 1.6 making use of the fluid ModelQuery interface. This would allow me to reuse both methods for getting newest entries and only getting entries where a user is involved (nobody wants to see messages not for him).
I already found out that in standard-SQL I have to use a subquery to get the newest entry for each of my forum threads. I also found out that in Propel you have to use a Criteria::CUSTOM query to achieve this, but the whole Criteria::CUSTOM stuff seems to be pre-Propel-1.6, because none of the examples makes use of the new ModelQuery.
Now the problem is, that I want to make use of the concenation feature in ModelQueries, where you can attach several own methods to each other like this:
$entries = MessageQuery::create()
->messagesInvolvingUser($user) // user retrieved or sent the message
->newestFromThread() // get the latest entry from a lot of Re:-stuff
I do not think that this would still be possible if I had to use
$c = new Criteria();
$c->add([the subquery filter]);
in newestFromThread().
What’s the best method to retrieve the latest entry for each thread given the following scheme (thread_id means that all messages belong to the same correspondence, I want only one entry per thread_id):
id(INT)
title(VARCHAR)
thread_id(INTEGER)
date(DATETIME)
The current PHP-implementation looks like this:
<?php
class MessageQuery extends BaseMessageQuery {
public function messagesInvolvingUser($user) {
return $this
->where('Message.AuthorId = ?', $user->getId())
->_or()
->where('Message.RecipientId = ?', $user->getId());
}
public function newestFromThread() {
return $this;
// To be implemented
}
}
And I am using it like this:
$messages = MessageQuery::create()
->messagesInvolvingUser(Zend_Auth::getInstance()->getIdentity())
->newestFromThread()
->find();
How about ordering results by date (DESC) and to limit to one result ?
Considering the answers in a similar question about pure SQL solutions, I guess it is easiest to add a new column newest indicating which message in a communcation is the newest. This probably fits the object-oriented approach of Propel better, too. I could write my application like this then:
public function preInsert(PropelPDO $con = null) {
$this->setNewest(1);
$this->getEarlier()->setNewest(0);
return true;
}

Resources