How to limit depth of reply comments in laravel - php

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..

Related

How to get saved value from dropdown in edit page laravel?

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.

how make modal edit with jquery in laravel?

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>

errors message laravel not work

i have some error in my validate data, the errors message not show in my blade file.Maybe you can help me
This is my code
tambah_jamaah.blade.php
{!! Form::open(['route' => 'jamaah.store']) !!}
#if (count($errors) > 0)
<div class="alert alert-danger">
Error :<br />
<ul>
#foreach ($errors->all() as $error)
<li></li>
#endforeach
</ul>
</div>
#endif
<div class="box-body">
<div class="form-group">
{!! Form::label('Name', 'Nama Jamaah') !!}
{!! Form::text('Name', null, ['class' => 'form-control',
'placeholder' => 'Masukan Nama Jamaah ...']) !!}
</div>
<div class="form-group">
{!! Form::label('Number', 'Nomor Telepon') !!}
{!! Form::number('Number', null, ['class' => 'form-control',
'placeholder' => 'Masukan Nomor Telepon ...']) !!}
</div>
<div class="form-group">
{!! Form::label('Birth', 'Tanggal Lahir') !!}
{!! Form::date('Birth', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('Wa', 'Nomor Whatsapp') !!}
{!! Form::number('Wa', null, ['class' => 'form-control',
'placeholder' => 'Masukan Nomor Whatsapp (Opsional) ...'])
!!}
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Simpan</button>
</div>
{!! Form::close() !!}
JamaahsController.php (store method)
This is my code in JamaahsController#store
public function store(Request $request)
{
//Validasi data
$this->validate($request, [
'Name' => 'required|max:255',
'Number' => 'required',
'Birth' => 'required'
]);
$pbk = new Pbk;
$pbk->Name = $request->Name;
$pbk->Number = $request->Number;
$pbk->Birth = $request->Birth;
$pbk->save();
return redirect()->route('jamaah.index');
}
Im not sure by adding some group(middleware) in my routes
routes.php
Route::group(['middleware' => ['web']], function() {
Route::resource('jamaah', 'JamaahsController');
Route::get('/', 'PagesController#getIndex');
});
Thanks
I think you need to update your blade file code like :
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Hope this work for you !!!
Official document recommends this code for validation errors. Right now you do not display any errors in your li items.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
For debugging I also suggest adding this code
public function store(Request $request)
{
//Validasi data
$this->validate($request, [
'Name' => 'required|max:255',
'Number' => 'required',
'Birth' => 'required'
]);
dd($request->all());
If dd outputs anything, then validation does not fail at all.
If validation fails then in your blade try this code. After posting with dd() in controller and then refresh the post without dd() in controller so you will see errors in blade.
{!! Form::open(['route' => 'jamaah.store']) !!}
{{dd($errors)}}
This shows you whats inside the $errors and if there are any errors found.

Insert Cart::content() into multiple Row From View Using Eloquent Laravel 5

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...)

Laravel 4 error when changing password

My Controller
public function update($id)
{
if (!is_numeric($id)) {
// #codeCoverageIgnoreStart
return \App::abort(404);
// #codeCoverageIgnoreEnd
}
$temp = Input::all();
$temp['password'] = Hash::make($temp['password']);
//form Processing
$result = $this->userForm->update($temp);
//$result = $this->userForm->update(Input::all());
if ($result['success']) {
// Success!
Session::flash('success', $result['message']);
return Redirect::action('UserController#index', array($id));
} else {
Session::flash('error', $result['message']);
return Redirect::action('UserController#edit', array($id))
->withInput()
->withErrors($this->userForm->errors());
}
}
My View
<div class="form-group {{ ($errors->has('firstName')) ? 'has-error' : '' }}" for="firstName">
{{ Form::label('edit_firstName', 'First Name', array('class' => 'col-sm-2 control-label')) }}
<div class="col-sm-10">
{{ Form::text('firstName', $user->first_name, array('class' => 'form-control', 'placeholder' => 'First
Name', 'id' => 'edit_firstName'))}}
</div>
{{ ($errors->has('firstName') ? $errors->first('firstName') : '') }}
</div>
<div class="form-group {{ ($errors->has('lastName')) ? 'has-error' : '' }}" for="lastName">
{{ Form::label('edit_lastName', 'Last Name', array('class' => 'col-sm-2 control-label')) }}
<div class="col-sm-10">
{{ Form::text('lastName', $user->last_name, array('class' => 'form-control', 'placeholder' => 'Last Name',
'id' => 'edit_lastName'))}}
</div>
{{ ($errors->has('lastName') ? $errors->first('lastName') : '') }}
</div>
#if (Sentry::getUser()->hasAccess('admin'))
<div class="form-group">
{{ Form::label('edit_memberships', 'Group Memberships', array('class' => 'col-sm-2 control-label'))}}
<div class="col-sm-10">
#foreach ($allGroups as $group)
<label class="checkbox-inline">
<input type="checkbox" name="groups[{{ $group->id }}]" value='1'
{{ (in_array($group->name, $userGroups) ? 'checked="checked"' : '') }} > {{ $group->name }}
</label>
#endforeach
</div>
</div>
<div class="form-group {{ ($errors->has('password')) ? 'has-error' : '' }}">
{{ Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) }}
{{ ($errors->has('password') ? $errors->first('password') : '') }}
</div>
#endif
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
{{ Form::hidden('id', $user->id) }}
{{ Form::submit('Submit Changes', array('class' => 'btn btn-primary'))}}
</div>
</div>
{{ Form::close()}}</div>
I'm trying to update data profile like; first name, last name, member and password.
I cannot update this password, my encryption password like:
$2y$10$mjkd5MRgUEn2JSK52xrrQ.bpDz5WZwAaHje6gd0TbmH7h4H3.7BBO

Categories