Laravel Crop Image Before Upload - php

I'm new to Jquery. I'm building a project with Laravel 7.28. I want users to crop an image before upload. I've searched on the net but all examples upload the image right after cropping. But I want the user to crop but not upload. I want users to upload the cropped image with input fields. Here is my form and script code:
#section('content')
<div class="row">
<div class="col-lg-12 col-12">
<!-- Basic Forms -->
<div class="box">
<div class="box-header with-border">
<h4 class="box-title">Quick Offer Creation</h4>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="card-text">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
{!! Form::open(['route'=>['quick_offer.store'], 'method' => 'POST', 'class'=> 'form', 'files' => true ]) !!}
<div class="row">
<div class="col-6">
<div class="form-group row col-md-12">
<label for="project_id" class="col-sm-2 col-form-label">Project</label>
<div class="col-sm-10">
<select name="project_id" id="" class="form-control select2">
<option value="">Please Select</option>
#foreach($projects as $project)
<option
value="{{ $project->id }}" {{ old('project_id') == $project->id ? 'selected' : '' }}>{{ $project->code . ', ' . $project->title}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row col-md-12">
<label for="bedroom" class="col-sm-2 col-form-label">Bedroom</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="bedroom" name="bedroom"
value="{{ old('bedroom') }}" min="1">
</div>
</div>
<div class="form-group row col-md-12">
<label for="location" class="col-sm-2 col-form-label">Location</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="location" name="location"
value="{{ old('location') }}">
</div>
</div>
<div class="form-group row col-md-12">
<label for="status" class="col-sm-2 col-md-2 col-form-label">Status</label>
<div class="col-sm-10 col-md-10">
<select name="status" id="" class="form-control">
<option value="">Please Select</option>
<option value="publish" {{ old('status') == 'publish' ? 'selected' : '' }}>
Publish
</option>
<option value="draft" {{ old('status') == 'draft' ? 'selected' : '' }}>
Draft
</option>
<option value="review" {{ old('status') == 'review' ? 'selected' : '' }}>
Review
</option>
<option value="unpublish" {{ old('status') == 'unpublish' ? 'selected' : '' }}>
Unpublish
</option>
</select>
</div>
</div>
<div class="form-group row col-md-12">
<label for="photo" class="col-sm-2 col-form-label">
Image
<br>
<small>The photo must be square</small>
</label>
<div class="col-sm-10">
<input type="file" name="photo" class="image form-control" value="{{ old('photo') }}">
</div>
</div>
</div>
<div class="col-6">
<div class="form-group row col-md-12">
<label for="currency" class="col-sm-2 col-md-2 col-form-label">Currency</label>
<div class="col-sm-10 col-md-10">
<select name="currency" id="" class="form-control">
<option value="">Please Select</option>
<option value="TRY" {{ old('currency') == 'TRY' ? 'selected' : '' }}>
(₺) Turkish Lira
</option>
<option value="EUR" {{ old('currency') == 'EUR' ? 'selected' : '' }}>
(€) Euro
</option>
<option value="USD" {{ old('currency') == 'USD' ? 'selected' : '' }}>
($) American Dollar
</option>
<option value="GBP" {{ old('currency') == 'GBP' ? 'selected' : '' }}>
(£) GBP
</option>
</select>
</div>
</div>
<div class="form-group row col-md-12">
<label for="price" class="col-sm-2 col-form-label">Normal Price</label>
<div class="col-sm-10">
<input class="form-control" type="number" id="price" name="price"
min="0" value="{{ old('price') }}">
</div>
</div>
<div class="form-group row col-md-12">
<label for="discounted_price" class="col-sm-2 col-form-label">Discounted Price</label>
<div class="col-sm-10">
<input class="form-control" type="number" id="discounted_price"
name="discounted_price"
min="0" value="{{ old('discounted_price') }}">
</div>
</div>
<div class="form-group row col-md-12">
<label for="button_link" class="col-sm-2 col-form-label">Call to Action Link</label>
<div class="col-sm-10">
<input class="form-control" type="url" id="button_link" name="button_link"
value="{{ old('button_link') }}">
</div>
</div>
<div class="form-group row col-md-12">
<label for="company" class="col-sm-2 col-md-2 col-form-label">Company</label>
<div class="col-sm-10 col-md-10">
<select name="company" id="" class="form-control">
<option value="">Please Select</option>
<option
value="londonist_investments" {{ old('company') == 'londonist_investments' ? 'selected' : '' }}>
Londonist Investments
</option>
<option value="turyap_uk" {{ old('company') == 'turyap_uk' ? 'selected' : '' }}>
Turyap UK
</option>
</select>
</div>
</div>
</div>
<div class="col-12">
<div class="form-group row col-md-12">
<label for="key_points" class="col-md-2 col-form-label">Key Points</label>
<div class="col-md-12">
<textarea class="textarea" placeholder="One to each line"
name="key_points" id="key_points"
style="width: 100%; min-height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;">{{ old('key_points')}}</textarea>
</div>
</div>
</div>
<div class="col-12">
<div class="form-actions">
<button type="submit" class="btn btn-danger btn-lg">
<i class="fa fa-check-square-o"></i> Save
</button>
</div>
</div>
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel"
aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalLabel">Laravel Cropper Js - Crop Image Before
Upload - Tutsmake.com</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="img-container">
<div class="row">
<div class="col-md-8">
<img id="image"
src="https://avatars0.githubusercontent.com/u/3456749">
</div>
<div class="col-md-4">
<div class="preview"></div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel
</button>
<button type="button" class="btn btn-primary" id="crop">Crop</button>
</div>
</div>
</div>
</div>
<!-- /.col -->
</div>
<!-- /.row -->
{!! Form::close() !!}
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
#endsection
#section('js')
<script>
var $modal = $('#modal');
var image = document.getElementById('image');
var cropper;
$("body").on("change", ".image", function(e){
var files = e.target.files;
var done = function (url) {
image.src = url;
$modal.modal('show');
};
var reader;
var file;
var url;
if (files && files.length > 0) {
file = files[0];
if (URL) {
done(URL.createObjectURL(file));
} else if (FileReader) {
reader = new FileReader();
reader.onload = function (e) {
done(reader.result);
};
reader.readAsDataURL(file);
}
}
});
$modal.on('shown.bs.modal', function () {
cropper = new Cropper(image, {
aspectRatio: 1,
viewMode: 3,
preview: '.preview'
});
}).on('hidden.bs.modal', function () {
cropper.destroy();
cropper = null;
});
$("#crop").click(function(){
canvas = cropper.getCroppedCanvas({
width: 800,
height: 800,
});
canvas.toBlob(function(blob) {
url = URL.createObjectURL(blob);
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
$.ajax({
type: "POST",
dataType: "json",
url: "crop-image-upload",
data: {'_token': $('meta[name="_token"]').attr('content'), 'image': base64data},
success: function(data){
console.log(data);
$modal.modal('hide');
alert("Crop image successfully uploaded");
}
});
}
});
})
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.6/cropper.js"></script>
How can I do that?

