Laravel Use DB::Raw with Concat if value not null - php

I need to concat values in Laravel Update query.If Id value is 12, and If I need to append 14 to it, it should be 12,14.if ID value is null, then it should append as 14 instead of ,14.I used the following code,
$inscustrec =DB::table('configure')->where('POST_ID', $request->postid)
->update(
[
'ID' => DB::raw('CONCAT(COALESCE(ID),",'. $insrec.'")')
]);
i am getting comma as first character if the ID value is null.
I tried the following code,
$inscustrec =DB::table('configure')->where('POST_ID', $request->postid)
->update(
[
'ID' => trim(DB::raw('CONCAT(COALESCE(ID),",'. $insrec.'")'),",")
]);
But this code not working.
Any help would be greatly appreciated.

Give this a go (not sure on your SQL variant my example is using MySQL):
$inscustrec = DB::table('configure')->where('POST_ID', $request->postid)
->update([
'ID' => trim(DB::raw('CONCAT_WS(",", COALESCE(ID, "'. $insrec.'"))'))
]);
Hope $insrec does not come from user input as its interpolated directly into SQL.

Related

Laravel - validate input to be in the interval [-1, 10] without 0. not_in not working properly?

As the title says, I'm having trouble validating an input field like I said in the title.
I tried it this way:
$request->validate([
'nota' => 'min:-1|not_in:0|max:10',
]);
Basically, I want this "nota" field to have values in the interval [-1, 10] without 0.
But I can still enter 0.
Why doesn't it work and how can I fix it?
You can try like this:
use Illuminate\Validation\Rule;
$request->validate([
'nota' => [Rule::notIn([0]), 'between:-1,10'],
]);
or generate numbers yourself like:
$request->validate([
'nota' => Rule::in(array_diff(range(-1, 10), [0])),
]);

How do save all request data to database

how can save all to Database?
$request->validate([
'name' => 'required',
'email' => 'required',
'street' => 'required',
'street_addition' => 'nullable',
'postal_code' => 'required',
'city' => 'required',
'country' => 'required',
'vat_id' => 'nullable'
]);
$user = Auth::user();
$billingDetail = $user->billingDetails()->save(new UserBillingDetail([
$request->all()
]));
the $request->all() dont work. idk why. it comes
SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a
default value" and email and and and...
The SQL error you are experiencing is because your columns doesn't allow NULL values and it produces this error, when you try and put in a row with empty values.
The reason why your values are empty, is because you're wrapping your UserBillingDetail construct parameter with an []:
$billingDetail = $user->billingDetails()->save(new UserBillingDetail([
$request->all()
]));
This will effectively send an array with a single key (0) containing the value of your request->all(). Hence, the name, email, etc., fields doesn't exist in the top level array (which is what you want).
You should remove the extra brackets, making your code look like this:
$billingDetail = $user->billingDetails()->save(new UserBillingDetail(
$request->all()
));
The error message already give you the answear. you pass data and some fields are empty.
first of all: make sure that this fields not empty. After then check if the fields are declare as fillable in your model. protected $fillable = [...]
Try using $request->except('_token') in place of $request->all() as the request also have _token key in it.
error message already give you the answer. you pass data and some fields are empty. or make the field nullable.

Codeigniter's update_batch for POINT type MySQL data type?

$locations[] = [
'id' => $r['id'],
'city' => $loc['city'],
'state' => $loc['state'],
'country' => $loc['country'],
'long_lat' => "POINT(".$loc['longitude']." ".$loc['latitude'].")"
];
$this->db->update_batch('locations', $locations, 'id');
My table has a POINT datatype column for long_lat. The above snippet does not work with the long_lat insertion entry, without it it works fine. For some reason, update_batch cannot handle the POINT() datatype.
Codeigniter doesn't seem to like the POINT type, or its escaping something. Is there a work around for this?
i can't comment so.. try it like this and it will help if you echo the query and post it
'long_lat' => "'POINT(".$loc['longitude']." ".$loc['latitude'].")'"

What are the specifics for Laravel Query Builder's updateOrInsert function?

This function is not in the Laravel documentation, I have found it in the source code however I am not completely sure how it should be used. For example, if I am working with products, I want to either insert or update a product in the database based on its UPC. If a product with the same UPC exists, update it with the new values. If not, insert the new values as a new product. How should this be done using this function?
Thank you!
Insert or update a record matching the attributes, and fill it with values.
updateOrInsert(array $attributes, array $values = [])
https://laravel.com/api/master/Illuminate/Database/Query/Builder.html#method_updateOrInsert
DB::table('products')->updateOrInsert(
[
'upc' => $request->get('upc'),
],
[
'upc' => $request->get('upc'),
'name' => $request->get('name'),
'vendor' => $request->get('vendor'),
'description' => $request->get('description')
]
);

Change fields name while fetching record in cakephp 3

I want to change field name and while fetching record from database
Ex.
$query = $formsElements->find('all')
->where(['Forms.slug' => 'allergy'])
->contain(['Forms'=>['fields'=>['id', 'name']], 'Elements']);
Above is my query and in same query i want to change name of "id" field as form_id.
Can you guys please suggest better ways to handle this without virtual field.
Thanks
you can use an alias, the manual says
You can set aliases for fields by providing fields
as an associative array:
So all you have to do is:
->contain([
'Forms'=>[
'fields'=>[
'form_id' => 'id',
'name'
]
],
'Elements'
]);

Categories