Laravel updateOrInsert with 'OR' or 'AND' operator in first arguments? - laravel

While it is possible to have multiple arguments for the updateOrInsert in Laravel query builder and what is the operator used by default.
For example in the documentation it is mentioned:
DB::table('users')
->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
Does that mean that email && name are checked or does it mean email || name is checked? How can we control it for one or the other if required?
Please forgive me if this is a silly question or if it is not worded as per the correct vocabulary, as I am new to Laravel. I couldn't find this information in the documentation or API.

updateOrInsert() method is used to update an existing record in the database if matching the condition or create if no matching record exists. Its return type is Boolean.
Syntax :
DB::table('blogs')->updateOrInsert(
[Conditions],
[fields with value]
);
In your query :
DB::table('users')->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
It will check if email == 'john#example.com' & name == 'john', then it will update votes=2.

Related

Row inserted but number not

I trying to insert a row in codeigniter and row inserted.
Problem is all row inserted properly but in sql bighint(11) field inserted 0.
I checked properly in array value given.
$data = [
'sku' => $POST['rec1'],
'pruch_price' => $POST['rec2'],
'sell_price' => $POST['rec3']
];
$model->insert ($data);
you should use $_POST[] instead of $POST.
But better yet, don't send $_POST directly to the models, instead use the post request provided by Codeigniter 4.
$data = [
'sku' => $this->request->getPost('rec1'),
'pruch_price' => $this->request->getPost('rec2'),
'sell_price' => $this->request->getPost('rec3')
];
$model->insert($data);

How to check for null in other field laravel FormRequest?

I am trying to exclude a field from being validated when another field has a value (not null). In this case, i want 'deleted_pictures' to be excluded if product_pictures is an array (not null). My problem is that i think exclude_unless:product_pictures,null evaluates null as a string, and not as, well, null.
This is my rule.
ProductRequest.php
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,null', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless product_pictures has a value of 'null'
I tried this to confirm my suspicion and it works like it should.
//test_field = 'some_value'
return [
'test_field' => 'required|string'
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:test_field,some_value', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless test_field has a value of 'some_value'
In my first case, deleted_pictures is excluded because it doesn't detect that product_pictures is 'null' (string)
While on the second case, deleted_pictures is NOT excluded because test_field matches the given value.
My question is, how do you evaluate null value in FormRequest Laravel?
So apparently you can just leave the second parameter blank to evaluate it as null
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,', 'required', new cant_delete_all($max,'1')],
];
I'm not sure if this is how its supposed to be done or intended behavior. But im just gonna leave this answer just in case someone might need it. If someone can suggest the 'proper' way of doing it then I'll accept that instead.

Any way to except empty field from $request->all() in laravel?

I want to except empty value field from $request->all();
Array ( [first_name] => Dev 1 [password] => [last_name] => [phone] => 123456 [password_confirmation] => )
I got the array like this but I want to except field from above array like last_name, password which has no value.
Any way to do this without for loop.
I mean laravel provide any default method for that ?
Thanks
array_filter will remove empty elements:
$filtered = array_filter($request->all());

Magento both AND and OR conditions in a collection

I want make a query which looks like below to filter some products (using attributes) from product collection.
SELECT <attributes>
FROM <tables & joins>
WHERE (<some AND conditions>) OR (<some AND conditions>)
WHERE condition should filter products that match either first set of AND conditions or second set of AND conditions.
Problem is I can't find a way to add an OR condition in between multiple AND conditions.
Can anyone help me to code above where condition using Magento addAttributeToFilter()? or any other functions?
If i'm understanding you correctly I think you need to do some variation of this:
->addAttributeToFilter(...filter here...)
->addAttributeToFilter(array(
array(
'attribute' => 'special_to_date',
'date' => true,
'from' => $dateTomorrow
),
array(
'attribute' => 'special_to_date',
'null' => 1
)
));
which would be:
...filter here... AND (special_to_date >= '2012-07-03' OR special_to_date IS NULL)...

What's the BNF of doctrine for?

It looks like a big mess,how does it work as reference?
http://www.doctrine-project.org/documentation/manual/1_1/en/dql-doctrine-query-language%3Abnf
I don't think it's used as a reference by any human-being, actually ; but it might be useful if someone want to use some automatic tool that understands BNF ; for instance, some kind of code generator.
The advantage of BNF being that's it's a formal way to describe a language -- much more easier to understand than english, when you are a program.
For reference :
BNF = Backus–Naur Form
Software using BNF
Edit after the comments : Here's a quick example about the DQL / Object stuff :
Let's consider this portion of code, which is using the object-oriented API to write a query, execute it, and get the results (hydrated as arrays -- prints out only the data, this way, when debugging) :
$result = Doctrine_Query::create()
->select('p.id, p.title, u.login')
->from('Ab_Model_Post p')
->innerJoin('p.User u')
->where('p.codeStatus = ?')
->orderBy('p.date desc')
->limit(2)
->execute(array('OK'), Doctrine::HYDRATE_ARRAY);
var_dump($result);
And here's the kind of output you'll get :
array
0 =>
array
'id' => string '7' (length=1)
'title' => string 'Septième post' (length=14)
'User' =>
array
'id' => string '1' (length=1)
'login' => string 'user1' (length=5)
1 =>
array
'id' => string '6' (length=1)
'title' => string 'Sixième post (draft=7)' (length=23)
'User' =>
array
'id' => string '1' (length=1)
'login' => string 'user1' (length=5)
Of course, this is considering the schema and models classes are OK -- and sorry for the example in french, I used a schema/model/database I set up some time ago for a demonstration of Doctrine, which was in french.
Basically, the DB is for a blogging application, and, here, we :
get some data from posts and the user who posted them
for valid posts
the most recents posts
only two posts
Now, here's an equivalent, using what I meant by "DQL" as in "pseudo-SQL language" :
$result = Doctrine_Query::create()
->query(<<<DQL
select p.id, p.title, u.login
from Ab_Model_Post as p,
p.User u
where p.codeStatus = ?
order by p.date desc
limit 2
DQL
, array('OK'), Doctrine::HYDRATE_ARRAY);
var_dump($result);
No object-oriented API here (well, to write the query, I mean) : I only wrote that pseudo-SQL I was thinking about -- which is what the BNF describes, as far as I can tell.
And, of course, the output of the var_dump is exactly the same as the one I got before.
I hope this makes things a bit more clear :-)
It's Backus–Naur Form, a method of describing context free grammars. See this wikipedia article.

Resources