How to save multiple checkboxes in Laravel? - laravel-5

I have created a blade (using Laravel Collective) with a multipe checkboxes:
#foreach($subsc as $subsc)
<div>
{{Form::checkbox('checkbox['. $subsc->Scheme->Scheme_id .']', '1')}}
{!! Form::label('SchemeName', $subsc->Scheme->Scheme_Name.$subsc->Scheme->Scheme_id, ['class' => 'control-label']) !!}
</div>
#endforeach
Now I want to save each checked box in a table as the scheme_id. How do I do that?

I guess you could use an array as name to make it easier and try something like this.
Form::checkbox('schemeIDS[]', $subsc->Scheme->Scheme_id, true);
// Parameters checkbox: name, value, checked
In the controller function use
$schemeIDS = $request->get('schemeIDS'); // get all the checked values as array
foreach($schemeIDS as $schemeID)
{
// insert into the database
}

Related

Pass variable from blade to blade

I'm new to uusing Laravel blade templating.
I have a bootstrap modal that I need to show on button click, also, I need to pass some values on that click event.
For example.
On my parent blade: (btw, its a nested modal blade)
modal_1.blade.php
<div class="modal" id="modal1">
...
#foreach($templates as $key => $val)
<button onclick="previewItem($templates[$key]['color'])">
</button>
#endforeach
</div>
<script>
const previewItem = (color) => {
// how to pass this `color` to the child modal blade
}
</script>
and the child blade modal
modal_2.blade.php
<div class="modal" id="modal2">
// how to access passed variable from modal1 ?
</div>
you can create a component and inject here your variables:
#component('components.updateMyUserModal' , ['modal_id' => $account->id , 'modal_title' => $account->title , 'modal_body' => $aContext, ] )
#endcomponent
one file needs to be inside the other, after that, you need to use cookie, to store the variable's value in the previous modal, when you click on the button to select the value, make by javascript a cookie that saves this value and on the next blade you recover that value and uses.
How to use:
https://www.w3schools.com/js/js_cookies.asp

Backpack for Laravel - Upload CSV and Store as JSON in Database

I have successfully configured Backpack for Laravel to handle all of the CRUD ops for my app except for one particular field. I need to import a CSV, convert it to JSON and then store the JSON in my database to later display as an HTML table.
I created a custom field in Backpack, which does in fact create a field to upload the file, but I can't figure out how to point it to a custom method to handle the logic to handle the conversion and storing of the CSV.
I have combed the documentation but I don't see any mention of anything like this. https://backpackforlaravel.com/docs/3.4/crud-fields#creating-a-custom-field-type
//Placed in the Controller CrudPanel Configuration
$this->crud->addField([
// CSV to JSON
'label' => "Specs Table",
'name' => "specifications_table",
'type' => 'csv2json', //custom field name
], 'update/create/both');
//Placed in csv2json.blade.php file
<div #include('crud::inc.field_wrapper_attributes') >
<label>{!! $field['label'] !!}</label>
<input
type="file"
name="{{ $field['name'] }}"
value="{{ old($field['name']) ? old($field['name']) : (isset($field['value']) ? $field['value'] : (isset($field['default']) ? $field['default'] : '' )) }} "
#include('crud::inc.field_attributes')
>
{{-- HINT --}}
#if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
#endif
</div>
If you need extra logic for a custom field type, it's always a good idea (imho) to do it inside the model, as an accessor for that particular model attribute.
Take the image field type as an example: it requires you to have a setImageAttribute() accessor, where the actual uploading and storing is done.
You could add a new method on your model:
public function setSpecificationsTableAttribute($value) {
// read $value
// convert it to JSON
// $this->attributes['specifications_table'] = $fileConvertedToJson;
}
Hope it helps.

Laravel : How to hide url parameter?

Here the scenario is I want to pass a variable which will be send from one page to another and in next page it's gonna store through a form. So I have passed the variable from first page to second page through the URL. But I want to hide the parameter in the URL. How do I do it?
Here is my route :
Route::get('/registration/{course_id}',[
'uses'=>'AppController#getregistration',
'as'=>'registration'
]);
And Controller :
public function getregistration($course_id)
{
return view('index')->with('course_id',$course_id);
}
And first page this is how I send the value to first page:
<li> A </li>
Post Method
Route
Route::post('/registration',['uses'=>'AppController#getregistration','as'=>'registration']);
View
{!!Form::open(array('url' => '/registration')) !!}
{!! Form::hidden('course_id', '1') !!}
{!! Form::submit('registration') !!}
{!! Form::close() !!}
Controller
public function getregistration(Request $request)
{
$course_id = $request->input('course_id');
return view('index')->with('course_id',$course_id);
}
Get method
use encryption method, it will show encrypted id in url
View
<li> A </li>
Controller
public function getregistration($course_id)
{
$course_id = Crypt::decrypt($course_id);
return view('index')->with('course_id',$course_id);
}
here is no way you hide parameter in url, rather then you convert parameter value encrypt or hash is up to you,
other-way is save value in session first, then call the value from session without define parameter in url.
because laravel route only working to pattern of url /string /id, post get. dynamic value you must be writing / getting using pattern method.
Thanks.
You cannot hide a parameter in URL. If you don't want to show the ID then try using SLUG. I hope you understand what is a SLUG. If you don't then here it is. If you course title is My new Course Title then its slug would be my-new-course-title. And make sure it is unique like an ID in the table. It is also good for SEO, readable and looks good.

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

Dropdownlist in a edit.blade.php form

please how can I put dropdown list in an edit form with the old selected value on default?
here is my example:
<div class="form-group">
{{ Form::label('Container', 'Container:') }}
{{ Form::select('Select_cont', $containers) }}
</div>
I don´t know where to put the code of the old new value selected in my view and what should it be.
Please don´t forget that when directing to edit.blade.php I wrote thisin the function of my controller
return View::make('audio.edit',array($container))
->with('containers', $containers)
thanks a lot for your help :)
Selected value is the 3rd param of Form::select, so:
{{ Form::select('Select_cont', $containers, $selectedPreviouslyKey) }}
How to get that key depends on your code and what's in the dropdown
Pass it to the view like this
return View::make('audio.edit',array('containers' => $containers, 'selectedPreviouslyKey' => $selectedKey));
or use compact() / with etc
The View::make call isn't entirely right. It should be:
return View::make('audio.edit')->with('containers', $containers);
or
return View::make('audio.edit')->withContainers($containers);
or
return View::make('audio.edit', array('containers' => $containers));
or
return View::make('audio.edit', compact('containers'));
Also: make sure $containers exists.
The documentation on the select tag. http://laravel.com/docs/html#drop-down-lists

Resources