inside EmployeeController in the edit function, i have this code
public function edit($id)
{
$employees = Employee::find($id);
$departmentlists = Department::pluck('id', 'name');
return view('employees.edit', compact('employees', 'departmentlists'));
}
and inside edit.blade.php to display the dropdown i have this code
{!! Form::open(['action' => ['EmployeeController#update', $employees->id], 'method' => 'POST', 'autocomplete' => 'off', 'class' => 'form-horizontal', 'enctype' => 'application/x-www-form-urlencoded']) !!}
<div class="card">
<div class="card-header card-header-primary">
<h4 {{ Form::label('', 'Change Employee Data', ['class' => 'card-title']) }}
</h4>
<p class="card-category"></p>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12 text-right">
<a href="{{ route('employees.index') }}" class="btn btn-sm btn-primary" style="font-size:12px">
<i class="material-icons">keyboard_backspace</i>
{{ __('kembali') }}
</a>
</div>
</div>
<div class="row">
{{ Form::label('Name', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{Form::text('name', $employees->name, ['class' => 'form-control', 'placeholder' => 'Name', 'required'])}}
<p style="color: red;">#error('name') {{ $message }} #enderror</p>
</div>
</div>
<div class="row">
{{ Form::label('Department', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="col-md-7">
<div class="form=group">
#foreach($departmentlists as $dl)
<option value="{{ $dl->id }}" #if($dl->id==$employees->department_id) selected='selected' #endif >{{ $employees->name }}</option>
#endforeach
<p style="color: red;">#error('id') {{ $message }} #enderror</p>
</div>
</div>
</div>
<div class="row">
{{ Form::label('Keterangan', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="form-group col-sm-7">
{{ Form::textarea('information', $employees->information, ['class' => 'form-control', 'placeholder' => 'Keterangan', 'rows' => '6', 'cols' => '50']) }}
</div>
</div>
</div>
<div class="card-footer ml-auto mr-auto">
{{ Form::hidden('_method','PUT') }}
{{ Form::submit('Ubah', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
This is the employees table
and this is departments table
Now, the goal is i want the dropdown on edit page to display department name of the employee belongs to, while the dropdown still have all of the department name.
so i change can it, but when i run this code it gives me this error.
Trying to get property 'id' of non-object (View:
C:\xampp\htdocs\ims-it-laravel7\resources\views\employees\edit.blade.php)
i have read other threads here but those code still doesn't solve the problem
$departmentlists = Department::pluck('id', 'name');
Later You use
#foreach($departmentlists as $dl) and $dl->id
$dl is NOT an object, it is an array because of the pluck() function.
More precisely it looks like this
[
"abc" => 1
"xyz" => 2
"foo" => 3
]
Note: To see it Yourself try using dd($departmentlists) or dump($departmentlists)
Please see Laravel Eloquent Pluck
In this case You might want to use Department::select(['id', 'name'])->get() as it will return collection of objects with specified properties.
Related
I can create unlimited reply comment by recursively. This thing can be terrible because my web looks ugly.
I want the user just can reply the comment maximum 3 depth of reply comments so if the depth has more from the maximum depth the comment not create nesting again.
Here is my code that I can try so far, in blog.detail.php to show comment
#include('nested.replies', ['comments' => $post->comments, 'post_id' => $parameter, 'depth' => 0])
and in replies.php
#foreach($comments->sortByDesc('created_at') as $comment)
<?php
$parameter = Hashids::connection()->encode($post->id);
$parameter2 = Hashids::connection()->encode($comment->id);
?>
<ol class="comments-list display-comment">
<li>
<div class="comment-box clearfix" >
<div class="avatar"><img alt="" src="{{ asset('frontboard/images/avatar.png') }}" /></div>
<div class="comment-content" id="reply?{{ $parameter2 }}">
<div class="comment-meta">
<span class="comment-by">{{ $comment->user->name }}</span>
<span class="comment-date">{{ $comment->date }}</span>
</div>
<span class="reply-link" stle="padding-top:-220px;">
<p>{{ $comment->body }}</p>
#if(Auth::guard('member')->check() && Auth::guard('member')->user()->active == 1)
#if(Auth::guard('member')->user()->isBan == 1)
<span class="reply-link">
Blocked
#else
{!! Form::open(['method' => 'DELETE','route' => ['comment.destroy', $comment->id]]) !!}
{!! Form::submit('delete', ['onclick' => 'return confirm("Are you sure?");','class' => 'btn btn-danger btn-xs pull-right'])!!}
{!! Form::close() !!}
Reply
{!! Form::open([
'route' => ['reply.add', $post->slug],
'class' => 'showActionComment',
'data-parsley-validate'])
!!}
#csrf
<div class="form-group {{ $errors->has('comment_body') ? 'has-error': '' }}">
{!! Form::textarea('comment_body', null, [
'name' => 'comment_body',
'class' => 'form-control',
'placeholder' => 'Write Something.. ',
'required', 'minlength="4"'])
!!}
#if($errors->has('comment_body'))
<span class="help-block">{{$errors->first('comment_body')}}</span>
#endif
{!! Form::hidden('post_id', $parameter) !!}
{!! Form::hidden('comment_id', $parameter2) !!}
</div>
<div class="form-group">
<input type="submit" id="postBtn" value="Add Comment" />
<input type="button" class="btn1 btn-warning" value="Hide" />
</div>
{!! Form::close() !!}
#endif
#endif
</span>
</div>
</div>
#if($depth < 3)
<ul>
<li>
#include('nested.replies', ['comments' => $comment->replies, 'depth' => $depth + 1])
</li>
</ul>
#endif
</li>
</ol>
#endforeach
we can see the replies on here
#if($depth < 3)
<ul>
<li>
#include('nested.replies', ['comments' => $comment->replies, 'depth' => $depth + 1])
</li>
</ul>
#endif
I think this code not working properly, because it still executeS nesting comment..
does every use of capital, have to use a validator in the store controller? and if you don't use a validator, what's the problem?
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 (modal)
<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']) !!}
</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']) !!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-block btn-primary btn-flat']) !!}
</div>
</div>
</div>
</div>
</div>
</div>
<script>
// jquery
$('.edit_real').on('click', function (event) {
event.preventDefault();
$('#edit_real').modal();
});
</script>
this is the resulting display
the problem faced is that it cannot save data after editing. what should be repaired?
_form.blade.php (Modal)
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-block btn-primary btn-flat', 'type'=>'submit']) !!}
</div>
How can I insert Multiple Row of Cart:: content() in Laravel 5.
I'm using Crinsane/LaravelShoppingcart for my cart, however, when you click on proceed to payment, I want the user to fill Customer details like (Terminal ID, sale date, and customer name. But I'm having issue with inserting Cart::(Content) into my database (I dont want to insert instance. I like to break the cart down to my database column. example, if someone pick like 2-5 items, I want it to be inserted to my db, with each item in each row. I've break the Cart::content down in my view, but I need to be able to use Laravel to insert multiple row based on the number of Items selected.
PLEASE NOTE: IF I ONLY ADD ONLY ONE ITEM TO CART, I'M ABLE TO INSERT TO DATABASE, IT'S ONLY WHEN THE ITEM IN CART IS MORE THAN ONE THAT I HAVE ISSUE WITH.
Below is my proceed.blade.php
<div class="panel-heading">Customer Detail</div>
<div class="panel-body">
#if (sizeof(Cart::content()) > 0)
{!! Form::open(['method'=>'POST', 'action'=> 'SalesController#store','files'=>true]) !!}
{!! Form::text('counter', $counter=Cart::content()->groupBy('id')->count(), ['class'=>'form-control'])!!}
#foreach (Cart::content() as $item)
<div class="form-group">
{!! Form::label('product_name', 'Product Name:') !!}
{!! Form::text('product_name', $item->name, ['class'=>'form-control'])!!} </div>
<div class="form-group">
{!! Form::label('quantity', 'Quantity:') !!}
{!! Form::text('quantity', $item->qty, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('unit_price', 'Unit Price:') !!}
{!! Form::text('unit_price', $item->price, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('rowId', 'Row ID:') !!}
{!! Form::text('rowId', $item->rowId, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('tax', 'Tax:') !!}
{!! Form::text('tax', $item->tax, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('total_price', 'Sub Total:') !!}
{!! Form::text('total_price', $item->subtotal, ['class'=>'form-control'])!!}
</div>
#endforeach
#endif
<div class="form-group">
{!! Form::label('terminal_id', 'Terminal ID:') !!}
{!! Form::text('terminal_id', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('customer_name', 'Customer Name:') !!}
{!! Form::text('customer_name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('sale_date', 'Sale Date:') !!}
{!! Form::text('sale_date', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Check Out', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
`
MY CONTROLLER
$counter= $request['counter'];
for ($i = 0; $i < count($counter); $i++) {
Sale::create([
'terminal_id' => $request['terminal_id'],
'customer_name' => $request['customer_name'],
'unit_price' => $request['unit_price'],
'total_price' => $request['total_price'],
'tax' => $request['tax'],
'quantity' => $request['quantity'],
'product_name' => $request['product_name'],
'sale_date' => $request['sale_date'],
'rowId' => $request['rowId']
]);
}
return redirect('shop');
}
First your fields are not arrays so with each #foreach in your view, the old value overrides.
Make your inputs to Array inputs by changing your view this way:
<div class="panel-heading">Customer Detail</div>
<div class="panel-body">
#if (sizeof(Cart::content()) > 0)
{!! Form::open(['method'=>'POST', 'action'=> 'SalesController#store','files'=>true]) !!}
{!! Form::text('counter', $counter=Cart::content()->groupBy('id')->count(), ['class'=>'form-control'])!!}
<?php $idx = 0; ?>
#foreach (Cart::content() as $item)
<div class="form-group">
{!! Form::label("product_name[$idx]", 'Product Name:') !!}
{!! Form::text("product_name[$idx]", $item->name, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("quantity[$idx]", 'Quantity:') !!}
{!! Form::text("quantity[$idx]", $item->qty, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("unit_price[idx]", 'Unit Price:') !!}
{!! Form::text("unit_price[idx]", $item->price, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("rowId[$idx]", 'Row ID:') !!}
{!! Form::text("rowId[$idx]", $item->rowId, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("tax[$idx]", 'Tax:') !!}
{!! Form::text("tax[$idx]", $item->tax, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label("total_price[$idx]", 'Sub Total:') !!}
{!! Form::text("total_price[$idx]", $item->subtotal, ['class'=>'form-control'])!!}
</div>
<?php $idx++; ?>
#endforeach
#endif
<div class="form-group">
{!! Form::label('terminal_id', 'Terminal ID:') !!}
{!! Form::text('terminal_id', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('customer_name', 'Customer Name:') !!}
{!! Form::text('customer_name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('sale_date', 'Sale Date:') !!}
{!! Form::text('sale_date', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Check Out', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
I suggest you also putting your for loop in a transaction in order to improve it's safety.
Like this:
public function store(SalesCreateRequest $request) {
$counter = Input::get('counter');
\DB::transaction(function() use ($counter){
for ($i=0; $i < $counter; $i++){
Sale::create([
'terminal_id' => Input::get("terminal_id")[$i],
'customer_name'=> Input::get("customer_name")[$i],
'unit_price'=> Input::get("unit_price")[$i],
'total_price'=> Input::get("total_price")[$i],
'tax'=> Input::get("tax")[$i],
'quantity'=> Input::get("quantity")[$i],
'product_name'=> Input::get("product_name")[$i],
'sale_date'=> Input::get("sale_date")[$i],
'rowId'=> Input::get("rowId")[$i]
]);
}
});
return "working";
}
Note that you could simply add a [] suffix instead of [$idx] in your input names to make your inputs arrays, but usually indexing your inputs explicitly is safer.
Edit the $idx key from Cart::content() was returning a weird number, that was why simple numerical numbers threw Out of range exception.
Change your view code as I did above, I hope fixing it this way.
Hope it helps
Just modify your controller
$counter = $request['counter'];
\DB::transaction(function() use ($counter, $request) {
for ($i = 0; $i < count($counter); $i++) {
Sale::create([
'terminal_id' => $request["terminal_id"],
'customer_name' => $request["customer_name"],
'unit_price' => $request["unit_price"][$i],
'total_price' => $request["total_price"][$i],
'tax' => $request["tax"][$i],
'quantity' => $request["quantity"][$i],
'product_name' => $request["product_name"][$i],
'sale_date' => $request["sale_date"],
'rowId' => $request["rowId"][$i]
]);
}
});
NOTE
Not all input fields are array (terminal_id, sale_date...)
I am new to the framework and I have a question.
I made a login authentication to access and when I go to my profile page I can view my data and change all on the same page.
I need to go get my ID to be able to do the update or not needed?
What do I need to do?
The way I'm doing okay? I only need to create a route::post?
my profile page:
#if(Session::has('message'))
<div class="alert alert-danger">
<h5>{{ Session::get('message') }}</h5>
</div>
#endif
{!! Form::open(array('url' => 'backend/perfil', 'name' => 'updateProfile', 'role' => 'form'))!!}
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('name', 'Utilizador', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::text('name', null, ['class' => 'form-control input-md', 'placeholder' => 'Utilizador']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('nascimento', 'Data de nascimento', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::date('nascimento', null, ['class' => 'form-control input-md']) !!}
{{ $errors->first('nascimento', '<span class=error>:message</span>') }}
</div>
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('sexo', 'Sexo', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::select('sexo', ['Masculino', 'Feminino'], null, ['class' => 'form-control input-md']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('email', 'Email', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::text('email', null, ['class' => 'form-control input-md', 'placeholder' => 'Email']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('password', 'Password', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::password('password', ['class' => 'form-control input-md', 'placeholder' => 'Password']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('rpassword', 'Confirmar password', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::password('rpassword', ['class' => 'form-control input-md', 'placeholder' => 'Confirmar password']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-2 col-lg-2">
{!! Form::label('imagem', 'Imagem', ['class' => 'label_perfil']) !!}
</div>
<div class="col-md-5 col-lg-5">
{!! Form::file('imagem', ['class' => 'input-file']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 20px; margin-top: 30px;">
<div class="col-md-3 col-lg-3"></div>
<div class="col-md-9 col-lg-9">
{!! Form::submit('Enviar', ['class' => 'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
My controller:
public function perfil() {
return view('backend/perfil.index');
}
public function updateProfile() {
$profileData = Input::except('_token');
$validation = Validator::make($profileData, User::$profileData);
if ($validation->passes()) {
User::where('id', Input::get('id'))->update($profileData);
return view('backend/perfil.index')->with('message', 'Updated Succesfully');
} else {
return view('backend/perfil.index')->with('message', $validation->messages());
}
}
My route:
Route::get('backend/perfil','BackendControlador#perfil');
Route::post('backend/perfil', 'BackendControlador#updateProfile');
My app User:
public static $profileData = array(
'email' => 'required|email',
'name' => 'required',
);
Here is the Detailed one what you wanted to do.
Step 1 : Open the Form
{!! Form::open(array('url' => 'updateProfile', 'name' => 'updateProfile', 'role' => 'form'))!!}
Note : Your form method action is empty. You shall view your source to see it
Step 2 : Write a route
Route::post('updateProfile', 'homeController#updateProfile');
It will call the homeController's updateProfile function
Step 3 : Define the Controller, validate the input and make your action done via model
Here's the simple/sample function for you
public function updateProfile()
{
$profileData = Input::except('_token');
$validation = Validator::make($profileData, User::$profileData);
if ($validation->passes()) {
User::where('id', Input::get('id'))->update($profileData);
return view('profile')->with('message', 'Updated Succesfully');
} else {
return view('profile')->with('message', $validation->messages());
}
}
What it does is it will get all the inputs except _token and store it in $profileData, then it will make a validation that is defined inside $profileData inside the User Model
Here is the Validator, you shall modify it
public static $profileData = array(
'email' => 'required|email',
'name' => 'required',
);
Step 4 : Return the Result
If the validation is passed, then it will update in the table to where the user whose id is passed i.e., Input::get('id') , and we will return the page with return view('profile')->with('message', 'Updated Succesfully');
I consider your page name as profile.blade.php and you shall change it according to your blade,
If the validation is failed, then we will return the page with error messages
return view('profile')->with('message', $validation->messages());
You should have this in your blade to display your error messages
#if(Session::has('message'))
<div class="alert alert-danger">
<h5>{{ Session::get('message') }}</h5>
</div>
#endif
Note :
If you don't want to refresh the page, then you shall just do an Ajax call to pass the variables and show/hide the results that is return by the Controller
Hope this helps you
I have created 2 separate forms. One for signin and one for signup. They work fine on separate pages but if they are on the same page they print each others error messages. I'm guess its because they both contain the same input names.
They have separate controller methods though. Here is the example setup.
Signup form
{{ Form::open(['route' => 'signup']) }}
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
{{ $errors->first('email', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::label('password','Paswword') }}
{{ Form::password('password', ['class' => 'form-control']) }}
<p class="help-block">Password needs to be between 6 - 8 characters</p>
{{ $errors->first('password', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::submit('Sign up', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Login form
{{ Form::open(['route' => 'login']) }}
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
{{ $errors->first('email', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::label('password','Paswword') }}
{{ Form::password('password', ['class' => 'form-control']) }}
{{ $errors->first('password', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::submit('Login', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
routes.php
Route::get('/signup', [
'as' => 'signup',
'uses' => 'UsersController#getSignup'
]);
Route::post('/signup', [
'as' => 'signup',
'uses' => 'UsersController#postSignup'
]);
Just wondering if anyone else has come across this issue and how to solve it.
Thanks
http://laravel.com/docs/4.2/validation#error-messages-and-views
Named Error Bags
If you have multiple forms on a single page, you may wish to name the MessageBag of errors. This will allow you to retrieve the error messages for a specific form. Simply pass a name as the second argument to withErrors:
return Redirect::to('register')->withErrors($validator, 'login');
You may then access the named MessageBag instance from the $errors variable:
<?php echo $errors->login->first('email'); ?>