How to render multiple livewire component as string? - laravel

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.

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.

laravel - #if won't accept database variable

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.

Request data from database after select in a form has been used

I am really sorry that I bother you again with my question, but this time I am asking about a kind of best practice. I found already various articles/posts about the topic and followed this article: https://makitweb.com/auto-populate-dropdown-with-jquery-ajax-in-laravel/ how to fill a second select based on the choice of a first select. As far as I can see, I have to use javascript and create new route.
My question would be is a new route the best idea because I also can also just call it by itself in the URL bar, eg. URL /getEmployees/1
How can I archive that I still get the required information but at the same time avoid that the URL can be used. Do I have to fetch all required data in the controller itself before I return the view and then filter them?
Thanks
Stephan
Update:
Another approach/try,
What is if I pass directly both tables to my view:
return view ('test')->with(compact('stuff_types'))->with(compact('positions'));
By the way, it really does not matter to select all data in the first instance, both tables will only have a couple of rows.
However, is it now possible to put a filter on the second table in the select view?
{{Form::select('position2', $positions, null, ['class'=>'form-control', 'id'=>"positions2"]) }}
I already tried $positions->where(‘id’, ‘1’), searched the internet for other ways but it always filles the select with the complete table information.
Later on I would love to put a filter with a javascript on the second table, based on the result of the first select, but at first it would be really nice just to see a where condition on it.
Update 2
Ok, another update / question:
Instead of using laravel collectives with
{{Form::select('position2', $positions::pluck('name', 'id'), null, ['class'=>'form-control', 'id'=>"positions2"])}}
I can use html and add an if condition, which I guess can be modified through javascript (will check tomorrow)
<select name="categories" id="categories" class="form-control">
#foreach($positions as $sector)
#if ($sector->id == 2)
<option value="{{ $sector->id }}">{{ $sector->name }}</option>
#endif
#endforeach
</select>
But now I am struggling with my controller. Before I was using this:
$ positions = DB::table('positions')->pluck('name','id');
But this does not seem to work with a standard select so I had to change the pluck to get, but now the larabel collective is not working anymore
$positions = DB::table('positions')->get();
Can anyone explain we why get() works with html, but not with laravel collectives, and vice versa?

How to include blade tempate in another file?

I have two fiels:
landing.blade.php
And block comments.blade.php.
How can I include comments.blade.php inside landing.blade.php?
You can include one blade template into other by following syntax:
Including Sub-Views
#include('view.name')
You may also pass an array of data to the included view:
#include('view.name', array('some'=>'data'))
For more details you can follow:
https://laravel.com/docs/5.0/templates
Thanks
According to the documentation you need to #include it:
landing.blade.php:
<div>
Some content
#include('comments', ['comments' => $comments])
</div>
simple way,
if landing.blade.php and comments.blade.php are on the same directory, let's assume they are both on ressource/view
So on landing.blade.php you can write #include('comments')

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) !!}

Resources