bootstrap-vue not passing array in checkbox - laravel-5

I'm using bootstrap-vue in my laravel project to pass data from the view to the database with checkboxes (b-form-checkbox), i want to select from the permissions passed from the database and assign it to a role, which means a role can have more than one permission, unfortunately the data is not persisting to the database as an array because if i select more than one in the checkbox it only shows the first one clicked. Please i need help as i have spent too much time on this issue. This is my code:
edit.blade.php
#extends('layouts.master')
#section('content')
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Admin
<small>Edit</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Role</li>
<li class="active">Edit</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
#include('layouts.partials.message')
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header with-border">
<h3 class="boxtitle">Edit Role</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<form action="{{ route('role.update', ['id' => $role->id]) }}" enctype="multipart/form-data" method="post" accept-charset="utf-8">
{{csrf_field()}}
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="display_name" value="{{ old('display_name', $role->display_name) }}" class="form-control" placeholder="Name (Human Readable)" required>
<span class="help-block text-red">
#if($errors->has('display_name'))
{{ $errors->first('display_name')}}
#endif
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="name" value="{{ old('name', $role->name) }}" class="form-control" placeholder="Slug (can not be edited)" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="description" value="{{ old('description', $role->description) }}" class="form-control" placeholder="Role Description">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Permissions:</h2>
<b-form-group>
<b-form-checkbox-group v-model="permissionsSelected">
#foreach ($permissions as $permission)
<div class="form-group">
<b-form-checkbox id="permissions" name="permissions" value="{{ $permission->id }}">
<div class="form-group">
{{ $permission->display_name }} <em> ({{ $permission->description }})</em>
</div>
</b-form-checkbox>
</div>
#endforeach
</div>
</div>
<hr>
<div class="row">
<div class="col-md-1">
<div class="form-group">
<button class="btn btn-primary" type="submit" id="submit">
<i class="fa fa-check"></i> Submit
</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
</div>
<div class="col-md-11">
<div class="form-group">
<div class="checkbox">
<label>
<input name="redirect" type="checkbox" checked> Redirect to role list after submission
</label>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</form>
</div>
</div>
</div>
</div>
</section>
#endsection
#section('vue')
<script>
var app = new Vue ({
el: '#app',
data: {
permissionsSelected: {!!$role->permissions->pluck('id')!!}
}
});
</script>
#endsection

Related

Showing Not Acceptable 406 error in laravel

I am getting a new error and its first time I am facing such error. Whenever I submit form and if it contains '%' in any field, then it shows me the 406 Not Acceptable error.
Also, when I submit the form and any field didn't contain '%' then form is successfully submitted.
I have already tried adding
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
in database.php but it didn't work.
Also, I have found in R&D that by adding 'mysql_real_escape_string' in PHP,, this can be resolved. But ho it can be used in laravel..
Please help me out,
Thanks in advance.
<div id="page-wrapper">
<div id="page-inner">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
#include('admin.partials.flash_message')
<form action="{{ $action }}" method="post" enctype="multipart/form-data">
#csrf()
<?php /*#if(!empty($method)) #method($method) #endif*/ ?>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>RFP Title <span class="text-danger">*</span> :</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="rfp_title" value="{{ $pageTitle }}" {{ $prop }} maxlength="100">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>RFP Description <span class="text-danger">*</span> :</label>
</div>
<div class="col-md-9">
<textarea id="summernote" name="rfp_description"></textarea>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Organisation <span class="text-danger">*</span> :</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="organisation" value="{{ $pageTitle }}" {{ $prop }} maxlength="100">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Publish Date <span class="text-danger">*</span> :</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="publish_date" value="{{ $pageTitle }}" {{ $prop }} maxlength="100" readonly>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Closing Date <span class="text-danger">*</span> :</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="closing_date" value="{{ $pageTitle }}" {{ $prop }} maxlength="100" readonly>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Company Image <span class="text-danger">*</span> :</label>
</div>
<div class="col-md-9">
<input type="file" name="company_image" id="imageUpload" accept=".png,.jpg,.jpeg" class="hide">
<label for="imageUpload" class="upload-poster mr-5">Select file</label> Max Size 2 MB<br>
<img src="{{ asset('public/assets/admin/images/dummy-logo.jpg')}}" id="imagePreview" class="organisation-logo" alt="Your image will appear here.">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Attachment <span class="text-danger">*</span> :</label>
</div>
<div class="col-md-9">
<input type="file" name="attachment" id="file-2" class="inputfile" data-multiple-caption="{count} files selected" accept=".pdf,.doc,.docx" multiple>
<label for="file-2" class="mr-5"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg> <span>Select File</span></label> Max Size 2 MB
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label>Contact Person :</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control" name="contact_person" value="{{ $pageTitle }}" {{ $prop }} maxlength="100">
</div>
</div>
</div>
<div class="form-group" style="margin-top: 20px;">
<input type="submit" class="btn btn-primary" value="Submit" name="btn_btn_add_product" {{ $prop }}>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>

