Best practice for storing checkbox and textarea in Laravel - laravel

I have already seen a few different approaches for storing
textarea and checkbox and then showing them on edit view and show view...
but I am still not satisfied and would like to hear from You, how do you handle the unchecked checkbox and
especially the textarea security on show and edit with some wysiwyg plugin like ckeditor or tinymce?
For example for checkboxes I am using this setter;
public function setActiveAttribute($value): void
{
$this->attributes['active'] = $value ? 1 : 0;
}
Is this the best way to do it?
And for textarea on submit I allow everything and then on edit view I show it like this:
{!! Form::textarea($key, htmlentities($value), ['class' => 'form-control ckeditor-field']) !!}
and on show view I'm using a Purifier (link below) and I show it like this
{!! \Purifier::clean($value) !!}
So I just wanted to know what are the best practices? Is this fine what I'm doing?
https://github.com/mewebstudio/Purifier

Related

Blade view for rich text field on description tag

I'm using voyager in a laravel 7 shopping app for the description of all products.
I want to output the field for the description of the product
#section('description', {!! $item->description !!} )
and
for the facebook share = og:description" content="{!! $item->description !!}">
When I use {!! $item->description !!} in the body, no problem. But in the tag the output always read the p tag and all style form the description.
The weird thing is it's working find on localhost but not on the server. I tried various combination of solution with the same result. I feel there's a quick or maybe it's just not possible?
try html_entity_decode() ref link https://www.php.net/manual/en/function.html-entity-decode.php
{!! $item->description !!}
to
{!! html_entity_decode($item->description) !!} // it will render html
If you are using laravel 7/8 you can create a blade component then you can simple call
<x-editor :content="$description></x-editor>
check out this laravel docs
try this:
{{strip_tags(trim($item->description)}}

Need to go through 2 objects at the same time with VUEJS

I just want to throught POSTS and USERINFO at the same pace, in a way that in show the right user avatar for a given post
Objects
posts : {!! $posts !!},
userinfo : {!! $userinfo !!},
Template
< v-cloak v-for="post in posts">
<div v-bind:style="{ backgroundImage: 'url(' + post.picture + ')' }">
</div>
Dont know how... basicly...
The correct way (IMHO) is to provide the data to the template in a correct way, so the template just loops through the data the least amount of time.
To support that a computed value may be the easiest way to implement.
You can create a complex loop using js in you computed value, and then you reduce any complex logic in the template. This may also reduce the number of renders or re-computations needed if other parts of the component or template are changing.
other options are to update data.variable using a watch or from an api callback
I think you can add a property id to your user object.
And a property userId to your post object to avoid mistakes.
User has to be a prop of your element to be used into it

Laravel 5 Bind data to var for menu in header.blade.php

What is the best solution for creating a menu.
I have $menudata (dynamic from dbase) and want to pass it to a heade.blade.php to generate top menu.
$menudata = Menu::all();
#foreach($menudata as $value).......
How can i achieve that? What's the best way to do it?
Add below code within the boot method of the AppServiceProvider
View::composer('*', function($view)
{
$view->with('menudata', Menu::all());
});
Here, * means all views will receive $menudata.
Now you can use this like
#foreach ($menudata as $menu).....
If the menu is visible in every page then add the following in any service provider (e.g. in the AppServiceProvider):
public function boot() {
// ...
View::share("menudata",Menu::all());
//...
}
Then you can use the menu data in any view e.g.
#foreach($menudata as $value).......
Laravel-menu provides three rendering methods out of the box. However you can create your own rendering method using the right methods and attributes.
As noted earlier, laravel-menu provides three rendering formats out of the box, asUl(), asOl() and asDiv(). You can read about the details here.
{!! $MyNavBar->asUl() !!}
You can also access the menu object via the menu collection:
{!! Menu::get('MyNavBar')->asUl() !!}

Laravel input checkbox if selected on edit

I have an array that I am setting up as checkboxes
<?php $buslist = array('Brooklyn','Lakewood'); ?>
#foreach ($buslist as $buses)
{{Form::label('brooklyn_1',$buses)}}
{{Form::checkbox('BusList2[]', $buses,false, ['id'=> $buses]) }}
#endforeach
Then I switch it to an string with a , using implode
But when I try to edit my information, none of the checkboxes are selected and the information gets lost on update.
What code can i put into the blade checkboxes, if that bus is in my string list?
Checkboxes are finicky creatures. In your form you actually have to do something like this
{!! Form::hidden('new-group-user-member', 0) !!}
{!! Form::checkbox('new-group-user-member', true, NULL) !!} Member
When a checkbox is not checked nothing gets sent to the server so you never know when to change the value in the backend. If you add a hidden form field with the same name and a value of 0 it will get sent with the form even though the checkbox is not checked.
In third parameter use in_array($buses, $buslist) instead of false. Your blade code will look like this:
{{Form::checkbox('BusList2[]', $buses,in_array($buses, $buslist), ['id'=> $buses]) }}

cakephp updating elements

I have an index view which has some elements on it .
index controller code;
$userID = $this->Authsome->get('id');
$qnotes = $this->Qnote->getnotes($userID);
$this->set('qnotes', $qnotes)
$this->render();
elements have been added to the page using
index view code
<?php echo $this->element('lsidebar'); ?>
now the Issue is I also Have an add controller.
add controller code
function add() {
if(!empty($this->data)) {
unset($this->Qnote->Step->validate['qnote_id']);
$this->Qnote->saveAll($this->data);
$this->Session->setFlash('New Note Template has been added.','flash_normal');
}
}
now what I am trying to achieve is once I add a Qnote i want the element('lsidebar') updated
for the new Qnote.
I am Using the Ajax helper. found at http://www.cakephp.bee.pl/
also Here the add qnote View Code :
<?php echo $ajax->submit(
'Submit', array(
'url' => array(
'controller'=>'qnotes',
'action'=>'add')
));
I know its sound like a noob question . can Somebody point me in the right direction atleast.
I have tried everything i could think off. I bet the solution something easy which i didnt think off
help :)
If you want to dynamically update a sidebar with information that is submitted via ajax, there should be a "success" option in your ajax post that would allow you to fire a specific javascript action when the post is finished (or succeeds). You should write a small javascript ajax function to reload the contents of your sidebar when the post succeeds.
See this other stackoverflow answer: CakePHP ajax form submit before and complete will not work for displaying animated gif

Resources