Vue.js v-for loop in laravel blade template - laravel-5

I'm unable to bind index and values when using v-for... however when I echo array it works...
blade file content:
<tr class="single-member" v-for='(member, index) in members' :index='index' >
#{{ index }}
<div class="field">
<div class="control">
<input class="input" type="text" placeholder="First name*" >
</div>
</div>
<div class="field">
<div class="control">
<input class="input" type="text" placeholder="Last name*" >
</div>
</div>
<div class="field">
<div class="control">
<input class="input" type="email" placeholder="Email*" >
</div>
</div>
<div class="field">
<div class="control">
<input class="input" type="number" placeholder="Phone">
</div>
</div>
</tr>
so this index is unable to get parsed and my members object is empty with fields "name, surname, email, phone"
NOTE that if I use #{{ members }} it will work, but when I want to use #{{ member }} or #{{ index }} that will not work, any ideas why is this happening?
an error that I'm getting is:
app.js:6103 [Vue warn]: Property or method "index" is not defined on the
instance but referenced during render. Make sure that this property is
reactive, either in the data option, or for class-based components, by
initializing the property. See:
https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
(found in Root)
data object
app.js file
data: function() {
return {
gdprChecked: false,
level: 'beginner',
teamName: '',
members: [],
formError: false,
formErrorMessage: '',
formSuccess: false,
formSuccessMessage: '',
}
},
mounted() {
// must be minimum 1 user
this.members.push({firstName: '', lastName: '', email: '', phone: ''});
},
working CODE, only blade file changed to this
<div class="columns" v-for="(member, index) in members">
<div class="column is-narrow">
#{{ index + 1 }}
</div>
<div class="column">
<div class="field">
<div class="control">
<input class="input" type="text" placeholder="{{trans('hackathon.firstName')}}" v-model="member.firstName">
</div>
</div>
<div class="field">
<div class="control">
<input class="input" type="text" placeholder="{{trans('hackathon.email')}}" v-model="member.email">
</div>
</div>
</div>
<div class="column">
<div class="field">
<div class="control">
<input class="input" type="text" placeholder="{{trans('hackathon.lastName')}}" v-model="member.lastName">
</div>
</div>
<div class="field">
<div class="control">
<input class="input" type="text" placeholder="{{trans('hackathon.phone')}}" v-model="member.phone">
</div>
</div>
</div>
</div>
still not sure why #1 not working and #2 does... any ideas?

you can follow this. it's worked for me
<tr v-for="(item,item_index) in purchase_items" :key="item_index">
td>#{{ item_index+1 }}</td>
<td>#{{ itemProduct(item.product_id) }}</td>
td>#{{ itemUnit(item.unit_id) }}</td>
<td>#{{ item.price }} * #{{ item.qty }}</td>
<td>#{{ item.amount }} </td>
<td>
<button v-on:click="removeItem(`#{{ item_index }}`)" class="btn
btn-danger btn-sm" > <i class="fa fa-trash-alt fa-1x"></i> </button>
</td>
</tr>

Try
<tr class="single-member" v-for='member in members' :key='member.id' >
this will refer to member.id
remember members will have to be an array of member
vue-list key

Related

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.

Laravel dont work method if i have 2 in 1 blade