How to fetch data from database related to key using Laravel?

I am a beginner, and I want to show the value data into the input field related to the key but I am very confused that how can I show the value data into the input field so please if you have an idea please help me thanks.
Database table
Setting table https://ibb.co/jGFX4t2
I want to show a value data in this field, please see https://ibb.co/Mh08c9b
Settings Model
class Settings extends Model
{
protected $table="setting";
protected $fillable =['id','key','value'];
}
Controller
public function setting()
{
$setting=Settings::all();
return view('admin.setting.setting',compact('setting'));
}
HTML view
<form method="post" action="{{route('update.setting')}}" enctype="multipart/form-data" >
#csrf
<div class="card-box">
<div class="panel panel-heading">
<h3>Update Settings</h3>
</div>
<div class="col-lg-5">
<div class="mt-3">
<input type="file" name="logo_image" class="dropify" />
</div>
</div>
<div class="row">
<div class="col-lg-5 mt-3">
<div class="group-form">
<label>Contact Number*</label>
<input type="text" name="contact_number" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-5 mt-3">
<div class="group-form">
<label>Contact Email *</label>
<input type="email" name="email" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-10 mt-3">
<div class="group-form">
<label>Location *</label>
<input type="text" name="location" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-5 mt-3">
<h3> Social Links:</h3>
<div class="group-form">
<label>Facebook *</label>
<input type="text" name="facebook" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-5 mt-5">
<div class="group-form">
<label>Twitter *</label>
<input type="text" name="twitter" value="{{}}" class="form-control" >
</div>
</div>
<div class="col-lg-6 mt-3">
<div class="group-form">
<label>Linkedin *</label>
<input type="text" name="linkedin" value="{{}}" class="form-control" >
</div>
</div>
<div class="col-lg-7 mt-3">
<div class="group-form ">
<button type="submit" id="btnsubmit" class="btn btn-danger waves-effect waves-light col-lg-2">Save</button>
</div>
</div>
</div>
</div> <!-- end card-box -->
</form>
please use this code.
{{ $setting->Where('key', 'Contact_Email')->first()->value }}
How about something like:
{{ $setting->firstWhere('key', 'Contact_Number')->value; }}
{{ $setting->firstWhere('key', 'Contact_Email')->value; }}
{{ $setting->firstWhere('key', 'Location')->value; }}
etc
When you use Eloquent Model::all(), it returns a collection to the blade view.
Therefore you can use firstWhere to find the key value.

Web pages gets distorted when ever I try to specify a variable on my route

