how to use variable to display date in datepicker HTML? - laravel

I want to make edit operation using variable. I am trying fetch date into html bootstrap datepicker.
Herein my code where should put my fetching variable?
<div class="form-group col-sm-4">
{!! Form::label('tradedate', 'Traded date:') !!}
<div class=" input-append date form_datetime" id="datetimepicker" data-date="{{date('Y-m-d H:i:s')}}" data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1">
<input size="16" type="text" onfocusout = 'all_function()' value="" readonly class="form-control">
<span class="add-on"><i class="icon-remove"></i></span>
<span class="add-on"><i class="icon-th"></i></span>
</div>
<input type="hidden" id="dtp_input1" value="" name="tradedate" />
</div>
public function edit($id)
{
$trade = Trade::findOrFail($id);
return view('member.add-single-trade.edit', compact('trade'));
}
$trade has date which i want to edit it for datepicker.

Since you have passed compact('trade') into your blade template you can simply use {{ $trade }} in your template. I am not sure what you're date column is called so I use the created_at timestamp as an example.
{{ $trade->created_at }}
So modifying your code to what I think you are wanting:
<div class="form-group col-sm-4">
{!! Form::label('tradedate', 'Traded date:') !!}
<div class=" input-append date form_datetime" id="datetimepicker" data-date="{{date('Y-m-d H:i:s')}}" data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1">
<input size="16" type="text" onfocusout = 'all_function()' value="{{ $trade->created_at }}" readonly class="form-control">
<span class="add-on"><i class="icon-remove"></i></span>
<span class="add-on"><i class="icon-th"></i></span>
</div>
<input type="hidden" id="dtp_input1" value="" name="tradedate" />
</div>

Related

Error "The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST. " update method

Well hello there, I am trying to update a register of my table assistants, but when I push the button submit of the form, this error appear, I am using a table pivot between assistants and events, but i am only try to edit a assistant.This is the error
This is my code AssistantController.php file and the methods edit and update
edit and update methods
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
This is my form to assistants
input form assistants
#csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
This is my edit page
#extends('layouts.back')
#section('title','Edit assistants')
#section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
#method('PATCH')
#include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
Cancel
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
#endsection
There are my routes
routes
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
I am beginner.
thank you all.
In your edit page, change your form action from
<form action="/assistants" method="POST">
to
<form action="/assistants/{{ $assistant->id }}" method="POST">
Without the assistant id in the action field, Laravel thinks you are trying to update the base assistants route, which is an invalid action for that route.

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.

how to break a second foreach loop

