Laravel console: How to output variables using - laravel

I've seen this question but the solution therein doesn't use the Laravel method.
I wanna do something like this:
$this->info('This is the result: ', $result);

I believe you are looking for:
$output = new \Symfony\Component\Console\Output\ConsoleOutput(2);
$output->writeln('This is the result: ', $result);

Try this
$this->info('This is the result: ');
dump($result);
You can use dump() for print print objects

Btw, Do you know you can include variable inside double quote to output, the first basic thing of php.
$this->info("This is the result: {$result}")

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 ¯\_(ツ)_/¯

how to display single value use arrayshit or other thing (without loop) in Laravel

I use display value or data. In case of a single value use loop, but I know this is a bad habit. How can I solve this? for example I use this:
#foreach($result as $result)
{{$result->data}}
#endforeach
First of all, your code doesn't tell anything, and also you have this code all wrong.
You can't name a pointer as the same name of the array, ($result as $result ???) you will have wrong results or errors.
Also inside the froeach you are referencing a variable that doesn't exists (?).
In my opinion "foreach" is necessary to loop through arrays.
I really don't know what are you trying to do, so that would be great if you expand your question and provide more information.
You can replace that with some code like this:
#if($result)
{{$data}}
#endif
You can try code below see
//example in file ExampleController.php
use App\Posts;
public function showAll(){
$data = Posts::all();
return view('showall',['result'=>$data]);
}
//file web.php
Route::get('/show-all','ExampleController#showAll');
//show-all.blade.php
#foreach($result as $value)
{{$value.title_name}}
#endforeah

Magento registry variables in controller

I have stored a variable in register by Mage::register('captcha', $var); in helper. And in the controller i tried to retrieve the variable by using Mage::registry('captcha'); But i dont getting any values here. Please help me to solve this.
In your helper file create a function like below :
public function getCaptcha(){
$var = 'myValue123';
Mage::register('varun', $var);
return Mage::registry('varun');
}
In your controller function:
$registryValue = Mage::helper('yourModule')->getCaptcha();
echo registryValue ; //prints myValue123
Hope it helps !!!
It's look like syntax is right.
Please first try to set some static value like $var="test"
Mage::register('captcha', $var);
after that got this value in controller.
Mage::registry('captcha');
if you got this value test then i think you have problem with $var in your helper.
Let me know if you have any problem
'captcha' is already in use, so magento never set your data in registry. Change the name, for example 'captcha1'
Mage::register('captcha1', $var);

How to put command include ("/path/public_html/file.php"); inside a variable?

I have something like
$example['description']="print this text: *** " ;
and instead of * I like to include a file /home/path/public_html/list.php where is stored the html text (php formatted).
How to implement the function
include ("/home/path/public_html/list.php");
inside the variable? Thank you for help.
One way would be:
ob_start();
include "/home/path/public_html/list.php";
$text = ob_get_clean();
$example['description'] = "print this text: $text";

Resources