Roles and Responsibilites in laravel - php

i am creating the roles and responsibilities the code has to be followed:
public function boot()
{
view()->composer('admin.sidebar', function ($view) {
$moduleRs = DB::table('users')
->join('permissions','users.role', '=', 'permissions.role_id')
->select('users.role as usersrole','permissions.role_id as role_id','permissions.module_name as module_name')
->where('users.role', '=', \Auth::user()->role)
->get();
// dd($moduleRs[0]->module_name);
$moduleData = null;
if ( count($moduleRs) > 0 ) {
$result = [];
foreach($moduleRs as $row){
if(!isset($result[$row->role_id])) {
$result[$row->role_id] = array(
'role_id' => $row->role_id,
'module_name' => [$row->module_name],
);
}else{
$result[$row->role_id]['module_name'] = array_merge($result[$row->role_id]['module_name'], [$row->module_name] );
}
}
$data=$moduleRs[0]->module_name;
dd($data);
$splittedstring[]=explode(" ,",$data);
//dd($splittedstring);
foreach ($splittedstring as $key => $value) {
echo " '.$value.'<br>";
}
}
$view->with('moduleData', $moduleData);
});
}
In the above code when i can use dd($data) it can display the id of permissions of the user.But when hide the dd($data) means it couldn't make the result as well.Please provide an idea.
when i can hide dd(...) it shows like this found above screen shots..
when i put dd($splittedstring) means it shows this found below
array:3 [▼
0 => "0"
1 => "4"
2 => "5"
]
This is module found at the below cases of screen shots i can store this module in modules table field name of module
[![enter image description here][2]][2]
this is the permission form found below which has to fetch the module name from modules table and store this value to permissions table field name of module_name but it is a integer not a string this is a foreign key..
[![enter image description here][3]][3]
and shows in a view display as like as follows..Here Role 1=>Super Admin 0=>Admin... i can make permission admin as 0=>Dashboard,4=>Employee,5=>Lead..when i login as a admin i can view only Dashboard,Employee and Lead only
Permission's create.blade.php
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">Create New Permission</div>
<div class="panel-body">
<button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button>
<br />
<br />
#if ($errors->any())
<ul class="alert alert-danger">
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
{!! Form::open(['url' => '/permission/permission', 'class' => 'form-horizontal', 'files' => true]) !!}
#include ('permission.permission.form')
{!! Form::close() !!}
</div>
</div>
</div>
Permission's form.blade.php
<div class="form-group {{ $errors->has('role_id') ? 'has-error' : ''}}">
{!! Form::label('role_id', 'Role', ['class' => 'col-md-4 control-label'])
!!}
<div class="col-md-6">
{!! Form::select('role_id',($role),null,['class' => 'form-control']) !!}
{!! $errors->first('role_id', '<p class="help-block">:message</p>') !!}
</div>
</div><div class="form-group {{ $errors->has('module_name') ? 'has-error': ''}}">
{!! Form::label('module_name', 'Module', ['class' => 'col-md-4 control-
label']) !!}
<div class="col-md-6">
{!! Form::select('module_name[]',($module), null, ['class' => 'form-
control','multiple' => true]) !!}
{!! $errors->first('module_name', '<p class="help-block">:message</p>')
!!}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-4">
{!! Form::submit(isset($submitButtonText) ? $submitButtonText :
'Create', ['class' => 'btn btn-primary']) !!}
</div>
</div>
permissionController.php store and create function
public function create()
{
$role = \DB::table('roles')->pluck('name');
$module = \DB::table('modules')->pluck('module');
return view('permission.permission.create',array('role'=>$role,'module'=>$module));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
*
* #return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(Request $request)
{
$requestData = $request->all();
$news = $request->input('module_name');
$news = implode(',', $news);
$input = $request->except('module_name');
//Assign the "mutated" news value to $input
$input['module_name'] = $news;
Permission::create($input);
Session::flash('flash_message', 'Permission added!');
return redirect('permission/permission');
}
public function show($id)
{
$permission = Permission::findOrFail($id);
return view('permission.permission.show', compact('permission'));
}
show.blade.php
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">Permission {{ $permission->id }}</div>
<div class="panel-body">
<button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button>
<button class="btn btn-primary btn-xs"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button>
{!! Form::open([
'method'=>'DELETE',
'url' => ['permission/permission', $permission->id],
'style' => 'display:inline'
]) !!}
{!! Form::button('<i class="fa fa-trash-o" aria-hidden="true"></i> Delete', array(
'type' => 'submit',
'class' => 'btn btn-danger btn-xs',
'title' => 'Delete Permission',
'onclick'=>'return confirm("Confirm delete?")'
))!!}
{!! Form::close() !!}
<br/>
<br/>
<div class="table-responsive">
<table class="table table-borderless">
<tbody>
<tr>
<th>ID</th><td>{{ $permission->id }}</td>
</tr>
<tr><th> Role </th><td> {{ $permission->role_id }} </td></tr><tr><th> Module </th><td> {{ $permission->module_name }} </td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>

You should change create function in permissioncontroller like:
permissionController.php
public function create()
{
$role = \DB::table('roles')->pluck('name');
$module = \DB::table('modules')->get();
$arrModule = array();
foreach($module as $modules){
$arrModule[$modules->id] = $modules->module;
}
return view('permission.permission.create',array('role'=>$role,'module'=>$arrModule));
}
Updated Answer
public function show($id)
{
$permission = Permission::findOrFail($id);
$permissionModule =explode(",",$permission->module);
$module = \DB::table('modules')->get();
$arrPermissionModule = array();
foreach($module as $modules){
if(in_array($modules->id,$permissionModule)){
$arrPermissionModule[$modules->id] = $modules->module;
}
}
return view('permission.permission.show', compact('permission','arrPermissionModule'));
}
Show.blade.php
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">Permission {{ $permission->id }}</div>
<div class="panel-body">
<button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button>
<button class="btn btn-primary btn-xs"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button>
{!! Form::open([
'method'=>'DELETE',
'url' => ['permission/permission', $permission->id],
'style' => 'display:inline'
]) !!}
{!! Form::button('<i class="fa fa-trash-o" aria-hidden="true"></i> Delete', array(
'type' => 'submit',
'class' => 'btn btn-danger btn-xs',
'title' => 'Delete Permission',
'onclick'=>'return confirm("Confirm delete?")'
))!!}
{!! Form::close() !!}
<br/>
<br/>
<div class="table-responsive">
<table class="table table-borderless">
<tbody>
<tr>
<th>ID</th><td>{{ $permission->id }}</td>
</tr>
<tr><th> Role </th><td> {{ $permission->role_id }} </td></tr>
<tr><th> Module </th>
<td>
#foreach($arrPermissionModule as $arrPermissionModules)
{{ $arrPermissionModules }}
#endforeach
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>

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

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

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.

Undefined property: Illuminate\Support\Collection::$id in Laravel 5.2

I have a confusion and I need a help to solve.
I am trying to add a record to a table that depends on the initial record, it is an event log table related to the initial registration of a call record.
As I try to do this it is with an input hidden, but it does not receive the id and it generates the following error (Undefined property:Illuminate\Support\Collection::$id).
This is part of the view with the record button of the incidences.
#foreach ($data as $call)
<tr class="active">
<td align="center">{{ ++$i }}</td>
<td style="text-align: center">{{ $call->created_at->format('d - m - Y') }}</td>
<td>{{ $call->name }}</td>
<td>{{ $call->last_name }}</td>
<td align="center">
#if($call->type == 1)
<span class="label label-info">Saliente</span>
#else
<span class="label label-success">Entrante</span>
#endif
</td>
<td>{{ $call->phone }}</td>
<td>{{ $call->movil }}</td>
<td align="center">
<a class="btn btn-info btn-xs" href="{{ route('calls.show',$call->id) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Detalle de llamada"> <i class="material-icons">info_outline</i> </a>
#permission('role-edit')
<a class="btn btn-primary btn-xs" href="{{ route('calls.edit',$call->id) }}" data-toggle="tooltip" data-placement="top" title="Editar registro de llamada"> <i class="material-icons">create</i> </a>
#endpermission
<a class="btn btn-warning btn-xs" href="{{ route('comments.create', $call->id) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Registrar incidencia"> <i class="material-icons">event</i> </a>
{!! Form::open(['method' => 'DELETE','route' => ['calls.destroy', $call->id],'style'=>'display:inline']) !!}
#permission('role-delete')
<button type="submit" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="top" title="Eliminar llamada"><i class="material-icons delete-white">delete</i></button>
#endpermission
{!! Form::close() !!}
</td>
</tr>
#endforeach
This is part of the view where the issue is logged, but I need to pass the id in the hidden field.
{!! Form::open(array('route' => 'comments.store','method' => 'POST')) !!}
<div class="col-md-12 col-xs-12">
<div class="input-group">
<div class="col-md-4 col-xs-4">
{!! Form::select('call_id', $calls, null, ['class' => 'form-control', 'placeholder' => 'Seleccionar cliente']) !!} <!--This is the select-->
{{ Form::hidden('call_id', $calls->id) }} <!--This is the hidden mode-->
</div>
<div class="col-md-8 col-xs-8">
{!! Form::text('name', null, array('placeholder' => 'Registrar incidencia','class' => 'form-control')) !!}
</div>
<span class="input-group-btn">
<button type="submit" class="btn btn-success btn-xs" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Guardar">
<i class="material-icons">save</i>
</button>
</span>
</div>
</div>
{!! Form::close() !!}
The error generated by the view is:
Undefined property: Illuminate\Support\Collection::$id
These are my methods for the controller (CommentController).
public function create()
{
$calls = Call::orderBy('id', 'asc')->lists('name', 'id');
return view('comments.create', compact('calls'));
}
public function store(Request $request)
{
//return $request->all();
$this->validate($request, [
'name' => 'required|unique:categories|max:255',
]);
$comments = Comment::create([
'name' => $request->get('name'),
'call_id' => $request->get('call_id'),
]);
return redirect()->route('comments.index')
->with('success','Comentario agregado correctamente!!!');
}
This is my route method.
Route::resource('comments','CommentController');
This is the call log view, when clicking the orange button calls the comment view to record an issue.
This is the view of record of incidents, here I have the dropdown but ideally instead of a dropdown can receive the ID of the record you select from the previous view.
Someone who can guide me, since I have used several methods and I have not been able to solve it.
Solve the confusion and method as follows:
The button to create the incidence in the view I defined it as follows:
#foreach ($data as $call)
<a class="btn btn-warning btn-xs" href="{{ route('comments.create', ['id' => $call->id]) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Registrar incidencia"> <i class="material-icons">event</i> </a>
#endforeach
In the create method of CommentController I structured it as follows:
public function create($id)
{
$calls = DB::table('calls')->find($id);
return view('comments.create', compact('calls'));
}
The store method of CommentController I structured it as follows:
public function store(Request $request)
{
//return $request->all();
$rules = [
'call_id' => 'required',
'comments_name' => 'required',
];
$messages = [
'call_id.required' => 'Debe seleccionar un código de llamada',
'comments_name.required' => 'Debe ingresar incidencia',
];
$this->validate($request, $rules, $messages);
$comments = Comment::create([
'comments_name' => $request->get('comments_name'),
'call_id' => $request->get('call_id'),
]);
return redirect()->route('calls.index')
->with('success','Incidencia agregada correctamente!!!');
}
The labels in the view create incidents are as follows:
{!! Form::open(array('route' => 'comments.store','method' => 'POST')) !!}
<div class="col-md-12 col-xs-12">
<div class="input-group">
<div class="col-md-12 col-xs-12">
{!! Form::text('comments_name', null, array('placeholder' => 'Nombres','class' => 'form-control')) !!}
{{ Form::hidden('call_id', $calls->id) }}
</div>
<span class="input-group-btn">
<a class="btn btn-warning btn-xs" href="{{ route('calls.index') }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Retornar">
<i class="material-icons">arrow_back</i>
</a>
<button type="submit" class="btn btn-success btn-xs" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Guardar">
<i class="material-icons">save</i>
</button>
</span>
</div>
</div>
{!! Form::close() !!}
And I defined the following route in the routes file:
Route::get('comments/create/{id}', [
'middleware' => 'auth',
'as' => 'comments.create',
'uses' => 'CommentController#create'
]);
This way you can create a call log with a list of many incidents.
If this method should be improved please give me the recommendations, but it was the way in which I could do it.
Thanks #RobFonseca and #CarlosAdames for your comments and help.
The lists method returns a collection instance. You need to convert the Collection into a plain array using the all method.
In your controller try this:
$calls = Call::orderBy('id', 'asc')->lists('name', 'id')->all();
You can read more about it in the following link The lists Method
Let me know if your problem is solved.

How to update data from a form in Laravel 5.2

Hi I am having problems on editing and updating data from my form. I already tried to get the data from the form but when I try to edit and update it, it will make another data. Thanks!
Controller:
public function registerPackage()
{
$activity_packages = Chap_activity_packages::all();
return view('admin.registerPackage',compact('activity_packages'));
}
public function savePackage(Request $request)
{
$this->validate($request,[
'chap_activity_packages_name'=>'required|Min:4|unique:chap_activity_packa ges',
'chap_activity_packages_price'=>'required|numeric'
]);
$values = $request->all();
Chap_activity_packages::create($values);
return view('admin.registerPackage');
}
public function editPackage($id)
{
$act = Chap_activity_packages::find($id);
$activity_packages=Chap_activity_packages::all();
return view('admin.registerPackage',compact('act','activity_packages'));
}
<h4 class="page-header">Packages Management</h4>
<div class="row">
<div class="col-xs-5">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Register New Package</h3>
#if($errors->any())
<div class="alert alert-danger">
#foreach($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
</div>
#endif
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
</div>
<div class="panel-body">
{{ Form::open(array('url' => 'admin/registerPackage')) }}
<div class="form-group">
{{ Form::label('chap_name','Package Name:') }}
{{ Form::text('chap_activity_packages_name',isset($act)? $act->chap_activity_packages_name:null,['class' => 'form-control', 'placeholder' => 'Enter package name']) }}
</div>
<div class="form-group">
{{ Form::label('chap_price','Package Price:') }}
{{ Form::text('chap_activity_packages_price',isset($act)? $act->chap_activity_packages_price:null,['class' => 'form-control', 'placeholder' => 'Enter package price']) }}
</div>
<div class="form-group">
{{ Form::submit('Register Package',['class' => 'btn btn-success btn-block']) }}
</div>
{{ Form::close() }}
</div>
</div>
</div>
</div>
#stop
#section('content2')
<div class="panel panel-success">
<div class="panel-heading">
<h4 class="panel-title">Available Packages</h4>
</div>
<div class="panel-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Package Name</th>
<th>Package Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($activity_packages as $activity_package)
<tr>
<td> {{ $activity_package->chap_activity_packages_name }} </td>
<td> {{ $activity_package->chap_activity_packages_price }} </td>
<td>
</span>
<span class="glyphicon glyphicon-trash"></span>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#stop
These are my codes for both the controller and the view. please let me know if what is wrong. thanks a lot!
I don't see the method for updating data in you controller, it should be look like below:
// YourController.php, use implicit route model binding
public function update(App\Chap_activity_packages $model)
{
// do your validation here
// $this->validate(blahblahblah...)
// ...
$model->update(request()->all());
return redirect()->back()->with('msg', 'Update success.');
}
// routes.php
Route::put('your/route/to/model/{model}', 'YourController#update');
// yourTemplate.blade.php
// remember to use method spoofing
{{ Form::open(['url' => 'your/route/to/model/' . $model->id, 'method' => 'put']) }}
Information about route model binding can be found here. Good luck!

Categories