Better to do resizing in controller, using intervention/image
Package Link
Tutorial Link
Hope this will be useful.

Related

How i fixed $data is undefined?

I have a problem while creating edit and create forms
I have a form that is used to create and edit when I call $data on the form so that it appears when editing the form.
But when I open the form to create new data the error $data is undefined appears.
This is my route
Route::resource('/tabungan/jadwal-autodebet', SavingAccountScheduleController::class);
This is the create and edit script in my controller
public function create()
{
$info['header'] = $this->title;
$info['description'] = 'Tambah';
$info['breadcrumb'] = null;
return view('banking::autodebet-schedule.form',$info);
}
public function edit($id)
{
$info['header'] = $this->title;
$info['description'] = 'Ubah';
$info['breadcrumb'] = null;
$data = TransactionSchedule::find($id);
$origin = clone $data;
$origin = $data->origin;
$destination = clone $data;
$destination = $data->destination;
$destinationType = '';
$saving = SavingAccount::class;
$loan = LoanAccount::class;
if($data->origin_type == $saving){
$destinationType = 'Tabungan';
}elseif($data->origin_type == $loan){
$destinationType = 'Pinjaman';
}
// dd($data,$origin,$destination);
return view('banking::autodebet-schedule.form',compact('data','origin','destination','destinationType'),$info);
}
and this is my autodebet.schedule.form script
<form id="formData" action="{{ isset($data) ? admin_url('cb/tabungan/jadwal-autodebet/'.$data->id) : admin_url('cb/tabungan/jadwal-autodebet/')}}" method="POST" autocomplete="off">
{{ csrf_field() }}
#if(isset($data))
{{method_field('PUT')}}
#endif
<div class="container d-flex justify-content-center">
<div class="col-12">
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Produk Tujuan</label>
</div>
<div class="col-10">
<select name="destination_type" id="destination_type" class="select2 form-control destination_type">
<option value="{{ $data->origin_type }}">{{ $destinationType }}</option>
<option class="type_option" id="SavingAccount" value="Modules\Banking\Entities\SavingAccount">Tabungan</option>
<option class="type_option" id="LoanAccount" value="Modules\Banking\Entities\LoanAccount">Pinjaman</option>
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">No Rekening Sumber</label>
</div>
<div class="col-10">
<select id="origin_code" class="select2 form-control origin_code">
<option value="{{ $origin->id }}">{{ $origin->saving_code }}-{{ $origin->customer->customer_name}}-{{ $destinationType }}</option>
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Transaksi Rekening Anggota/Nasabah Lain</label>
</div>
<div class="col-10">
<input type="hidden" id="other_costumer_transaction" name="other_costumer_transaction" value="0">
<div class="az-toggle {{ 'off' }}" id="other_costumer_transaction_toggle"><span></span></div>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">No Rekening Tujuan</label>
</div>
<div class="col-10">
<select id="destination_code" class="select2 form-control destination_code" >
<option value="{{ $destination->id }}">{{ $destination->saving_code }}-{{ $destination->customer->customer_name}}-{{ $destinationType }}</option>
</select>
</div>
</div>
<div class="row" id="info-nasabah" style="display: none">
<div class="col-2">
</div>
<div class="col-10">
<h6>No. Rekening : <span id="info-kode">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Nama Nasabah : <span id="info-nama">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Tipe Produk : <span id="info-produk">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Saldo : <span id="info-saldo">xxx.xxx.xxx</span></h6>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">Tanggal Autodebet</label>
</div>
<div class="col-10">
<select name="scheduled_day" id="scheduled_day" class="select2 form-control scheduled_day">
<option disabled value="{{ $data->scheduled_day }}">{{ $data->scheduled_day }}</option>
#for ($i = 1; $i <= 31; $i++)
<option class="day_option" id="day_{{ $i }}" value="{{ $i }}">{{ $i }}</option>
#endfor
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label for="saving_type_package">Keterangan</label>
</div>
<div class="col-10">
<textarea name="desc" class="form-control desc" id="" cols="30" rows="5">{{ $data->desc }}</textarea>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Status Autodebet</label>
</div>
<div class="col-10">
<input type="hidden" id="transaction_schedule_status" name="transaction_schedule_status" value="{{ $data->transaction_schedule_status ?? 1 }}">
<div class="az-toggle {{ isset($data->transaction_schedule_status) ? ($data->transaction_schedule_status == 1 ? 'on' :'') : 'on' }}" id="autodebet_status_toggle"><span></span></div>
</div>
</div>
<input type="hidden" value="" id="origin_id" name="origin_id">
<input type="hidden" class="schedule_id" id="schedule_id" name="schedule_id">
<input type="hidden" name="destination_code" id="destination_code_input">
<input type="hidden" name="origin_code" id="origin_code_input">
<div class="row">
<div class="col-12">
<div class="float-right">
<div class="btn-group">
<a href="{{admin_url('cb/tabungan/jadwal-autodebet')}}">
<button id="back_button" type="button" class="btn btn-secondary btn-block"><i class="typcn typcn-times"></i>Batalkan</button>
</a>
</div>
<div class="btn-group">
<button id="submit_button" class="btn btn-success btn-block">
<i id="loading_icon" style="display: none" class="fa fa-spin fa-spinner"></i>
<i id="submit_icon" class="typcn typcn-input-checked"></i>
Simpan</button>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</form>
how i fixed this problem?

