why when editing data using modal, change all IDs? - php

Why are all ids are called when editing on one number
Controller
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$plan = $spent_time->plan;
$total_spent_times = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('spent_time');
$request['spent_time'] = (int)$total_spent_times + (int)$request->get('daily_spent_time');
$total_percentage = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('percentage');
$request['percentage'] = (int)$total_percentage + (int)$request->get('daily_percentage');
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time','plan','total_spent_time','total_percentage'));
}
index.blade.php
<td>
<a href="" class="edit_real" data-toggle="modal" data-target="#edit_real-{{$spent_time->plan_id}}">
{{$spent_time->plan->user_story}}
</a>
#include('reals._form')
</td>
_form.blade.php
<div class="modal fade" id="edit_real-{{$spent_time->plan_id}}" role="dialog" >
<div class="modal-dialog modal-sm-8">
<div class="modal-content">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs pull-left">
<li class="">Plan</li>
<li class="active">Real</li>
</ul>
<div class="tab-content">
<div class="chart tab-pane active" id="revenue-chart">
{!! Form::open([$spent_time, 'url' => route('real.update', $spent_time->id), 'method' => 'POST', 'role'=>'form']) !!}
{{ csrf_field() }} {{ method_field('PUT') }}
<div class="box-body">
<div class="form-group">
{!! Form::label('user_story','Today Plan *', ['class' => 'control-label', 'name'=>'user_story']) !!}</label>
{!! Form::text('user_story', old('plan_id', is_null($spent_time->plan->user_story) ? null : $spent_time->plan->user_story),
['class'=>'form-control', 'readonly' => 'true']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_spent_time','Spent Time *', ['class' => 'control-label', 'name'=>'daily_spent_time']) !!}</label>
{!! Form::text('daily_spent_time', old('daily_spent_time', $spent_time->daily_spent_time ?: null),
['class'=>'form-control', 'id'=>'daily_spent_time']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_percentage','% Done *', ['class' => 'control-label', 'name' => 'daily_percentage']) !!}</label>
{!! Form::text('daily_percentage', old('daily_percentage', $spent_time->daily_percentage ?: null),
['class'=>'form-control', 'id'=>'daily_percentage']) !!}
</div>
<div class="form-group">
{!! Form::label('reason','Reason', ['class' => 'control-label', 'name'=>'reason']) !!}</label>
{!! Form::textarea('reason', old('reason', $spent_time->reason ?: null), ['class'=>'form-control', 'id'=>'reason']) !!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-block btn-primary btn-flat', 'type'=>'submit']) !!}
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript">
// jQuery
$('.edit_real').on('click', function (event) {
event.preventDefault();
$('#edit_real').modal();
});
Preliminary data, when editing in number 2, data will be saved to data number 1
Why when editing data using modal, change all IDs?
What should be improved? where is the error that must be corrected? why all ids are called when editing on one number?

Answer
add {!! Form::close() !!} in _form.blade.php

Controller
public function edit($id)
{
$spent_times = SpentTime::all();
$user_story = Plan::pluck('user_story', 'id');
$spent_time = SpentTime::find($id);
return view('reals.edit', compact('spent_times', 'user_story', 'spent_time'));
}
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time'));
}

Related

How to add Edit and Delete function in my FullCalendar laravel ph?

I'm doing a project in laravel, where I can show my list of events using fullcalendar. I already created/have a fullcalendar view with events and adding event, but I want to be able to click the date and edit it or delete it.
Sample event fullcalendar
I already search some solutions or guides for this, but I can't work it properly. I tried creating another one from scratch but it doesn't work.
Here are the links that I searched and doing it as my guide.
FullCalendar Events and Scheduling
github driftingruby/042-fullcalendar
This is my codes in my create.blade.php
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">Event Calendar</div>
<div class="panel-body">
{!! Form::open(array('route' => 'admin/events.create','method'=>'POST','files'=>'true')) !!}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
#if (Session::has('success'))
<div class="alert alert-success">{{ Session::get('success') }}</div>
#elseif (Session::has('warning'))
<div class="alert alert-danger">{{ Session::get('warning') }}</div>
#endif
</div>
<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group">
{!! Form::label('event_name','Event Name:') !!}
<div class="">
{!! Form::text('event_name', null, ['class' => 'form-control']) !!}
<!-- {!! $errors->first('event_name', '<p class="alert alert-danger">:message</p>') !!} -->
</div>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
{!! Form::label('start_date','Start Date:') !!}
<div class="">
{!! Form::date('start_date', null, ['class' => 'form-control']) !!}
<!-- {!! $errors->first('start_date', '<p class="alert alert-danger">:message</p>') !!} -->
</div>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
{!! Form::label('end_date','End Date:') !!}
<div class="">
{!! Form::date('end_date', null, ['class' => 'form-control']) !!}
<!-- {!! $errors->first('end_date', '<p class="alert alert-danger">:message</p>') !!} -->
</div>
</div>
</div>
<div class="col-xs-1 col-sm-1 col-md-1 text-center"> <br/>
{!! Form::submit('Add Event',['class'=>'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Event Details</div>
<div class="panel-body">
{!! $calendar_details->calendar() !!}
</div>
</div>
</div>
<link rel="stylesheet" href="https://cdjns.cloudfare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.css"/>
<!-- Scripts -->
<script src="http://code.jquery.com/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.js"></script>
{!! $calendar_details->script() !!}
And this is my code for my AdminEventsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Events;
use App\Barangay;
use Illuminate\Support\Facades\Auth;
use Session;
use Calendar;
class AdminEventsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index() {
$events = Events::get();
$event_list = [];
foreach ($events as $key => $event) {
$event_list[] = Calendar::event(
$event->event_name,
true,
new \DateTime($event->start_date),
new \DateTime($event->end_date.' +1 day')
);
}
$calendar_details = Calendar::addEvents($event_list);
return view('admin.events.create', compact('calendar_details') );
}
public function addEvent(Request $request)
{
$mybrgyid = Auth::user()->brgyid;
$mybarangay = Barangay::select('brgyname')->where('id', $mybrgyid)->get();
session(['mybarangay' => $mybarangay]);
$this->validate($request,[
'event_name' => 'required',
'start_date' => 'required',
'end_date' => 'required'
]);
//$validator = validator::make($request->all(), [
//'event_name' => 'required',
//'start_date' => 'required',
//'end_date' => 'required'
//]);
//if ($validator->fails()) {
// \session::flash('warning', 'Please enter the valid details');
// return Redirect::to('/admin/events')->withErrors($validator);
//}
$event = new Events;
$event->brgyid = Auth::user()->id;
$event->event_name = $request['event_name'];
$event->start_date = $request['start_date'];
$event->end_date = $request['end_date'];
$event->save();
return redirect('/admin/events')->with('success', 'Event Created');
}
}
Somehow I'm being confuse as to where I would create a modal for editing and deleting an event in my fullcalendar.
Your help is much appreciated.

Submitting modal form while other form is open in Laravel 5.6

What I have:
I've got a web application where I want to be able to add qualifications to a user.
When I open the Edit Employee form there is an Add button and a Delete button next to a select box displaying all qualifications associated with that user. When clicking on the Add button, a Bootstrap modal opens, with another form in it, where you can choose the Qualification as well as a Date Of Issue.
What I want:
For now I want the qualification to be added to the user and then let the controller redirect to the edit view, with a success message. Later I would like the qualification to be added via Ajax, close the window and display a success message as well. Maybe only refresh the Qualifications form element in the main form as well, if possible.
Problem:
When I submit the form in the modal, the main form get's submitted, not the one from the modal, even though I call different controller methods on submit.
My Forms:
Modal:
<link href="{{ asset('css/modals.css') }}" media="all" rel="stylesheet" type="text/css" />
<div class="modal fade" id="qualificationModal" tabindex="-1" role="dialog" aria-labelledby="qualificationModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="qualificationModalLabel" style='font-weight: bold;'>Add Qualification</h4>
</div>
<div class="modal-body">
{!! Form::open(['route' => ['qualification.add', $employee->id], 'method' => 'PUT']) !!}
<div class="form-group">
{{ Form::label('qualification', 'Qualification') }}<br>
{{ Form::select('qualification', $qualification_names, null, array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('issue_date', 'Date Of Issue') }}<br>
{{ Form::date('issue_date', \Carbon\Carbon::now(), array('class' => 'form-control', 'required' => 'required')) }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
{!! Form::submit('Add', array('class' => 'btn btn-primary')) !!}
</div>
</div>
</div>
</div>
Main:
#section('content')
<div class='col-lg-4 col-lg-offset-4'>
<h1><i class='fa fa-user-plus'></i> Edit {{$employee->name}}</h1>
<hr>
{{ Form::model($employee, ['route' => ['employees.update', $employee->id], 'method' => 'PUT']) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', null, array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email', null, array('class' => 'form-control', 'required' => 'required')) }}
</div>
<h5><b>Give Role</b></h5>
<div class='form-group'>
#foreach ($roles as $role)
{{ Form::checkbox('roles[]', $role->id, $employee->roles ) }}
{{ Form::label($role->name, ucfirst($role->name)) }}<br>
#endforeach
</div>
<div class="form-group">
{{ Form::label('password', 'Password') }}<br>
{{ Form::password('password', array('class' => 'form-control', 'placeholder' => ' • • • • • • • • • •', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Confirm Password') }}<br>
{{ Form::password('password_confirmation', array('class' => 'form-control', 'required' => 'required')) }}
</div>
<div class="form-group">
{{ Form::label('qualifications', 'Qualifications') }}<br>
{{ Form::select('qualifications', $employee->qualifications, null, ['size' => 5, 'class' => 'form-control']) }}
<button
type="button"
class="btn btn-default pull-right"
data-toggle="modal"
data-target="#qualificationModal"
data-qualifications="{{ $qualifications }}"
data-qualification_names="{{ $qualification_names }}">
Add
</button>
<button type="button" class="btn btn-danger pull-right">Remove</button>
#include('dispo.employees.add_qualification')
<br>
<br>
</div>
{{ Form::submit('Save', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
#stop
Routes:
Route::put('', [
'as' => 'qualification.add',
'uses' => 'DispoEmployeeController#addQualification'
]);
Route::resource('employees', 'Dispo\DispoEmployeeController');
Controller
public function update(Request $request, $id){
$employee = User::findOrFail($id);
//Validate name, email and password fields
$this->validate($request, [
'name'=>'required|max:120',
'email'=>'required|email|unique:users,email,'.$id,
'password'=>'required|min:6|confirmed',
]);
$input = $request->only(['name', 'email', 'password']);
$roles = $request->get('roles'); //Retreive all roles
$employee->fill($input)->save();
if (isset($roles)) {
$employee->syncRoles($roles);
}
else {
$employee->roles()->detach();
}
return redirect()->route('employees.index')->with('success','User successfully edited.');
}
public function addQualification(Request $request, $id){
$employee = User::findOrFail($id);
$this->validate($request, [
]);
$qualification = $request->input('qualification');
$employee->qualifications()->attach($qualification);
return redirect()->route('employees.edit')->with('success','Qualification successfully edited.');
}
I've made an assumption in this answer, so if this is wrong let me know and I can adjust.
Assumption: your modal is being added to the page via the blade partial #include('dispo.employees.add_qualification').
If this is how the modal is being included, then you have your modal form nested inside your main form (which would explain why the main form is being submitted).
Solution: move your modal outside the main form, by shifting #include('dispo.employees.add_qualification') to be below {{ Form::close() }} in the main blade file.

MethodNotAllowedHttpException

I'm trying to update the fields in the database, but I couldn't
here is my routes :
Route::get('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
here the controller:
public function postOrder()
{
$this->orderForm->validate(Input::all());
$order = $this->orders->getNew([
'link' => Input::post('link'),
'size' => Input::post('size'),
'color' => Input::post('color')
]);
$this->orders->save($order);
return Redirect::back()->withMessage('Order has been updated');
}
here is the blade:
{{ Form::open() }}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
so each time I try to update the order I got error "MethodNotAllowedHttpException ", I tried a lot of methods but I'm lost. I'm still beginner in php and this problem drive me crazy, so I'll be glad if you can help guys.
thanks
*** I've updated the code
So you're posting to the route, /orders. Therefor you need a HTTP POST request. You're now assigning a GET request to the /orders route.
You need to change your code to:
Route::post('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
Also you need to add a CSRF Token, this can be done through adding {!! csrf_field() !!} in your blade (inside your Form open and close).
{{ Form::open() }}
{!! csrf_field() !!}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Hope this works!
You must specify the method in the Form::open method.
{{ Form::open(array('method' => 'post')) }}
Just added this in the repository:
public function updateOrder($id, array $data)
{
$orders = $this->getById($id);
if (!empty($data['title'])) {
$orders->title = $data['title'];
}
if (!empty($data['link'])) {
$orders->link = $data['link'];
}
(AND SO ON)
$orders->save();
and in the controller:
public function postOrder($id)
{
$this->orders->updateOrder($id, Input::all());
return Redirect::back()->withMessage('Updated');
}
and that's it

Class 'App\Http\Controllers\Image' not found - HomeController.php line 53 - Laravel 5.1

This is the error I get when I submit my create code. Looks like my public_path is not being respected by the store function, so it looks for a controller called Image.
Hope you guys can help me find out the error.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Flash;
use File;
use App\Home;
class HomeController extends Controller
{
public function store(Request $request)
{
$data = $request->all();
$image = $request->file('image');
if($image)
{
$data['image'] = $image->getClientOriginalName();
}
$home = new Image($request->all());
$home->fill($request->all());
$home->image = $data['image'];
$home->save();
if($home->image){
$path = public_path() . '/image/homes/' . $home->id;
if( ! File::exists($path)) {
File::makeDirectory($path, 0775, true, true);
}
$imageName = $home->image;
$request->file('image')->move($path, $imageName);
}
Flash::success("Se ha agregado un nuevo slider con exito");
return redirect()->route('home.index');
}
}
My public path is -> public/image/homes
Create function:
#section('main-content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Crear slider</h3>
</div>
<div class="panel-body">
{!! Form::open(['route' => 'home.store', 'method' => 'POST', 'files' => true]) !!}
#include('home.form')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
#endsection
Actual form:
<div class="form-group">
{!! Form::label('text', 'Texto') !!}
{!! Form::text('text', null, ['class' => 'form-control', 'placeholder' => 'Texto', 'required']) !!}
</div>
<div class="form-group">
{!! Form::label('order', 'Orden') !!}
{!! Form::text('order', null, ['class' => 'form-control', 'placeholder' => 'Orden', 'required']) !!}
</div>
<div class="form-group">
{!! Form::label('Imagen ') !!}
</div>
<div class="form-group">
#if( ! isset($home->image))
<div class="col-sm-6">
{!! Form::file('image', null, array('class' => 'image')) !!}
</div>
#else
<div class="col-sm-6">
<img class="thumbnail" src="/image/stores/{{ $home->id }}/{{ $home->icon }}" width="200" height="100">
{!! Form::text('image', $home->image, array('class' => 'image hidden')) !!}
{!! Form::file('image', null, array('class' => 'image', 'form-control')) !!}
</div>
#endif
</div>
<div class="form-group">
{!! Form::submit('Guardar', ['class' => 'btn btn-primary']) !!}
</div>

Form Data Not Creating Array

I have a form which I use jQuery .cloneme to make duplicate elements. I have the form elements names as "xxxxx[]" so they should create an array once processed, but I'm only getting the first instance:
#extends('app')
#section('content')
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1>Create a New Jobsheet</h1>
<hr/>
{!! Form::open(['url' => 'jobsheets']) !!}
<div class="form-group">
{!! Form::label('customer','Customer') !!}
<select name="customer" size="1" class="form-control">
<option value="" disable selected>-- Select Customer --</option>
#foreach($customers as $c)
<option value="{{ $c->name }}">{{ $c->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<br/>
<div class="row">
<div class="col-md-5 col-md-offset-1">
<div class="form-group">
{!! Form::label('travel','Travel Description: ') !!}
{!! Form::text('travel',null,['class' => 'form-control']) !!}
{!! Form::label('travelduration','Travel Duration: ') !!}
{!! Form::input('number','travelduration',null,['class' => 'form-control','step'=>'any']) !!}
</div>
</div>
<div class="col-md-4 col-md-offset-1">
<div class="form-group">
{!! Form::label('mileage','Mileage: ') !!}
{!! Form::input('number','mileage',null,['class' => 'form-control','step'=>'any']) !!}
</div>
</div>
</div>
<br/>
<div id="product">
<div class="clonedInput" id="input1">
<div class="row">
<div class="col-md-6 col-md-offset-1">
{!! Form::label('product[]','Product: ') !!}
<select name="product[]" size="1" id="product1">
#foreach($products as $p)
<option value="{{ $p->name }}">{{ $p->name }}</option>
#endforeach
</select>
</div>
<div class="col-md-2 col-md-offset-1">
{!! Form::label('prodquant[]','Product Quantity: ') !!}
{!! Form::input('number','prodquant[]','1', ['class' => 'form-control', 'id' => 'prodquant1', 'step' => 'any'])
!!}
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-1">
{!! Form::label('proddescription[]','Description: ') !!}
{!! Form::textarea('proddescription[]',null,['class' => 'form-control','id' => 'proddescription1']) !!}
</div>
</div>
</div>
</div>
<button class="cloneme" rel="product">Add Product</button>
<div class="form-group">
{!! Form::submit('Create Jobsheet', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection
#section('scripts')
<script>
//should clone and add on to the bottom of the scorer sections in the game edit form
$(function() {
$('.cloneme').click(function(e){
e.preventDefault();
var id = $(this).attr("rel");
var clone_item = $("#" + id).find(".clonedInput");
clone_item.clone().removeAttr("id").removeClass("clonedInput").appendTo('#' + id);
});
});
</script>
#endsection
Can someone please tell me why when I create a duplicate the form data doesn't go into an array?
You can debug valid post data with chrome Inspector (F12), tab Network.
In code you dump all as below
dd($request->all());
//or
dd(\Input::all());
If everything works fine, you can loop data by
foreach($request->get('product') as $key=>$product) {
$qty = $request->get('prodquant')[$key];
}
Edit:
It should be:
#section('content')
{!! Form::open.... !!}
// all form inputs
{!! Form::close() !!}

Categories