I tried parsing a variable through my route and but the CSS isn't rendered on the page and my page shows purely html=
Here is my web route;
Route::get('/create-transaction/{id}', 'AdminController#create')->name('Create Transaction');
And here is my controller;
public function create($id){
$users = User::orderBy('id', 'desc')->paginate(100);
return view('create-transaction')->with(compact('users', 'id'));
}
I get this sam error with every other web page on my website when. once I parse in a variable through my route, the whole page losses it's formatting. I've tried the same page without parsing variables and it works fine but it's a different case when I parse in a variable
Here is my view page;
#extends('layouts.admin')
#section('content')
<section class="content-top-margin page-title-small border-bottom-light border-top-light">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12 wow fadeInUp" data-wow-duration="300ms">
<!-- page title -->
<h1 class="black-text">Create Transaction</h1>
<!-- end page title -->
</div>
<div class="col-md-6 col-sm-12 breadcrumb text-uppercase wow fadeInUp" data-wow-duration="600ms">
<!-- breadcrumb -->
<ul>
<li>Dashboard</li>
<li>Withdrawals</li>
<li>Change Password</li>
</ul>
<!-- end breadcrumb -->
</div>
</div>
</div>
</section>
<section class="wow fadeIn border-top">
<div class="container">
<div class="row">
<div class="col-md-5 col-sm-10 center-col">
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<form action="/create-transaction" method="POST">
#csrf
<div class="form-group">
<div class="col-md-6">
<label for="exampleInputEmail1">Date</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
<div class="col-md-6">
<label for="exampleInputEmail1">Amount</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="exampleInputEmail1">Type</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
<div class="col-md-9">
<label for="exampleInputEmail1">Description</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
</div>
<button class="btn btn-black btn-small btn-round no-margin-bottom" type="submit">Create Transaction</button>
<!-- end button -->
</form>
</div>
</div>
</div>
</section>
#endsection

Uploading files in Laravel 5

I'm trying to upload a file with Laravel but it does not work
I will send the field with the meta name view
<div class="panel-body">
<div class="tab-content">
<div class="tab-pane fade in active" id="tab1success">
<form method="post" name="type" action="songs">
{{ csrf_field() }}
<div class="form-group">
<div class="row">
<label class="col-md-3 control-label" for="Name">نام موزیک</label>
<div class="col-md-7">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-music">
</i>
</div>
<input id="Name" name="title" type="text"
placeholder="نام موزیک" class="form-control input-md">
</div>
</div>
</div>
<br>
<div class="row">
<label class="col-md-3 control-label" for="Name (Full name)">دسته
بندی</label>
<div class="col-md-7">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-list">
</i>
</div>
<select id="Name" name="category" type="دسته بندی"
placeholder="دسته بندی" class="form-control input-md">
<option>fun</option>
<option>love</option>
<option>birth</option>
<option>wedding</option>
</select>
</div>
</div>
</div>
<br>
<div class="row">
<label class="col-md-3 control-label" for="Name (Full name)">توضیحات</label>
<div class="col-md-7">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-file-text-o">
</i>
</div>
<textarea id="Name" name="text" placeholder="توضیحات"
class="form-control input-md"></textarea>
</div>
</div>
</div>
<br>
<div class="row">
<label class="col-md-3 control-label" for="Name (Full name)">انتخاب
فایل</label>
<div class="col-md-7">
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-folder-o">
</i>
</div>
<input id="meta" name="meta" type="file" placeholder="انتخاب فایل"
class="form-control input-md">
</div>
</div>
</div>
<br>
<div class="row" style="text-align: center">
<div class="col-md-10 " style="text-align: center">
<button id="Name" name="submit" type="submit" placeholder="انتخاب فایل"
class="form-control input-md" style="text-align: center">اضافه
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
Controller
public function create(Request $req, $type) {
$this->authorize('is_admin');
$req->file('meta')->store('app');
// $path = Storage::putFile('app', $req->file('meta'))
}
error
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function store() on null
Here is an image of the error
Does anybody know what i should do about this?
Add enctype="multipart/form-data" to your <form> tag in your blade:
<form method="post" name="type" action="songs" enctype="multipart/form-data">
Based on your blade file you can see that the file meta being uploaded is not required, so in your controller, you need to check if the file was sent before calling the store method on it. Check this:
public function create(Request $req, $type) {
$this->authorize('is_admin');
$file = $req->file('meta') ? $req->file('meta')->store('app') : null;
}