Actually in this code I tried to show the form label and form input field come dynamic from the database.
In this first foreach loop contains a label
Second, foreach contain the input fields data.
If the second foreach have no data, both foreach not work what should I do?
#section('body')
<!--{{session('uid')}} -->
<div class="col-sm-12">
<!-- Wizard container -->
<div class="wizard-container">
<div class="card wizard-card" data-color="red" id="wizard">
<form action="{{URL::to('faculty/submitreport')}}" method="post" name="reportform">
<!-- You can switch " data-color="blue" " with one of the next bright colors: "green", "orange", "red", "purple" -->
{{ csrf_field()}}
<div class="wizard-header">
<h3 class="wizard-title">
Monthly Progress Report
</h3>
<h5>This information will let us know more about you.</h5>
</div>
#foreach($fetchform as $data)
#foreach($fetchans as $ans)
#if(is_null($ans)){
#break
<div class="wizard-navigation">
<ul>
<li>{{$data->f_title}}</li>
</ul>
</div>
<div class="tab-content">
<div class="tab-pane" id="details">
<div class="row">
<div class="col-lg-12">
#if(is_null($data->f1))
#else
<div class="form-group label-floating"> <label class="control-label">{{$data->f1}}</label>
<input name="f1" type="text" class="form-control"value='{{$ans->f1}}'></input>
</div>
#endif
#if(is_null($data->f2))
#else
<div class="form-group label-floating">
<label class="control-label">{{$data->f2}}</label>
<input name="f2" type="text" class="form-control" value='{{$ans->f2}}'></input>
</div>
#endif
#if(is_null($data->f3))
#else
<div class="form-group label-floating">
<label class="control-label">{{$data->f3}}</label>
<input name="f3" type="text" class="form-control" value='{{$ans->f3}}'></input>
</div>
#endif
#if(is_null($data->f4))
#else
<div class="form-group label-floating">
<label class="control-label">{{$data->f4}}</label>
<input name="f4" type="text" class="form-control" value='{{$ans->f4}}'></input>
</div>
#endif
#if(is_null($data->f5))
#else
<div class="form-group label-floating">
<label class="control-label">{{$data->f5}}</label>
<input name="f5" type="text" class="form-control" value='{{$ans->f5}}'></input>
</div>
#endif
#if(is_null($data->f6))
#else
<div class="form-group label-floating">
<label class="control-label">{{$data->f6}}</label>
<input name="f6" type="text" class="form-control" value='{{$ans->f6}}'></input>
</div>
#endif
#if(is_null($data->f7))
#else
<div class="form-group label-floating">
<label class="control-label">{{$data->f7}}</label>
<input name="f7" type="text" class="form-control" value='{{$ans->f7}}'></input>
</div>
#endif
#if(is_null($data->f8))
#else
<div class="form-group label-floating">
<label class="control-label">{{$data->f8}}</label>
<input name="f8" type="text" class="form-control" value='{{$ans->f8}}'></input>
</div>
#endif
#if(is_null($data->f9))
#else
<div class="form-group label-floating">
<b> <label class="control-label">{{$data->f9}}</label></b>
<input name="f9" type="text" class="form-control" value='{{$ans->f9}}'></input>
</div>
#endif
#if(is_null($data->f10))
#else
<div class="form-group label-floating">
<b> <label class="control-label">{{$data->f10}}</label></b>
<input name="f10" type="text" class="form-control" value='{{$ans->f10}}'></input>
</div>
#endif
#if(is_null($data->f11))
#else
<div class="form-group label-floating">
<b> <label class="control-label">{{$data->f11}}</label> </b>
<input name="f11" type="text" class="form-control" value='{{$ans->f11}}'></input>
</div>
#endif
#if(is_null($data->f12))
#else
<div class="form-group label-floating">
<b> <label class="control-label">{{$data->f12}}</label></b>
<input name="f12" type="text" class="form-control" value='{{$ans->f12}}'></input>
</div>
#endif
<input type='hidden' name="f_id" value='{{$data->f_id}}'>
<input type='hidden' name="u_id" value='{{session()->get("uid")}}'>
<input type='hidden' name="f_status" id="f_status" />
</div>
}
#endif
#endforeach
#endforeach
</div>
</div>
</div>
</div>
<div class="wizard-footer">
<div class="pull-right">
<input type='submit' class='btn btn-next btn-fill btn-danger btn-wd-lg' name='save' value='Save' />
<input type='button' class='btn btn-finish btn-fill btn-danger btn-wd-lg' onclick="myFunction()" name='Submit' value='submit' />
</div>
<div class="clearfix"></div>
</div>
</form>
</div>
</div> <!-- wizard container -->
</div>
<script>
function myFunction() {
var txt;
var r = confirm("Really Do You Want To Finally Submit a Form \n After Submitting You Can't Update Anything into It.");
if (r == true) {
document.getElementById("f_status").value = '1';
reportform.submit();
} else {
}
}
</script>
#endsection
From reading your code, it looks like you are trying to fill the label elements with the values of the $data variable and the input fields with the value of the $ans variable. As the code is written right now, you are looping through all the items in $fetchform and each time you loop you go through all the values of $fetchans. So if you had 3 form items and 3 answers you would actually run 9 loops. You're getting every possible combination of values.
It seems more likely that each fetchform has a corresponding fetchans. If this is the case, continuing with the example above, you would only want run 3 loops. If I were to write this code, I would have it look something like this:
//loop through all items in $fetchform
#for ($i = 0; $i < count($fetchform); $i++)
//php tags needed in blade to set the variables
#php
$data = $fetchform[$i]; //get the form data
$ans = $fetchans[$i]; //get the corresponding form answer
#endphp
//it might be more appropriate to check these for 'empty' instead of null
//that all depends on your code
#if(!is_null($data) || !is_null($ans))
//make sure object has the 'f1' key and value is not null
#if(array_key_exists('f1', $data) && !is_null($data->f1))
<div class="form-group label-floating">
<label class="control-label">{{ $data->f2 }}</label>
<input name="f2" type="text" class="form-control" value='{{ $ans->f2 }}' />
</div>
#endif
#endif
#endfor
Keep in mind that the code above makes a few assumptions about your question. First, it assumes that $fetchform and $fetchans are numerical arrays that can be accessed by the $i value. Second, it assumes that $fetchform and $fetchans have corresponding key values, so the data and answers are both at the same numerical key in their respective arrays. Finally, it assumes that the values, when not present will be null versus being empty, which I would think is more likely the case. You may have to modify the code I provided to fit your exact needs. Play with it and see if it works.

