laravel - #if won't accept database variable - laravel

How can I solve this? I've tried anything but I still can't compare the uuid from the database with a local id I created.

you don't need to use {{}}, as they internally render to echo().
The comparison is just:
#foreach($tasks as $task)
#if($task->uuid == $id)

You don't need to use {{}} in controll statements of blade temple. {{}} Is used while rendering any variable on the views.

Related

Difference between #aware and #props directives in laravel 9

The #aware Laravel Blade template directive was introduced in Laravel 8, but I still don't understand what it does differently from the already existing #props directive.
For instance, if I use a Blade template like this:
<x-view-content :page="$page ?? ''" />
and the component used above is defined in views/components/view-content.blade.php like this:
#aware(['page'])
<div>
{{ $page }}
</div>
I get the page successfully rendered inside the component. However, replacing #aware(['page']) with #props(['page']) produces the same result.
I would like to know what the differences between them are.
#aware is for accessing data from a parent component where you have #props. You can find here all relevant information. In your case you don't have child component.

How to render multiple livewire component as string?

I have multiple livewire components e.g. opd.patient-count, opd.visit-count, opd.checkup-count and so on. I would like to store these components name into database then call by user role. Anyway, It does not work once I tried as below.
foreach ...
echo '<livewire:opd.patient-count />';
endforeach ...
Any advice or guidance on this would be greatly appreciated, Thanks.
As I understand your problem, You can achieve using this syntax instead
#foreach($components as $eachComponent)
#livewire($eachComponent,['componentData' => $data])
#endforeach
in this code above, the component name is assigned accordingly using a blade #foreach directive and iterate over each element and make the component.

Is it possible to manipulate the output of a laravel #yield in the blade template?

Wondering if it is possible to manipulate the output of a #yield in Laravel? In short I have a title in my child template that I would like to set some id's with to make them unique. I know I could just create another #section('id', 'asdf') but rather not have to worry users with that especially since the title has to be unique anyways...
Could not find anything that says this is possible?
Example (which fails) but what I am essentially trying to do:
id="{{Str::kebab(#yield('title'))}}-preview-tab"
#yield gets replaced with a PHP echo statement so that is not what you want. If you want the content of a section you can grab it from the View Factory:
$__env->getSection($name, $default)
Or even calling yieldContent:
$__env->yieldContent($section, $default)
So you could try:
{{ Str::kebab($__env->getSection('title', 'some default if you want')) }}
If you have any issues with that, try the yieldContent method.

How to keep the line breaks from user input but also sanitize in blade?

I try to render an data from user textarea input saved in my database.
I need to keep the line breaks use nl2br,
and also want to santize to prevent malicious script by using blade {{{ }}}.
But {{{ nl2br($output) }}} wont work, the br tag would also be sanitize.
Please give me some hint, thanks.
For Laravel 4 users:
{{ nl2br(e($message)) }}
e($x) is equivalent to {{{ $x }}}.
Laravel 5 users:
{!! nl2br(e($message)) !!}
e($x) is equivalent to {{ $x }}.
Sawny's answer is a great one that really leverages the power of the Blade syntax well, except I would take it a step further. You can use Blade::extend to create your own Blade # shortcodes so I use the following:
Blade::extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('nlbr');
return preg_replace($pattern, '$1<?php echo nl2br(e($2)); ?>', $value);
});
Now in your Blade template all have to do is something like this:
<div>#nlbr($sometext)</div>
EDIT: I realized someone coming across this may very well wonder, "Where do I put the Blade::extend function?"
To be honest, it can go in a lot of places (and it depends on if you're using Laravel 4 or 5 as to the 'best' approach).
A simple place to put it is in the routes.php or global.php files as they will get picked up with the least effort. These are however, not the best files to put them in and you would be best off learning to create Laravel Providers.
simply use this remove br no need to write
this function nl2br
it work fine
{!!($mylogin->title) !!}

CodeIgniter - form_dropdown defaults to multiple

Im having a little trouble with my form_dropdown in codeigniter, basically it ALWAYS adds the multiple="multiple" and i dont know how to get rid of it.
Here is the code im using to generate the dropdown
$js = 'class="users"';
echo form_dropdown('users', $users, set_value('users', $users), $js);
Is there anything i can add so that it doesnt automatically create it with the multiple option
I think the issue is related to your third option set_value('users',$users)
Since $users is probably an array, the set_value may be setting multiple options to selected and in such a case form_dropdown would generate the multiple property.
Try passing a single user value and make sure that works as you expect.
Also, check the out put from the set_value function to see if it returns an array instead of a single value.
You don't need to use set_value here. Just use the value you want selected.
$js = 'class="users"';
$user = 1;
echo form_dropdown('users', $users, $user, $js);

Resources