I have 2 method in 1 blade, Delete and PUT, if i want delete it's work, but if I save info updated and I use method PUT, drop error: The DELETE method is not supported for this route. Supported methods: GET, HEAD, PUT., I use form collective composer for form.
SAVE Button not working, drop error, Remove button working good.
<div class="container-fluid mt--7">
<div class="row">
<div class="col-xl-12">
{!! Form::open(['action' => ['InvoiceController#update', $invoice->id], 'method' => 'POST']) !!}
{{csrf_field()}}
<div class="card bg-secondary shadow">
<div class="card-header bg-white border-0">
<div class="row align-items-center">
<div class="col-8">
<h3 class="mb-0">Edit {{$invoice->number}} invoice </h3>
</div>
<div class="col-4 text-right">
{{ Form::submit('Save', ['class' => 'btn btn-sm btn-primary']) }}
{{ Form::hidden('_method', 'PUT') }}
</div>
</div>
</div>
<div class="card-body">
<div class="pl-lg-4">
<div class="row">
<div class="col-lg-4">
<div class="form-group">
<label class="form-control-label">Company</label>
<select class="form-control form-control-alternative" name="company_id" required>
<option value="{{$invoice->company_id}}" selected>{{ $invoice->company_name->Name }}</option>
#foreach($companys as $company)
<option value="{{$company->id}}">{{ $company->Name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label class="form-control-label">Payment</label>
<select class="form-control form-control-alternative" name="payment" required>
<option value="{{$invoice->payment}}" selected>{{$invoice->payment}}</option>
<option value="Pending">Pending</option>
<option value="Paid">Paid</option>
<option value="Canceled">Canceled</option>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="form-group">
<label class="form-control-label">Invoices number</label>
<input class="form-control form-control-alternative" type="text" value="{{$invoice->number}}" disabled>
<input class="form-control form-control-alternative" name="number" placeholder="Auto generated" type="text" value="{{$invoice->number}}" hidden>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label class="form-control-label">Date</label>
<input class="form-control form-control-alternative datepicker-here" name="date" data-language='en' placeholder="Select date" type="text" value="{{$invoice->date}}">
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-control-label">Due date</label>
<input class="form-control form-control-alternative datepicker-here" name="due_date" data-language='en' placeholder="Select date" type="text" value="{{$invoice->due_date}}">
</div>
</div>
</div>
<hr class="my-4" />
<div class="row">
<div class="col-lg-12">
<table class="table table-bordered" id="dynamic_field">
<thead>
<tr>
<th scope="col">No.</th>
<th scope="col">Description</th>
<th scope="col">Qty</th>
<th scope="col">Price</th>
<th scope="col">Amount</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
#if(count($invoice_lists) > 0)
<?php $no = 1; ?>
#foreach( $invoice_lists as $invoice_list )
<tr>
<td>
{{ $no }}
<input type="text" name="list_id[]" value="{{ $invoice_list->id }}" hidden>
</td>
<td><input class="form-control form-control-alternative" type="text" name="description[]" value="{{$invoice_list->desc}}" placeholder="Description"></td>
<td><input class="form-control form-control-alternative qty" type="text" name="qty[]" value="{{$invoice_list->qty}}" placeholder="Qty"></td>
<td><input class="form-control form-control-alternative price" type="text" name="price[]" value="{{$invoice_list->price}}" placeholder="Price"></td>
<td><input class="form-control form-control-alternative amount" type="text" name="amount[]" value="{{$invoice_list->amount}}" placeholder="Amount"></td>
<td>
#if ($no == 1)
{!! Form::open(['action' => ['InvoiceController#remove', $invoice_list->id, $invoice->id], 'method' => 'POST']) !!}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Remove', ['class' => 'btn btn-danger btn-sm', 'disabled']) }}
{!! Form::close() !!}
#else
{!! Form::open(['action' => ['InvoiceController#remove', $invoice_list->id, $invoice->id], 'method' => 'POST']) !!}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Remove', ['class' => 'btn btn-danger btn-sm']) }}
{!! Form::close() !!}
#endif
</td>
<?php $no++; ?>
</tr>
#endforeach
#else
....
#endif
</tbody>
<thead>
<tr>
<th colspan="2"></th>
<th scope="col"><b>Sub Total</b></th>
<th scope="col"><b>VAT(21%)</b></th>
<th scope="col"><b>Grand Total</b></th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="2"></th>
<td scope="col">
<input class="form-control form-control-alternative sub" value="{{$invoice->sub}}" disabled>
<input class="form-control form-control-alternative subhidden" name="subhidden" value="{{$invoice->sub}}" hidden>
</td>
<td scope="col">
<input class="form-control form-control-alternative vat" value="{{$invoice->vat}}" disabled>
<input class="form-control form-control-alternative vathidden" name="vathidden" value="{{$invoice->vat}}" hidden>
</td>
<td scope="col">
<input class="form-control form-control-alternative grand" value="{{$invoice->grand}}" disabled>
<input class="form-control form-control-alternative grandhidden" name="grandhidden" value="{{$invoice->grand}}" hidden>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<hr class="my-4" />
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label class="form-control-label">Terms and Conditions</label>
<textarea class="form-control form-control-alternative" name="terms" rows="5" placeholder="Terms and Conditinios" required>{{$invoice->terms}}</textarea>
</div>
</div>
</div>
<input type="text" name="id" value="{{$invoice->id}}" hidden>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
You have multiple forms in 1 form, this is not actually good idea, but if it is critically required to have so, you have to give unique id attribute to each form and also set form="currentFormId" to the form's inputs

How can I fetch detail of each perticular ID?

In My list view I have all the details of leave. But when I click on details It will display me a Pop-up. In Pop up box it has to fetch and give me all the details of particular field but instead of that it always give me details of last inserted record
Here Is code of my list file
<table id="myTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>Employee Name</th>
<th>Leave Type</th>
<th>Leave Duration</th>
<th>Applied On</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if($allLeaves != null)
#foreach($allLeaves as $leave)
<tr>
<td> {{ $leave->username }} </td>
<td> {{ $leave->typeOfLeave }} </td>
<td>
{{ $leave->startDate }} To
{{ $leave->endDate }}
</td>
<td> {{ $leave->startDate }} </td>
<td>
#if($leave['status']=='Pending')
<span class="btn btn-warning">Pending</span>
#elseif($leave['status']=='Approved')
<span class="btn btn-success">Approved</span>
#else
<span class="btn btn-danger">Rejected</span>
#endif
</td>
<td>Details</td>
</tr>
</tbody>
</table>
and in same page i wrote code of fetch records
<form id="myform" class="form-horizontal" method="POST" action="{{ route('approve_leave', $leave->id) }}">
#csrf
<div class="card-body">
<div class="row">
<label class="col-md-6"><strong> Employee Name</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="emp_name" disabled value="{{$leave->username}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Leave Type</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="leavetype" disabled value="{{$leave->typeOfLeave}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Leave Duration</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="leaveduration" disabled value="{{ $leave->startDate }} To {{ $leave->endDate }}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Reason</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="reason" disabled value="{{$leave->reasonForLeave}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Applied on</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="appliedon" disabled value="{{$leave->startDate}}" style="border:none">
</div>
</div>
<br>
<div class="row">
<label class="col-md-6"><strong>Action</strong></label>
<div class="col-md-6">
<select class="form-control" id="status" name="status" value="{{$leave->status}}">
<option value="Pending" selected="selected">Pending</option>
<option value="Approved">Approved</option>
<option value="Rejected">Rejected</option>
</select>
</div>
</div>
#endforeach
<br>
<div class="row">
<label class="col-md-6"><strong>Reason For Action</strong></label>
<div class="col-md-6">
<input type="text" class="form-control" id="reason" name="reasonForAction" placeholder="Reason Of Action" style="border:none">
</div>
</div>
<br>
<div class="modal-footer">
<button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Save</button>
<button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
</div>
</div>
</form>
And This is the code i wrote in controller file
//Code Of list view
public function listOfLeave()
{
$allLeaves = LeaveManagement::all();
return view('pages.leavelist', compact('allLeaves'));
}
//Code of click on details button and fetch record of that particular id
public function handleLeave($id)
{
$leave = LeaveManagement::find($id);
return view('pages.leavelist', compact('leave', 'id'));
}
//code of approve reject leave and change the status of leave
public function approveLeave(Request $request ,$id)
{
$leave = LeaveManagement::find($id);
$leave->status = $request->get('status');
$leave->reasonForAction = $request->get('reasonForAction');
$leave->save();
return view('pages.leavelist');
}
If you want to show data according to user's id then you have to pass each field like data-fieldname and then you can fetch that field's data as shown in script.
Button:
<button type="button"
class="btn btn-xs btn-default confirm-modal"
data-toggle="modal"
data-target="#model"
data-id="{{ $singleRecord->id }}"
data-name="{{ $singleRecord->full_name }}"
data-leave_reason="{{ $singleRecord->leave_reason }}"
data-from_date="{{ date('d-F-Y', strtotime($singleRecord->from_date)) }}"
data-to_date="{{ date('d-F-Y', strtotime($singleRecord->to_date)) }}"
>Details </button>
And add script
<script type="text/javascript">
$('.confirm-modal').click(function (event) {
$('.employee_name').text($(this).data('name'));
$('#from_date').text($(this).data('from_date'));
$('#to_date').text($(this).data('to_date'));
$('#total_leaves').text($(this).data('total_leaves'));
$('.leave_reason').text($(this).data('leave_reason'));
})
</script>
In model div, add each id to display that data.
<div class="col-md-12">
<label for="to_date">To Date:</label>
<span id="to_date"></span>
Hope this helps :)
Edit your modal button and modal id like below,
<td>Details</td>
and your modal,
<div id="details_{{ $leave->id }}" class="modal fade edit-form" role="dialog">
or you can use javascript to populate your field,
<button type="button" class="btn btn-xs btn-default confirm-modal" data-toggle="modal" data-target="#details"
data-leave="{{ $leave }}">Details </button>
<script type="text/javascript">
$(document).on('click', '.confirm-modal', function(e){
var data = $(this).data('leave');
$('#emp_name').text(data.username);
$('#from_date').text(data.from_date');
$('#to_date').text(data.to_date);
$('#total_leaves').text(data.total_leaves);
$('#leave_reason').text(data.leave_reason);
})
</script>

Vue.js - Buefy Modules (Radio btns/ Check Box)

I'm practising back-end in Laravel, and for front-end I'm using Vue.js - Buefy Modules, and I have little problem with Radio and CheckBox.
Here User should choose one of the two offered radio btns:
<div class="block">
<b-radio-group v-model="permissionType">
<b-radio name="permission_type" value="basic">Basic Permission</b-radio>
<b-radio name="permission_type" value="crud">CRUD Permission</b-radio>
</b-radio-group>
</div>
If User click on first btun (Basic) there should appear 3 input fields:
<div class="field" v-if="permissionType == 'basic'">
<label for="display_name" class="label">Name (Display Name)</label>
<p class="control">
<input type="text" class="input" name="display_name" id="display_name">
</p>
</div>
<div class="field" v-if="permissionType == 'basic'">
<label for="name" class="label">Slug</label>
<p class="control">
<input type="text" class="input" name="name" id="name">
</p>
</div>
<div class="field" v-if="permissionType == 'basic'">
<label for="description" class="label">Description</label>
<p class="control">
<input type="text" class="input" name="description" id="description" placeholder="Describe what this permission does">
</p>
</div>
If User click on second btn (CRUD) there should appear 1 input, 4 check box btns and table, but they do not appear.
<div class="field" v-if="permissionType == 'crud'">
<label for="resource" class="label">Resource</label>
<p class="control">
<input type="text" class="input" name="resource" id="resource" v-model="resource" placeholder="The name of the resource">
</p>
</div>
<div class="columns" v-if="permissionType == 'crud'">
<div class="column is-one-quarter">
<b-checkbox-group v-model="crudSelected">
<div class="field">
<b-checkbox custom-value="create">Create</b-checkbox>
</div>
<div class="field">
<b-checkbox custom-value="read">Read</b-checkbox>
</div>
<div class="field">
<b-checkbox custom-value="update">Update</b-checkbox>
</div>
<div class="field">
<b-checkbox custom-value="delete">Delete</b-checkbox>
</div>
</b-checkbox-group>
</div> <!-- end of .column -->
<input type="hidden" name="crud_selected" :value="crudSelected">
<div class="column">
<table class="table" v-if="resource.length >= 3">
<thead>
<th>Name</th>
<th>Slug</th>
<th>Description</th>
</thead>
<tbody>
<tr v-for="item in crudSelected">
<td v-text="crudName(item)"></td>
<td v-text="crudSlug(item)"></td>
<td v-text="crudDescription(item)"></td>
</tr>
</tbody>
</table>
</div>
</div>
I've checked Buefy documentation and there were some updates, but when I change code, still not works..
Here is script:
<script>
var app = new Vue({
el: '#app',
data: {
permissionType: 'basic',
resource: '',
crudSelected: ['create', 'read', 'update', 'delete']
},
methods: {
crudName: function(item) {
return item.substr(0,1).toUpperCase() + item.substr(1) + " " + app.resource.substr(0,1).toUpperCase() + app.resource.substr(1);
},
crudSlug: function(item) {
return item.toLowerCase() + "-" + app.resource.toLowerCase();
},
crudDescription: function(item) {
return "Allow a User to " + item.toUpperCase() + " a " + app.resource.substr(0,1).toUpperCase() + app.resource.substr(1);
}
}
});
</script>
Here I place original code without changes, if someone could fix this, I would be grateful. Thanks!
you forget to add v-model to your radio group, try this and it should work
<div class="block">
<b-radio-group v-model="permissionType">
<b-radio v-model="permissionType" name="permission_type" native-value="basic">Basic Permission</b-radio>
<b-radio v-model="permissionType" name="permission_type" native-value="crud">CRUD Permission</b-radio>
</b-radio-group>

How to dynamically populate checkbox from database

I want to populate the view.blade.php page with the value given in add.blade.php. I am getting my form values using a variable $form_value from the database. How can I use this variable to populate the chekbox in view page.
view.blade.php
<div class = "row">
<div class="col-lg-4">
<label>
<input type="checkbox" name="hoogwerker"><b> Hoogwerker gebruikt</b>
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="koerier"><b> Materiaal toegeleverd via koerier</b>
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="bijgewerkt"><b> Kraanboek bijgewerkt</b>
</label>
</div>
</div>
add.blade.php
<div class = "row">
<div class="col-lg-4">
<label>
<input type="checkbox" name="hoogwerker"> Hoogwerker gebruikt
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="koerier"> Materiaal toegeleverd via koerier
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="bijgewerkt"> Kraanboek bijgewerkt
</label>
</div>
</div>
Laravel will do it automatically for you if your blade template is done using Form::model():
{{ Form::model($model) }}
{{ Form::checkbox('hoogwerker') }}
{{ Form::label('hoogwerker', 'Hoogwerker gebruikt') }}
{{ Form::checkbox('koerier') }}
{{ Form::label('koerier', 'Materiaal toegeleverd via koerier') }}
{{ Form::close() }}
And your controller should do something like
$model = User::find(1);
return View::make('your-view')->with('model', $model);

Resources