Edit db record with modal window

I'm trying to edit some database record based on an ID that I'm saving into a button value.
#foreach ($employment as $empl)
<button data-toggle="modal" data-target="#edit-empl" href="#edit-empl" class="btn btn-default editbtn-modal" value="{{ $empl->id }}" type="button" name="editbtn">Edit</button>
<h3 class="profile-subtitle">{{ $empl->company }}</h3>
<p class="profile-text subtitle-desc">{{ $empl->parseDate($empl->from) }} - {{ $empl->parseDate($empl->to) }}</p>
#endforeach
As you can see here, I have an edit button with an id attached.
When I click edit I open a modal window to edit the fields and later on submit the form.
The thing is, I'm not sure how to get that id from the button into the modal window so I can compare the values and display the correct fields..
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="">
#foreach ($employment as $empl)
#if ($empl->id == buttonidhere)
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
#endif
#endforeach
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
I was able to pass the button value into the modal using javascript.. I put it into a hidden input but that doesn't help me at all because I can't get the input value in order to compare the values..
Solution 1: Using ajax
Step 1: Create a route in laravel which will return a JSON object containing employing data of requested employee.
For e.g,
/profile/employment/data/{empl_id}
Will get you employement data of id empl_id.
Step 2: Change your form as below
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
<input class="editID" type="hidden" name="editID" value="">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 3: Use javascript(jQuery) to get the data using ajax and load it into the form in modal.
jQuery code:
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$.ajax({
url: url,
method: "get"
}).done(function(response) {
//Setting input values
$("input[name='editID']").val(id);
$("input[name='company']").val(response.company);
$("input[name='to']").val(response.to);
$("input[name='from']").val(response.from);
//Setting submit url
$("modal-form").attr("action","/profile/employment/edit/"+id)
});
});
Solution 2: Using remote modal
Step 1:
Create another blade file for eg. editEmployee.blade.php and add the above form in it.
<form class="app-form" id="modal-form" action="/profile/employment/edit/{{ $empl->id }}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="{{ $empl->id }}">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 2: Create a controller which would return the above form as HTML.
Tip: use render() function. example
Step 3: load the form into modal window before showing using javascript(jQuery)
considering your modal id is "emp-modal"
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$('#emp-modal').modal('show').find('.modal-body').load(url);
});
One solution would be to send the details you want the same way you send the id to the modal.
and the proper way to send variables to modal is to include this in the button that opens modal:
data-variablename="{{$your-variable}}"
use this jQuery to get the values of your variables to modal. where edit-empl is the id of your modal and data-target of your button
$('#edit-empl').on('show.bs.modal',function (e) {
var variablename= $(e.relatedTarget).data('variablename');
$(e.currentTarget).find('input[id="yourinputID"]').val(variablename);

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