Laravel 8 safe (think problem datetime-local input field to mysql)

When I click save on my form, the page reloads. Which is correct until then. Unfortunately, it does not save the data for me in the database. So I tried to find the error with dd($request->all()). Unfortunately, I haven't found a real error as far as the correct data is loaded on the form. However, they never end up in the database. I currently only have one guess that it is due to the wrong format of DateTime_local input. But can't say for sure.
I don't get any error messages or anything like that.
Here is a small snippet of my code:
app/Http/Controllers/TasksController
public function store(Request $request)
{
// validate all required Data
$request->validate([
'title' => 'required',
'user' => 'required',
'labels' => 'required',
'work' => 'required',
'start_work' => 'required',
'problems' => 'required',
'stop_work' => 'required',
]);
$input = $request->all();
Task::create($input);
return back()->with('success', 'Successfully saved.');
}
app/Models/Task.php
class Task extends Model
{
use HasFactory;
public $fillable = ['title', 'user', 'labels', 'work', 'start_work', 'problems', 'stop_work'];
}
Migration
public function up()
{
Schema::create('task', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('user');
$table->string('label');
$table->string('work');
$table->string('problems');
$table->dateTime('start_work');
$table->dateTime('stop_work');
$table->timestamps();
});
}
View snippet
#extends('layouts.app')
#section('content')
<!-- Success message -->
#if(Session::has('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
#endif
<form method="post" action="{{ route('screate') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<section class="person">
<div class="container-fluid">
<div class="card card-default">
<div class="card-header">
<h3 class="card-title">Daten</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="title">title</label>
<input type="text" class="form-control {{ $errors->has('title') ? 'error' : '' }}" id="title" name="title" required>
</div>
<!-- /.form-group -->
</div>
<div class="col-md-4">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control {{ $errors->has('username') ? 'error' : '' }}" id="username" name="username">
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="labels">Labels</label>
<select id="labels" name="label" class="select2 select2-hidden-accessible {{ $errors->has('label') ? 'error' : '' }}" multiple="" name="label[]" style="width: 100%;" data-select2-id="5" tabindex="-1" aria-hidden="true">
#foreach($labels as $label)
<option value="{{ $label->id }}">{{ $label->name }}</option>
#endforeach
</select>
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="work">work</label>
<input type="text" class="form-control {{ $errors->has('work') ? 'error' : '' }}" id="work" name="work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="start_work">Start-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="start_work" name="start_work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="problems">Problems</label>
<input type="text" class="form-control {{ $errors->has('problems') ? 'error' : '' }}" id="problems" name="problems">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="stop_work">Stop-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="stop_work" name="stop_work">
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div><!-- /.container-fluid -->
</section>
<section class="options">
<div class="row">
<div class="col-md-2 centered">
<button class="btn btn-danger" href="{{ route('home') }}">
<i class="fas fa-times">
</i>
Abbrechen
</button>
</div>
<div class="col-md-2">
<button class="btn btn-success" type="submit" name="submit">
<i class="fas fa-plus">
</i>
Speichern
</button>
</div>
</div>
</section>
</form>
#endsection
I hope you can help me with this small problem.
edit:
Here is my dd output:
dd() output

