Trying to sum a groupBy value in Laravel 5.5 - laravel

I have this code suggested to me. I am trying to sum the stock_in_qty and stock_out_qty.
$warehouse1stocks = Warehouse1stocks::select(
"order_item_id",
Warehouse1stocks::raw('SUM(stock_in_qty) as stock_in_qty'),
Warehouse1stocks::raw('SUM(stock_out_qty) as stock_out_qty'))->groupBy('order_item_id')->get();
// dd($warehouse1stocks);
return view("warehouse1.index", compact("warehouse1stocks", $warehouse1stocks));
My problem is this error
I tried to look for an answer from other questions here and I think my code seems ok but why am i still having that error?
what do you think is the problem with my code? thanks in advance!

Instead of writing:
Warehouse1stocks::raw('SUM(stock_in_qty) as stock_in_qty')
write:
DB::raw('SUM(stock_in_qty) as stock_in_qty')
when defining the write expressions

use this one
$warehouse1stocks = Warehouse1stocks::select(
"order_item_id",
\DB::raw('SUM(stock_in_qty) as stock_in_qty'),
\DB::raw('SUM(stock_out_qty) as stock_out_qty'))->groupBy('order_item_id')->get();
// dd($warehouse1stocks);
return view("warehouse1.index", compact("warehouse1stocks", $warehouse1stocks));

Related

Get first letter of word in statement

I have a statement like "Animal Association" from the database. I want to get its short form. It means, only the first letter of each word like this "AA". In the blade file, I got the whole statement as follows,
<p>{{ $animal->user->club->name}}</p>
So, how can I get a short form of this name?
Thank You!
If you are using MySQL 8+, then a raw select with REGEXP_REPLACE should work here:
$users = DB::table('animals')
->select(DB::raw("SELECT REGEXP_REPLACE(name, '(\\w)\\w+\\s*', '$1')"))
->get();
This very common problem where we ran into, I can provide you a function that will solve your problem. I am sharing two solutions and you can use any of these solutions.
using function
You can use this function in your model and solve your problem.
public function getNameAbbreviate($string){
$abbreviation = "";
$string = ucwords($string);
$words = explode(" ", "$string");
foreach($words as $word){
$abbreviation .= $word[0];
}
return $abbreviation;
}
There is probably no one-line solution, the solution which I provided is readable and understandable.
using regex
This solution is easy to apply and in case you can't make the first method work then go with this.
<p>{{ preg_split("/\s+/", $animal->user->club->name) }}</p>
using regex we can get a direct solution but I personally don't like it or recommend it.

Laravel whereRaw - how to escape a '?'

Is there a way to escape a "?" in a eloquent whereRaw Statement? (Using laravel 6.x)
example:
ExampleModel::whereRaw(' "table"."json_field"::jsonb ?| array[\'test\', \'test2\'] ')->get();
This gets sent to the db as
where "table"."json_field"::jsonb $1| array['test', 'test2']
And well, thats not what i wanted to query...
Tried with '\?', put it in a binding (Laravel doc) - still no success.
Also i didn't find a reference in the docs ...
In my Usecase i want it to compare a json object with the psql comparing "?|" (Postgres Doc)
Thanks in advance!
the question is pretty old but my answer might help anyway.
The solution is very simple: just escape the questionmark with another questionmark.
The code of your example would look like this:
ExampleModel::whereRaw(' "table"."json_field"::jsonb ??| array[\'test\', \'test2\'] ')->get();
This solution has been tested with Laravel 7.28.3.
Didn't test but try using PDO:
$whereRaw = DB::connection()->getPdo()->quote(' "table"."json_field"::jsonb ?| array["test", "test2"] ');
ExampleModel::whereRaw($whereRaw)->get();
Well, didn't find an answer, but a workaround:
Don't use the question mark operators!
Instead i went for the named function. I found the named function via
SELECT
oprname,
oprcode || '(' || format_type(oprleft, NULL::integer) || ', '
|| format_type(oprright, NULL::integer) || ')' AS function
FROM pg_operator
WHERE oprname = '?|';
( Found there: big thanks to this guys post!)
So my Eloquent query now looks like:
ExampleModel::whereRaw('jsonb_exists_any("table"."json_field"::jsonb, array[\'test\', \'test2\'])')->get();
At least its working ¯\_(ツ)_/¯

Seperate initials in freemarker

Is it possible to set points in initials?
For example to change MAW into M.A.W.
I tried keep_before, but it doesn't work.
?keep_before(" ")+". "}
Result: MAW.
Please help.
You could do it like:
${'MAW'?replace('','.')[1..]}
'MAW'?replace('','.') will result in .M.A.W., which you can "substring" by using the range [1..].
See
https://freemarker.apache.org/docs/ref_builtins_string.html#ref_builtin_replace
https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_stringop_slice
It's easiest to do with regular expressions: ${initials?replace('.', '$0.', 'r')}. It's maybe nicer if you wrap this into a #function though (<#function dotify(s)><#return s?replace('.', '$0.', 'r')></#function>, and then ${dotify(initals)}), especially if you need to do this on multiple places.
If your letters are in name try:
<#list 0..(name?length-1) as idx>${name[idx]}.</#list>

Codeigniter problem with routes!

I am trying to do this route trick:
$route['cp/roles/:num'] = "cp/roles/index/:num";
but it doesn't work :(
please help me!!
advanced thanks .
According to the documentation on URI Routing:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
“A URL with "product" as the first segment, and a number in the second will be remapped to the "catalog" class and the "product_lookup_by_id" method passing in the match as a variable to the function.”
So, for your particular instance, you would do the following:
$route['cp/roles/(:num)'] = "cp/roles/index/$1";
You could try
$route['cp/roles/:num'] = "cp/roles";
and then instead of passing a variable in your function you use
$this->uri->segment(3);
or the number that correspond to the segment.

Smarty issues with Fckediter

I'm new to this forum , i hope u do not mind questions even if its stupid.
i m trying to post a value from the fckeditor embedded in smarty template.The value submitted is,
a
b
c
d
however when echo the posted value i get ,
a b c d
which is very irritating because i want the actual value submitted.
No matter whatever i do i only see text with these tags i do not understand if have to do any configuration in smarty or fckeditor or what ?
Please help with this any help will be greatly appreciated.
i will appreciate your help
Mukesh ?
It was just that my framework used was sanitizing all GET,POST requests.
Then removed those function that was causing the function and job done .
function sanitize($data)
{
$data = trim(strip_tags($data, "<a><b><strong><em><i><u><br><h3><h4><h5>"));
return $data;
}

Resources