Update method laravel using modal box

My update method is working unfortunately it always updates the wrong data in this case it always updates the last data in my db list. I believe this occurs because my modal box directs to $user->id which always points to the last id as I have a for loop used at the top, is there a ways I could do to point it to the selected id instead?
view.blade.php
<div class="well col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
#foreach ($users as $user)
<div class="row user-row">
<!--div class="col-xs-3 col-sm-2 col-md-1 col-lg-1">
<img class="img-circle"
src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=50"
alt="User Pic">
</div-->
<div class="col-xs-2 col-sm-3 col-md-4 col-lg-4">
<h5 style="font-weight: bold">{{ $user->name }}</h5>
</div>
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 dropdown-user" data-for=".{{ $user->id }}">
<h5 class="glyphicon glyphicon-chevron-down text-muted pull-right"> </h5>
</div>
</div>
<div class="row user-infos {{ $user->id }}">
<div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">User Information</h2>
</div>
<div class="panel-body">
<div class="row">
<div class=" col-md-10 col-lg-10 hidden-xs hidden-sm">
<div class="col-xs-5">User level:</div><div class="col-xs-5"> {{ $user->role->role_description }}</div>
<div class="col-xs-5">Email:</div> <div class="col-xs-5"> {{ $user->email }}</div>
<div class="col-xs-5">Phone number: </div> <div class="col-xs-5"> {{ $user->mobile }} </div>
<div class="col-xs-5">Office extension: </div> <div class="col-xs-5"> [ TO IMPLEMENT ]</div>
</div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-sm btn-warning" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
<span class="pull-right">
<button class="btn btn-sm btn-danger" type="button">Inactive <i class="glyphicon glyphicon-remove"></i></button>
</span>
</div>
</div>
</div>
</div>
#endforeach
</div>
#if(Session::has('flash_message'))
<div class="alert alert-success col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
{{ Session::get('flash_message') }}
</div>
#endif
<div class="col-sm-offset-1 col-sm-2">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<!-- Modal -->
<div id="form" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User Information</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-3" for="name">Username:</label>
<div class="col-sm-5 #if ($errors->has('name')) has-error #endif">
<input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
</div>
...
Your modal is referencing the $user object, but it is outside of your foreach loop.
Specifically this line:
<form class="form-horizontal" role="form" action="/manage_accounts/{{ $user->id }}" novalidate>
You could register an onClick event for the modal pop-up button, that grabs a hidden input field of an user's id and dynamically updating the the action URL. Alternatively, you could just have the action URL be the same and handle the logic server side. This approach would have a hidden input field for the user ID that you would be updating, but is a lot cleaner that dealing with URL structure.
Edit:
Javascript Example:
<script type="text-javascript">
$(function() {
$('.btn--edit').on('click', function() {
var formAction = $('.form-horizontal').attr('action').replace(/(?!.*/).*, '');
var userId = $(this).closest('[name=user_id]').val();
$('.form-horizontal').attr('action', formAction + '/' + userId);
});
});
</script>
This requires you to update your modal button with a class name of .btn--edit
<button class="btn btn-sm btn-warning btn--edit" type="button" data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
This also requires you add a hidden input within the foreach somewhere. I chose after the user-infos class.
<input type="hidden" name="user_id" value="{{ $user->id }}" />
So here's how I got my update method to work.
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/" novalidate>
<input type="hidden" name="_method" value="PUT">
Javascript:
<script type="text/javascript">
$(function() {
$('.btn--edit').on('click', function(e) {
var userId = $(this).attr('data-for');
var formAction = "/manage_accounts/" + userId;
$('.form-horizontal').attr('action', formAction);
});
</script>
The modal button with class name of .btn--edit,
<button class="btn btn-sm btn-warning btn--edit" type="button"
data-toggle="modal" data-target="#update" data-for="{{$user->id}}">Edit <i class="glyphicon glyphicon-edit"></i></button>

Resources