I am trying to use Laravel updateOrCreate to update a record if its within this month and last month based on if an ID exists.
\App\Models\Result::updateOrCreate(
[
'id' => $request->get('id'),
// where created_at is within the last 2 months
'created_at' => [
'>',
\Carbon\Carbon::now()->startOfMonth()->subMonthsNoOverflow()
],
],
[
'discipline_one' => $request->get('d-one'),
'discipline_two' => $request->get('d-two'),
'discipline_three' => $request->get('d-three'),
'discipline_four' => $request->get('d-four'),
'discipline_five' => $request->get('d-five'),
'discipline_six' => $request->get('d-six'),
]
);
If the ID exists, and the result is in this current month or last, a new record is created instead of updated.
Expected input: 1 (which exists from last month)
Expected output: Record is updated
Expected input: 2 (which doesn't exist or is not within 2 months)
Expected output: New record is created
Update:
Using the answer suggested,
'id' => $request->get('id'),
'created_at' => \App\Models\Result::whereBetween('created_at', [
\Carbon\Carbon::now()->startOfMonth()->subMonthsNoOverflow(),
\Carbon\Carbon::now()->endOfMonth(),
]),
I get the error:
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
Here you go:
You only need to explicitly define the operators in the method's first parameter.
Each element of the array in the method's first parameter should be an array containing the three arguments.
column, operator, value
$startDate = \Carbon\Carbon::now()->startOfMonth()->subMonths(2);
$endDate = \Carbon\Carbon::now()->endOfMonth();
$resultRow = \App\Models\Result::query()
->where('id', $request->id)
->whereBetween('created_at', [$startDate, $endDate])
->first();
\App\Models\Result::updateOrCreate(
[
[
'id', '=', $request->get('id'),
],
[
'created_at', '>=', $startDate,
],
[
'created_at', '<=', $endDate,
],
],
array_merge(
[
'discipline_one' => $request->get('d-one'),
'discipline_two' => $request->get('d-two'),
'discipline_three' => $request->get('d-three'),
'discipline_four' => $request->get('d-four'),
'discipline_five' => $request->get('d-five'),
'discipline_six' => $request->get('d-six'),
],
$resultRow ? [] : [
'id' => $request->get('id'),
'created_at' => \Carbon\Carbon::now()
]
)
);
Notice that I repeated id & created_at column settings in the method's second parameter. It's because contrary to the regular behaviour, a merge of the 2 method's parameter arrays wasn't performed based on my tests if no result was found with that criteria.
Related
I have a Laravel collection with record IDs and foreign keys:
{id=1, foreign_id=1},
{id=2, foreign_id=1},
{id=3, foreign_id=2},
{id=4, foreign_id=3},
{id=5, foreign_id=2}
I expect:
{id=2, foreign_id=1},
{id=5, foreign_id=2},
{id=4, foreign_id=3}
I want to search Laravel query builder for unique values for foreign_id if id in collection occurs more than 1 time.
I want then to give latest foreign_id.
Try $collection->unique('foreign_id');
Here I'm giving an example, You can check by yours,
$a = collect([
[
'id' => 1,
'foreign_id' => 2
],
[
'id' => 2,
'foreign_id' => 1
],
[
'id' => 3,
'foreign_id' => 2
],
[
'id' => 4,
'foreign_id' => 3
],
[
'id' => 5,
'foreign_id' => 2
],
]);
$a->unique('foreign_id');
The easiest way to do it is to sort collection by "id" in descending order and than use unique method by "foreign_id"
$myCollection->sortByDesc('id')->unique('foreign_id')
I'm doing "inserts" by $push in an array inside my document, and inside it there is a field date that I want to sort it when I use the find(), but not sorting the "_id". How do I sort by date (ordens.dtOrdem)?
<?php
$mongo = (new MongoDB\Client('mongodb://localhost:27017'))->Carteira;
$collection = $mongo->ativo;
/*First way that I've tried*/
$result1 = $collection->find([].['ordens' => ['sort' => ['dtOrdem' => -1]]]);
/*Second way that I've tried*/
$result2 = $collection->find([],['ordens' => ['each' => ['ordens' => ['sort' => ['dtOrdem' = -1]]]]]);
?>
The field 'ordens.dtOrdem' is no been sorted in descending.
You cannot sort an array during a find(). You have two options:
You can specify a $sort operation during your $push to ensure that your elements are added to the array in sorted order.
You can using aggregation to sort the array on retrieval.
Solutions might look something like the following (some modification may be necessary):
// Sorting on push.
$collection->updateOne(
[...],
[ '$push' => [
'ordens' => [
// You MUST include the $each operator here or this will not work.
'$each' => [
[ 'dtOrdem' => 5 ]
],
// Where the magic happens, sorts in descending order.
'$sort'=> [ 'dtOrdem' => -1 ]
]
]]
);
// Sorting on retrieval.
$collection->aggregate([
[ '$unwind' => '$ordens' ],
[ '$sort' => [
'$ordens.dtOrdem' => -1
]],
[ '$group' => [
'_id' => '$_id',
'ordens' => [
'$push' => '$ordens'
]
]]
]);
Is there a way to updateorcreate records in a table where the created_at date field is greater than today(midnight)?
I have the following sample:
Model::updateOrCreate(
[
'match_id' => $match->id,
// 'created_at' => some query here to match a record created today
]
);
Here's the solution I found to work:
Model::updateOrCreate(
[
'match_id' => $match->id,
'created_at' => Model::where('created_at', '>', DB::raw('CURDATE()'))->first()->created_at ?? null
],
[
'otherColumn' => $value
]
);
How do I write this kind of COALESCE() statement in the query builder?
SQL
SELECT COALESCE(n.value, p.value) AS value
FROM nodes n
LEFT JOIN parents p ON p.id = n.parent_id
PHP
I can retrieve both the child and parent values and then go through the result set and just use the parent one if the child one is empty, but if there is a more elegant way to build it into the query itself, I would prefer that.
$child = $this->Nodes->find()
->select(['id', 'value'])
->where(['Nodes.id' => $id])
->contain([
'Parents' => function ($q) {
return $q->select('value');
}
])
->first();
if (empty($child->value)) {
$child->value = $child->parent->value;
}
Update 1
So this is what I have at the moment, but it doesn't work.
$child = $this->Nodes->find()
->select(['id', 'value'])
->where(['Nodes.id' => $id])
->contain([
'Parents' => function ($q) {
return $q->select([
'value' => $q->func()->coalesce([
'Nodes.value',
'Parents.value'
])
]);
}
])
->first();
Returns:
object(Cake\ORM\Entity) {
'id' => (int) 234,
'value' => (float) 0,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Nodes'
}
The child value is NULL and the parent value is 1.00 so I would expect the entity value to be 'value' => (float) 1.00 but I assume it's coming out of the query as FALSE converted to (float) 0.
Update 2
It seems aliasing the coalesce to a name which already exists as a normal field does not work. It requires a unique field name for the coalesce result.
Update 3
I did another test and selected the name field from the two tables instead, and it just returns the actual strings I entered into the function (they do not get evaluated as column names):
return $q->select([
'value' => $q->func()->coalesce([
'Nodes.name',
'Parents.name'
])
]);
The returned entity has:
'value' => 'Nodes.name'
So my new question would be how to get the query builder to evaluate the strings as table/field names?
I could not get Cake's coalesce() function to evaluate the parameters as fields, it was just returning the actual strings of the field names.
I got it working by manually creating the COALESCE statement instead.
// Create the query object first, so it can be used to create a SQL expression
$query = $this->Nodes->find();
// Modify the query
$query
->select([
'id',
'value' => $query->newExpr('COALESCE(Nodes.value, Parents.value)')
])
->where(['Nodes.id' => $id])
->contain('Parents')
->first();
CakePHP's coalesce() uses the following format to differentiate field names from literal values:
'value' => $q->func()->coalesce([
'User.last_name' => 'identifier',
', ',
'User.first_name' => 'identifier'
])
The code above should yield results like Smith, John.
The default behavior is to treat the element as a literal.
See https://api.cakephp.org/3.3/class-Cake.Database.FunctionsBuilder.html#_coalesce
The docs don't explain this well at all. H/T to https://www.dereuromark.de/2020/02/06/virtual-query-fields-in-cakephp/ for a clear example.
See http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions
Haven't tried it but I guess it's:
$child = $this->Nodes->find()
->select(['id', 'value'])
->where(['Nodes.id' => $id])
->contain([
'Parents' => function ($q) {
return $q->select(['value' => $query->func()->coalesce([
/* Fields go here... I think. :) */
])]);
}
])
->first();
If this isn't working check the unit tests of the core how to call this function.
I use the sync function for syncing a belongsToMany Relation:
$model->products()->sync($productIds);
In the $productIds array there is flat array with some Id's -
something like this:
$productIds = [1,3,5,6];
What I want:
The pivot table has also additional columns like "created_by" and "updated_by".
But how can I add these fields to my array WITHOUT doing a foreach loop?
Is there a shorter way to do this?
I need an array like this:
$productIds = [1 => [
'created_by' => 1,
'updated_by' => 1
],3 => [
'created_by' => 1,
'updated_by' => 1
],5 => [
'created_by' => 1,
'updated_by' => 1
],6 => [
'created_by' => 1,
'updated_by' => 1
]];
Yes I know I can do it with foreach and add the columns while I loop through the array. But I want do it shorter.. is there a way to do it shorter (perhaps with laravel)?
It should be enough to pass what you have set in $productIds in your code example to sync().
This method works not only with array of integers. You can also pass an array where key is the synced ID and value is the array of pivot attributes that should be set for given ID.
This should do the trick:
$productIds = [
1 => [
'created_by' => 1,
'updated_by' => 1
]
//rest of array
];
$model->products()->sync($productIds);
Just make sure you have defined those fields as pivot fields in your relation definition.
In order to generate such table based on a list of IDs in $productIds you can do the following:
$productIds = array_fill_keys($productIds, array(
'created_by' => 1,
'updated_by' => 1,
));