Form Data Not Creating Array - php

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() !!}

Related

Adding two different controllers to a single blade view in laravel which contains two deifferent forms

In my laravel application, I have a view to edit my non-admin users (participants) (via admin dashboard).
In this application, each user can add a pet too.
Once an admin goes to a certain participant's profile admin should be able to edit the participant and also the pet info as well if needed.
For that, I have to use two forms, one to update the user and another to update the pet. But both the forms are in a single view.
I've already created the participant controller (ParticipantController) and tested all the methods are working fine.
For the pets too I created a different model and a controller(PetsController).
But now I'm struggling to connect my pets controller to the participant edit blade.
following is the routing for the relevant scenario in my web.php
Route::group(['middleware' => ['auth']], function() {
Route::resource('/admins/roles','Admin\RoleController');
Route::resource('/admins/users','Admin\UserController');
Route::resource('/admins/participants','Admin\ParticipantController');
});
I've already created the participant's edit form which works properly and looking for some help to adding the petscontroller to the same blade.
following is my participants edit blade
#extends('layouts.admin')
#section('content')
<div class="row">
<div class="col-md-9">
<h2 class="view-title">{{ __('Edit User') }}</h2>
</div>
<div class="col-md-3">
<div class="text-left">
<!-- <a class="btn btn-primary btn-admin" href="{{ route('users.index') }}"> {{ __('Back') }}</a> -->
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<a class="close" onclick="jQuery('.alert').hide()">×</a>
<p>{{ $message }}</p>
</div>
#endif
<div class="row">
<div class="col-md-12" mt-5>
#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>
</div>
{!! Form::model($user, ['method' => 'PATCH','enctype'=>'multipart/form-data','route' => ['participants.update', $user->id]]) !!}
<div class="row">
<div class="col-md-3 mt-5">
<!-- #if($user->image_id != 'default-avatar.png')
<button type="submit" name="resetphoto" class="btn btn-danger px-3 mr-5 pull-right"><i class="fa fa-trash" aria-hidden="true"></i>
</button>
#endif
-->
#if(empty($user->image_id))
<center> <img src="/propics/default-avatar.png" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3"></center>
</p>
#else
<center> <img src="/propics/{{$user->image_id}}" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3"></center>
#endif
<center><label for="propic" class="btn btn-default subscribe"><i class="fas fa-camera"></i></label></center>
<input id="propic" type="file" name="image_id" class="form-control">
<label id="file_name"></label>
#error('propic')
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-9 mt-5">
<div class="form-group row">
<div class="col-md-6">
{!! Form::text('first_name', null, array('placeholder' => 'First Name','class' => 'form-control txt_txt')) !!}
{!! $errors->first('first_name', '<span class="help-block" role="alert">:message</span>') !!}
</div>
<div class="col-md-6">
{!! Form::text('last_name', null, array('placeholder' => 'Last Name','class' => 'form-control txt_txt')) !!}
{!! $errors->first('last_name', '<span class="help-block" role="alert">:message</span>') !!}
</div>
</div>
<div class="form-group row">
<div class="col-md-6 ">
{!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control txt_txt')) !!}
{!! $errors->first('email', '<span class="help-block" role="alert">:message</span>') !!}
</div>
<div class="col-md-6 ">
{!! Form::password('password', array('placeholder' => 'Password','class' => 'form-control txt_txt')) !!}
{!! $errors->first('password', '<span class="help-block" role="alert">:message</span>') !!}
</div>
</div>
<div class="form-group row">
<div class="col-md-6 ">
{!! Form::password('confirm-password', array('placeholder' => 'Confirm Password','class' => 'form-control txt_txt')) !!}
</div>
<div class="col-md-6">
{!! Form::select('roles[]', $roles,$userRole, array('class' => 'form-control','multiple')) !!}
{!! $errors->first('roles', '<span class="help-block" role="alert">:message</span>') !!}
{!! Form::text('role_id','null', array('class' => 'form-control txt_none')) !!}
{!! Form::text('gender','null', array('class' => 'form-control txt_none')) !!}
</div>
</div>
<div class="col-md-12 text-right px-0 ">
<a class="btn btn-primary btn-admin-form-cancel mt-5" href="{{ route('participants.index') }}"> {{ __('Cancel') }}</a>
<button type="submit" class="btn btn-primary btn-admin-form-save mt-5">{{ __('Save') }}</button>
</div>
</div>
</div>
{!! Form::close() !!}
<div class="row">
<hr class="black_line">
</div>
{!! Form::model($user, ['method' => 'PATCH','enctype'=>'multipart/form-data','route' => ['participants.update', $user->id]]) !!}
<div class="row">
<div class="col-md-3 mt-5">
<!-- #if($user->image_id != 'default-avatar.png')
<button type="submit" name="resetphoto" class="btn btn-danger px-3 mr-5 pull-right"><i class="fa fa-trash" aria-hidden="true"></i>
</button>
#endif
-->
#if(empty($user->image_id))
<center> <img src="/propics/default-avatar.png" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3"></center>
</p>
#else
<center> <img src="/propics/{{$user->image_id}}" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3"></center>
#endif
<center><label for="propic" class="btn btn-default subscribe"><i class="fas fa-camera"></i></label></center>
<input id="propic" type="file" name="image_id" class="form-control">
<label id="file_name"></label>
#error('propic')
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-9 mt-5">
<div class="form-group row">
<div class="col-md-6">
{!! Form::text('first_name', null, array('placeholder' => 'Pet Name','class' => 'form-control txt_txt')) !!}
{!! $errors->first('first_name', '<span class="help-block" role="alert">:message</span>') !!}
</div>
<div class="col-md-6">
{!! Form::text('pet_age', null, array('placeholder' => 'Pet Age','class' => 'form-control txt_txt')) !!}
{!! $errors->first('pet_age', '<span class="help-block" role="alert">:message</span>') !!}
</div>
</div>
<div class="col-md-12 text-right px-0 ">
<a class="btn btn-primary btn-admin-form-cancel mt-5" href="{{ route('participants.index') }}"> {{ __('Cancel') }}</a>
<button type="submit" class="btn btn-primary btn-admin-form-save mt-5">{{ __('Save') }}</button>
</div>
</div>
</div>
{!! Form::close() !!}
#endsection

Laravel fullcalendar- How to display the event specifically on the current user?

I had developed a system that use maddhatter/laravel-fullcalendar.It works and i had no problem to display the calendar with the events.However, there is the problem that the calendar show all events include from different users.Can someone help me to solve this?
Controller
//show the events in the calendar
$events = Schedule::get();
$events->user()->id;
$event_list = [];
foreach ($events as $key => $event) {
$event_list[] = Calendar::event(
$event->event_name,
false,
new \DateTime($event->start_date),
new \DateTime($event->end_date),
null,
// Add color and link on event
[
'color' => '#05B06C',
//'url' => 'http://full-calendar.io',
]);
}
//Display Fullcalendar
$calendar_details = Calendar::addEvents($event_list)
->setOptions([ //set fullcalendar options
'firstDay'=> 1,
'editable'=> true,
'navLinks'=> true,
'selectable' => true,
'durationeditable' => true,
]);
return view('front.teacher.schedule.index', compact('calendar_details','events') );
}
Model
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Schedule extends Model
{
protected $fillable=[
'event_name','start_date','end_date'
];
}
View
{!! Form::open(array('route' =>'schedule:addEvents','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','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-sm-3">
<div class="form-group">
{!! Form::label('start_date','Start Date:')!!}
<div class="">
{!! Form::input('datetime-local','start_date',\Carbon\Carbon::now(),['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::input('datetime-local','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 cold-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">My Event Details</div>
<div class="panel-body">
{!! $calendar_details->calendar() !!}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('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() !!}
Schedule Table
Table

why when editing data using modal, change all IDs?

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'));
}

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

Cannot Access $errors in laravel 5.2

I am having a issue with Laravel 5.2 accessing the $errors variable in my partial.
routes.php
I have wrapped my routes in the middleware web.
and in my auth.blade.php
<div class="col-md-4">
<div class="panel panel-{{ $errors->all() ? 'danger' : 'default'}}">
<div class="panel-heading">
`enter code here`<h2 class="panel-title">#yield('heading')</h2>
</div>
<div class="panel-body">
#if($errors->all())
<div class="alert alert-danger">
<strong>We found some errors</strong>
<ul>
#foreach($errors->all() as $errors)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#yield('content')
</div>
</div>
</div>
here is the code for my login partial
#extends('layouts.auth')
#section('title','Login')
#section('heading','Welcome Please login')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Login', ['class' => 'btn btn-primary']) !!}
Forgot password?
{!! Form::close() !!}
#endsection
when i click login it just redirects to the same page buit not flashing the errors?
When i click login it shows this error
enter image description here
Remove web middleware, this may cause empty $errors. Since 5.2.27, web middleware aplies automatically to all routes and you shouldn't add it again manually.

Categories