dropdown list not shown in laravel

I've got problem with dropdown list.
i want to show a primary key as a dropdown list in view blade , but i can not make the right rout to show it. Have you any ideas how to solve this problem?
this my root
$objects = ['users', 'permissions', 'roles', 'coins','pillars','subtindicators'];
foreach ($objects as $object) {
Route::get("$object", ucfirst(str_singular($object))."Controller#index")->middleware("permission:browse $object")->name("{$object}");
Route::get("$object/datatable", ucfirst(str_singular($object))."Controller#datatable")->middleware("permission:browse $object")->name("{$object}.datatable");
Route::get("$object/add", ucfirst(str_singular($object))."Controller#add")->middleware("permission:add $object")->name("{$object}.add");
Route::post("$object/create", ucfirst(str_singular($object))."Controller#create")->middleware("permission:add $object")->name("{$object}.create");
Route::get("$object/{id}/edit", ucfirst(str_singular($object))."Controller#edit")->middleware("permission:edit $object")->name("{$object}.edit");
Route::post("$object/{id}/update", ucfirst(str_singular($object))."Controller#update")->middleware("permission:edit $object")->name("{$object}.update");
Route::get("$object/{id}/delete", ucfirst(str_singular($object))."Controller#delete")->middleware("permission:delete $object")->name("{$object}.delete");
Route::get("$object/{id}", ucfirst(str_singular($object))."Controller#view")->middleware("permission:view $object")->name("{$object}.view");
Route::get("$object/create", ucfirst(str_singular($object))."Controller#list")->middleware("permission:add $object")->name("{$object}.create");
this my controller
public function add()
{ $strs = DB::table('stargets')->select('*')->get();;
return view('subtindicators.add-edit',compact('strs'));
}
public function update(Request $request, $id)
{
$object = $this->objectModel::find($id);
$object->update([
'skey_name' => $request->skey_name,
'Subtarget_base' => $request->Subtarget_base,
'Subtarget_end' => $request->Subtarget_end,
'subtarget_id' => $request->subtarget_id
]);
if ($request->save == 'browse')
return redirect()->route("{$this->objectName}");
elseif ($request->save == 'edit')
return redirect()->route("{$this->objectName}.edit", ['id' => $object]);
elseif ($request->save == 'add')
return redirect()->route("{$this->objectName}.add");
else
return redirect($request->previous_url);
}
this my blade
#extends('adminlte::page')
#include('bread.title')
#section('main-content')
<div class="container-fluid spark-screen">
<div class="row">
{!! csrf_field() !!}
<form class="form-horizontal" action="{{$actionName=='edit'?route("{$objectName}.update",['id'=>$object->id]):route("{$objectName}.create") }}" method="post">
{!! csrf_field() !!}
<input type="hidden" name="previous_url" value="{{ url()->previous() }}">
<form class="form-horizontal" action="{{ $actionName == 'edit' ? route("{$objectName}.update", ['id' => $object->id]) : route("{$objectName}.create") }}" method="post">
{!! csrf_field() !!}
<input type="hidden" name="previous_url" value="{{ url()->previous() }}">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">{{ ucfirst($actionName) }} {{ ucfirst($objectName) }} {{ !empty($object) ? "(ID $object->id)" : '' }}</h3>
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">{{ ucfirst($actionName) }} {{ ucfirst($objectName) }} {{ !empty($object) ? "(ID $object->id)" : '' }}</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
</div>
<div class="box-body">
<div class="form-group">
<label for="" class="col-md-2 control-label">col</label>
<div class="col-md-10">
<input type="text" name="skey_name" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_arname : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="col-md-2 control-label">cl_d</label>
<div class="col-md-10">
<input type="text" name="Subtarget_base" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_enname : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="col-md-2 control-label">cl_e</label>
<div class="col-md-10">
<input type="text" name="Subtarget_end" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_arsymbol : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="form-control">c_i</label>
<div class="col-md-10">
<select class="form-control" name="starget_id">
<option selected disabled value = " ">choos</option>
#foreach($strs as $vis)
<option value="{{$vis->id}}">{{$vis->target_name}}</option>
#endforeach
<!-- <p class="form-control-static">{{ $object->subtarget_id }}</p>-->
</div>
</div>
<div class="form-group has-feedback">
<label for="title">main<span class="text-danger">*</span></label>
<select class="form-control" name="starget_id">
<option selected disabled value = " ">choos</option>
#foreach($strs as $slider2)
<option value="{{$slider2->id}}" {{ $slider2->id== $slider->starget_id ? 'selected' : '' }}>{{$slider2->vname}}</option>
#endforeach
</div>
</div>
</div>
<div class="box-footer">
#include('bread.add-edit-actions')
</div>
</div>
</form> </form>
</div>
</div>
</div>
#endsection
this error what i got
ErrorException (E_ERROR)
Undefined variable: actionName (View: C:\project Last\resources\views\bread\title.blade.php) (View: C:\Ministry Last\resources\views\bread\title.blade.php)
You should forward $actionName variable to your view:
public function add()
{
$strs = DB::table('stargets')->select('*')->get();
$actionName = 'edit';
return view('subtindicators.add-edit',compact('strs', 'actionName'));
}
However this will hardcode the form to being an "edit" form. You must see what you really want with your logic.

Googlemap location based on IP address is not showing in laravel project

am trying to show google map in one section of my form, which i want to load locations based on IP Address, but when i tried locations are not loading based on ip address.What is the issue
#extends('user.layout.app')
#section('content')
<script src="{{ url('js/user/location.js') }}"></script>
<div class="container-fluid add-location">
<div class="row">
<div class="col-md-12">
<div class="card">
<form method="post" action="{{ route('locationstore') }}" name="locationadd" id="locationadd" enctype="multipart/form-data" novalidate>
{{ csrf_field() }}
<div class="card-header">
<h4 class="card-title"> Add My Location </h4>
</div>
#if(!empty($errors->all()))
<div class="row"> #foreach ($errors->all() as $error)
<div class="col-lg-12">
<div class="alert alert-danger"> <span>{{ $error }}</span> </div>
</div>
#endforeach </div>
<div class="row geolocationerror hide">
<div class="col-lg-12">
<div class="alert alert-danger"> <span>location not found</span> </div>
</div>
</div>
#endif
<div class="card-content">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="form-group">
<label class="control-label">Location Name
<star>*</star>
</label>
<input id="location_name" name="location_name" class="controls form-control" type="text" placeholder="Location Name" value="{{ old('location_name') }}">
#if ($errors->has('location_name')) <span class="help-block"> {{ $errors->first('location_name') }} </span> #endif </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="form-group">
<label class="control-label">Clinics
<star>*</star>
</label>
<select class="selectpicker" data-style="btn-info btn-fill btn-block" id="clinic_id" name="clinic_id">
#foreach($clinics as$clinic)
<option value="{!! $clinic->clinicID !!}" id="{!! $clinic->clinicID !!}">{!! $clinic->clinicName !!}</option>
#endforeach
</select>
#if ($errors->has('category_id')) <span class="help-block"> {{ $errors->first('category_id') }} </span> #endif </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="form-group">
<label class="control-label">Address Line 1
<star>*</star>
</label>
<input type="text" placeholder="Address Line 1" class="form-control geoaddress" id="address_1" name="address_1" value="">
</div>
<div class="form-group">
<label class="control-label">Address Line 2 </label>
<input type="text" placeholder="Address Line 2" class="form-control geoaddress" id="address_2" name="address_2" value="">
</div>
<div class="form-group">
<label class="control-label">City
<star>*</star>
</label>
<input type="text" placeholder="City" class="form-control geoaddress" id="city" name="city" value="">
</div>
<div class="form-group">
<label class="control-label">State
<star>*</star>
</label>
<input type="text" placeholder="State" class="form-control geoaddress" id="state" name="state" value="">
<input type="hidden" id="state_code" name="state_code" value="" class="hidden-validation">
<input type="hidden" id="formatted_address" name="formatted_address" value="" class="hidden-validation">
<input type="hidden" id="latitude" name="latitude" value="" class="hidden-validation">
<input type="hidden" id="longitude" name="longitude" value="" class="hidden-validation">
</div>
<div class="form-group">
<label class="control-label">Country
<star>*</star>
</label>
<input type="text" placeholder="Country" class="form-control geoaddress" id="country" name="country" value="">
</div>
<div class="form-group">
<label class="control-label">Zip
<star>*</star>
</label>
<input type="text" placeholder="Zip" class="form-control geoaddress" id="zip" name="zip" value="">
</div>
<div class="form-group">
<label class="control-label">Phone Number
<star>*</star>
</label>
<input type="text" placeholder="Phone Number" class="form-control geoaddress" id="phone" name="phone" onkeypress="return isNumber(event)">
</div>
<div class="form-group">
<label class="control-label">Website
<star>*</star>
</label>
<input type="text" placeholder="Website" class="form-control" id="website" name="website" >
<span class="text-info"><strong>[ex. http://www.website.com]</strong></span> </div>
<div class="form-group">
<label class="control-label">Location Email
<star>*</star>
</label>
<input type="text" placeholder="Reports Email" class="form-control" id="reports_email" name="reports_email">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="form-group hide">
<label class="control-label">Location
<star>*</star>
</label>
<input id="pac-input" name="location" class="controls form-control" type="text" placeholder="Search Box">
<!-- <div id="map" height="1000" width="1000"></div> -->
</div>
<h4 >Preview</h4>
<div class="form-group">
<div id="regularMap" class="map"></div>
</div>
</div>
</div>
</div>
<div class="clear"></div>
<div class="clear"></div>
<div class="col-xs-12 col-sm-12 col-md-6">
</div>
<div class="clear"></div>
<div class="col-xs-12 col-sm-12 col-md-12 form-action">
<button type="submit" class="btn btn-fill btn-info">Submit</button>
Cancel </div>
<div class="clear"></div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="{{ asset('js/bootstrap-selectpicker.js')}}"></script>
<script>
$(document).ready(function(){
{{-- custom.initSmallGoogleMaps('41.31', '-72.92'); --}}
var map = new google.maps.Map(document.getElementById('regularMap'), {
zoom: 16,
center: new google.maps.LatLng(41.31,-72.92),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
myMarker = new google.maps.Marker({
position: new google.maps.LatLng(41.31, -72.92),
draggable: true
});
map.setCenter(myMarker.position);
myMarker.setMap(map);
google.maps.event.addListener(myMarker, 'dragend', function(evt) {
document.getElementById("latitude").value = evt.latLng.lat();
document.getElementById("longitude").value = evt.latLng.lng();
});
$('#locationadd').submit(function(event) {
var formatted_address = $('#formatted_address').val();
var latitude = $('#latitude').val();
var longitude = $('#longitude').val();
if(formatted_address == '' && !isFloat(latitude) && !isFloat(longitude))
{
getLocation();
}
});
$('.geoaddress').blur(function(){
getLocation();
})
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
function getLocation(){
var address_1 = $('#address_1').val();
var address_2 = $('#address_2').val();
var city = $('#city').val();
var state = $('#state').val();
var country = $('#country').val();
var zip = $('#zip').val();
if(address_1 != '' && city != '' && state != '' && country != '' && zip != '' ){
var finalAdd = address_1+'+'+address_2+'+'+city+'+'+state+'+'+country+'+'+zip;
var formatted_address = address_1+' '+address_2+' '+city+' '+state+' '+country+' '+zip;
$.ajax({
url : 'https://maps.googleapis.com/maps/api/geocode/json?address='+finalAdd+'&key=MY_KEY',
type: 'GET',
success : function(data){
if(data.results.length >0){
var places = data.results;
document.getElementById("latitude").value = places[0].geometry.location.lat;
document.getElementById("longitude").value = places[0].geometry.location.lng;
document.getElementById("formatted_address").value = formatted_address;
document.getElementById("pac-input").value = places[0].formatted_address;
for (var i = 0, len = places[0].address_components.length; i < len; i++) {
var ac = places[0].address_components[i];
if (ac.types.indexOf("administrative_area_level_1") >= 0) {
document.getElementById("state_code").value = ac.short_name;
document.getElementById("state").value = ac.long_name;
}
}
var newLatLang = new google.maps.LatLng(places[0].geometry.location.lat, places[0].geometry.location.lng);
map.panTo(newLatLang);
myMarker.setPosition(newLatLang);
custom.getTimeZone(places[0].geometry.location.lat, places[0].geometry.location.lng);
}else{
document.getElementById("latitude").value = '';
document.getElementById("longitude").value = '';
document.getElementById("formatted_address").value = '';
$.scrollTo($('#geolocationerror'), 1000);
$('#geolocationerror').show();
event.preventDefault();
event.preventDefault();
}
}
});
}
}
});
</script>
{!! $validator !!}
#endsection
Please not that i have added correct API key, which i have added in .env file. Now the locations are only showing based on values provided in State,Zip,location name values etc.
I want maps get loaded based on the IP adddress

Processing form-required-fields

I can´t figure out how to edit this code in order to get all cotegories (dropdown-list-fields) being required fields. This script only validates up to 2 dropdown-list-fields before being processed and I have up to 4 categories dropdown-list-fields. Any ideas?
<!-- category select -->
<div class="form-group">
<div class="col-md-12">
<?= FORM::label('category', __('Category'), array('for'=>'category'))?>
<div id="category-chained" class="row <?=($id_category === NULL) ? NULL : 'hidden'?>"
data-apiurl="<?=Route::url('api', array('version'=>'v1', 'format'=>'json', 'controller'=>'categories'))?>"
data-price0="<?=i18n::money_format(0)?>"
<?=(core::config('advertisement.parent_category')) ? 'data-isparent' : NULL?>
>
<div id="select-category-template" class="col-md-5 hidden">
<select class="disable-chosen select-category" placeholder="<?=__('Pick a category...')?>"></select>
</div>
<div id="paid-category" class="col-md-12 hidden">
<span class="help-block" data-title="<?=__('Category %s is a paid category: %d')?>"><span class="text-warning"></span></span>
</div>
</div>
<?if($id_category !== NULL):?>
<div id="category-edit" class="row">
<div class="col-md-8">
<div class="input-group">
<input class="form-control" type="text" placeholder="<?=$selected_category->name?>" disabled>
<span class="input-group-btn">
<button class="btn btn-default" type="button"><?=__('Select another')?></button>
</span>
</div>
</div>
</div>
<?endif?>
<input id="category-selected" name="category" value="<?=$id_category?>" class="form-control invisible" style="height: 0; padding:0; width:1px; border:0;" required></input>
</div>
</div>

Categories