I have a quiz app and when I press add question button the error Undefined variable: question $question is undefined. Make the variable optional in the blade template. Replace {{ $question }} with {{ $question ?? '' }} occurs. The error appear for line 13 and 17:
<div class="form-group">
{!! Form::label('title', 'Title'); !!}
{{ Form::text('title', null, ['class' => 'form-control input-editor', 'placeholder' => 'Title']) }}
</div>
<div class="form-group">
{!! Form::label('category_id', 'Category'); !!}
<select class="form-control" id="category_id" name="category_id">
<option selected="selected" value="">---Pick a Category---</option>
#foreach($categories as $key => $cat)
#if(!empty($child_categories[$key]))
<optgroup label="{{ $cat }}">
#foreach($child_categories[$key] as $ckey => $ccat)
<option value="{{ $ckey }}" {{ $question->category_id == $ckey ? 'selected' : '' }}>{{ $ccat }}</option>
#endforeach
</optgroup>
#else
<option value="{{ $key }}" {{ $question->category_id == $key ? 'selected' : '' }}>{{ $cat }}</option>
#endif
#endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('question_type', 'Question Type'); !!}
{!! Form::select('question_type', ['regular' => 'Regular Question', 'photo' => 'Photo Question'], null, ['id' => 'question_type', 'class' => 'form-control']); !!}
</div>
#if($btntitle == 'Update Question')
<div class="alert alert-success question_image" style="{{ isset($question) && $question->question_type == 'photo' ? 'display: block;' : 'display: none;' }}">
Upload image if you want to ovewrite existing one(if any), otherwise leave that field blank
</div>
#endif
Related
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.
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..
I have nested foreach in my blade in laravel but all records compact twice in my select option here is my code:
#foreach($user as $user)
#foreach($user_renter as $last_user_renter)
#if($user->id == $last_user_renter->user_id)
<option value="{{ $user->id }}" selected>
{{ $user->name }} {{ $user->family }} - {{ $user->email }}
</option>
#else
<option value="{{ $user->id }}">
{{ $user->name }} {{ $user->family }} - {{ $user->email }}
</option>
#endif
#endforeach
#endforeach
now is there any way to compact them once?
Try to change the user_renter as array as given bellow
$user_renter = array(
array(
'name' => 'flash',
'id' => 1
),
array(
'name' => 'zoom',
'id' => 2
),
array(
'name' => 'snart',
'id' => 3
)
);
And
#foreach($users as $user)
<?php $key = array_search($user->id, array_column($user_renter, 'id')); ?>
<option value="{{ $user->id }}" #if(is_int($key)) selected #endif>
{{ $user->name }} {{ $user->family }} - {{ $user->email }}
</option>
#endforeach
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
I am trying to validate a form in Laravel 5.3. The form has checkboxes. I need at least one checkbox to be selected for the form to be vald. This is my form
Here is my form
<div class="form-group {{ $errors->has('gender') ? 'has-error' : ''}}">
<div class="col-md-2"></div>
<div class="col-md-10">
<label for="gender_" class="checkbox-inline">
{!! Form::checkbox('gender', '', null, ['id' => 'gender_']) !!}
{{ trans('blogs.gender_') }}
</label>
<label for="gender_1" class="checkbox-inline">
{!! Form::checkbox('gender', '1', null, ['id' => 'gender_1']) !!}
{{ trans('blogs.gender_1') }}
</label>
<label for="gender_2" class="checkbox-inline">
{!! Form::checkbox('gender', '2', null, ['id' => 'gender_2']) !!}
Female
</label>
{!! $errors->first('gender', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
{!! Form::label('name',trans('blogs.name'),['class' => 'col-md-2 control-label']) !!}
<div class="col-md-10">
{!! Form::text('name',null, ['class' => 'form-control']) !!}
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
I am using FormRequest object. Here is my rules() method
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'gender' => 'required'
];
}
However, the validation fails unless I select every checkbox!
How can I correctly ensure that the validation only fails if no checkboxes is selected?
Your rules() looks correct so no require change for it. Try below:
<div class="form-group {{ $errors->has('gender') ? 'has-error' : ''}}">
<div class="col-md-2"></div>
<div class="col-md-10">
<label for="gender_0" class="checkbox-inline">
{!! Form::checkbox('gender[]', 0, null, ['id' => 'gender_0']) !!}
{{ trans('blogs.gender_') }}
</label>
<label for="gender_1" class="checkbox-inline">
{!! Form::checkbox('gender[]', 1, null, ['id' => 'gender_1']) !!}
{{ trans('blogs.gender_1') }}
</label>
<label for="gender_2" class="checkbox-inline">
{!! Form::checkbox('gender[]', 2, null, ['id' => 'gender_2']) !!}
Female
</label>
{!! $errors->first('gender', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
{!! Form::label('name',trans('blogs.name'),['class' => 'col-md-2 control-label']) !!}
<div class="col-md-10">
{!! Form::text('name',null, ['class' => 'form-control']) !!}
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
Hope this could help you!