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

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.

Related

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.

Export to excel a table from laravel

I'm so confused why i can't export this to excel. Please help me. Theres an error where it can't read "all".
Here's my code in my UsersController
public function userExport()
{
Excel::create('data_function',function($excel){
$excel->sheet('mysheet', function($sheet){
$sheet->loadView('user.list');
});
})->download('xls');
}
Then here is it in my user/list.blade
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Employees
<small>#lang('app.list_of_registered_users') - {{ $all }}</small>
</h1>
</div>
</div>
#include('partials.messages')
<form method="GET" action="" accept-charset="UTF-8" id="users-form">
<div class="row tab-search">
<div class="col-md-2">
<div class="form-group-sm">
<select class="form-control form-control-sm" id="company" name="company">
<option selected disabled>Company</option>
#foreach ($companies as $company)
#if (($company->id)=="1")
<option>All</option>
#else
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endif
#endforeach
</select>
</div>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-warning btn-sm btn-block" id="filter-user">
<i class="glyphicon glyphicon-filter"></i>
Filter Employee
</button>
</div>
<div class="col-md-1">
<a href="{{ route('user.export') }}" class="btn btn-sm btn-success btn-block">
<i class="fa fa-file-excel-o"></i>
Excel
</a>
</div>
<div class="col-md-2">
<a href="{{ route('user.create') }}" class="btn btn-primary btn-sm btn-block" id="add-user">
<i class="glyphicon glyphicon-plus"></i>
Add
Employee
</a>
</div>
</div>
<div class="row tab-search"> <div class="col-md-2">
<div class="form-group-sm">
<select class="form-control form-control-sm" id="benefit" name="benefit">
<option selected disabled>Benefits</option>
#foreach ($benefits as $benefit)
#if (($benefit->id)=="1")
<option>All</option>
#else
<option value="{{ $benefit->id }}">{{ $benefit->name }}</option>
#endif
#endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group-sm">
<div class="input-group custom-search-form-sm">
<input type="text" class="form-control form-control-sm" name="search" value="{{ Input::get('search') }}" placeholder="Search Name">
<span class="input-group-btn">
<button class="btn btn-default btn-sm" type="submit" id="search-users-btn">
<span class="glyphicon glyphicon-search"></span>
</button>
#if (Input::has('search') && Input::get('search') != '')
<a href="{{ route('user.list') }}" class="btn btn-danger btn-sm" type="button" >
<span class="glyphicon glyphicon-remove"></span>
</a>
#endif
</span>
</div>
</div>
</div>
</div>
</div>
</form>
<div class="table-responsive" id="users-table-wrapper">
<table class="table table-bordered table-striped table-hover table-condensed" id="datatable-default">
<thead>
<th>#lang('app.full_name')</th>
<th>Sex<i type="button" class="fa fa-fw fa-sort"></th>
<th>Birthday<button name="birthday_sort" style="background-color:#ffffff"><i class="fa fa-fw fa-sort"></button></th>
<th>Age<i class="fa fa-fw fa-sort"></th>
<th>Civil<br>Status<i class="fa fa-fw fa-sort"></th>
<th>Company<i class="fa fa-fw fa-sort"></th>
<th>Department<i class="fa fa-fw fa-sort"></th>
<th>Employee<br>Status<i class="fa fa-fw fa-sort"></th>
<th>Level<i class="fa fa-fw fa-sort"></th>
<th>Date<br>Hired<i class="fa fa-fw fa-sort"></th>
<th>Tenure</th>
</thead>
<tbody>
#if (count($users))
#foreach ($users as $user)
<tr class="gradeX">
<td>{{ $user->first_name . ' ' . $user->middle_name . ' ' . $user->last_name }}</td>
<td>{{ $user->gender[0] }}</td>
<td>{{ $user->birthday->format('M j/y') }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->civil_status }}</td>
<td>#foreach ($user->company as $company)#endforeach{{ $company->name }}</td>
<td>#foreach ($user->department as $department)#endforeach{{ $department->name }}</td>
<td>{{ $user->emp_status }}</td>
<td>{{ $user->level }}</td>
<td>{{ $user->date_hired }}</td>
<td>{{ $user->difference }}</td>
</tr>
#endforeach
#else
<tr class="gradeX">
<td colspan="6"><em>#lang('app.no_records_found')</em></td>
</tr>
#endif
</tbody>
</table>
{!! $users->render() !!}
</div>
The problem is that it says an error like "all","companies", and etc are undefined variables. Also because the companies is another table. How can define it in my excel that it won't be error again?
It seems you need to pass variables to view from controller which you are passing. Please check my code and update.
public function userExport()
{
$companies = $this->companyModel->get();
$all = $this->something->get();
Excel::create('data_function',function($excel) use($all, $companies) {
$excel->sheet('mysheet', function($sheet) use($all, $companies) {
$sheet->loadView('user.list', ['all' => $all, 'companies' => $companies]);
});
})->download('xls');
}
and also pass the other variables with same as I passed to $sheet->loadView() in example.
I think this will help you.

Laravel 5.4 display another button renewal if created_time is more than one year

I am new to laravel and I want to pass a data to display a new button for renewal of application when the user application is already more than one year. But if the application is not more than one year it will not display the renew button. I tried using blade #if else statement in the datatable but it does not display the information when I tried to use #if else statement using blade and it says:
DataTables warning: table id=dataTableBuilder - Ajax error. For more
information about this error, please see http://datatables.net/tn/7
Here is the code in my
datatables_actions.blade.php
{!! Form::open(['route' => ['applicant.applications.destroy', $application_number], 'method' => 'delete']) !!}
<div class='btn-group'>
<a href="{{ route('applicant.applications.show', $application_number) }}" class='btn btn-default btn-xs'>
<i class="glyphicon glyphicon-eye-open"></i>
</a>
#if ($application->created_at->gt(\Carbon\Carbon::now()->addYear()))
<a href="{{ route('applicant.applications.renew', $application_number) }}" class="btn btn-primary btn-xs">
</a>
#endif
<!-- <a href="{{ route('applicant.applications.edit', $application_number) }}" class='btn btn-default btn-xs'>
<i class="glyphicon glyphicon-edit"></i>
</a> -->
<!-- {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', [
'type' => 'submit',
'class' => 'btn btn-danger btn-xs',
'onclick' => "return confirm('Are you sure?')"
]) !!} -->
</div>
{!! Form::close() !!}
You are missing </a> closing tag
Try with a different comparation:
{!! Form::open(['route' => ['applicant.applications.destroy', $application_number], 'method' => 'delete']) !!}
<div class='btn-group'>
<a href="{{ route('applicant.applications.show', $application_number) }}" class='btn btn-default btn-xs'>
<i class="glyphicon glyphicon-eye-open"></i>
</a>
#if ($application->created_at->diffInYears(\Carbon\Carbon::now())>=1)
</a>
#endif
<!-- <a href="{{ route('applicant.applications.edit', $application_number) }}" class='btn btn-default btn-xs'>
<i class="glyphicon glyphicon-edit"></i>
</a> -->
<!-- {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', [
'type' => 'submit',
'class' => 'btn btn-danger btn-xs',
'onclick' => "return confirm('Are you sure?')"
]) !!} -->
</div>
{!! Form::close() !!}

Roles and Responsibilites in laravel

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>

Laravel 5 Delete existing Article with destroy method

I am trying to delete an article with the following code:
ArticlesController:
public function destroy($id) {
$article = Article::findOrFail($id);
$article->delete();
return redirect('backend/dashboard')->with([
'flash_message' => 'Deleted',
'flash_message_important' => false
]);
}
View:
#foreach($articles as $key => $article)
<tr>
<td class="td-actions text-right">
<a href="{{action('ArticlesController#edit',$article->id)}}"type="button" rel="tooltip" title="" class="btn btn-info btn-simple btn-xs" data-original-title="Edit Article">
<i class="fa fa-edit"></i>
</a>
<a href="{{action('ArticlesController#destroy',$article->id)}}" type="button" rel="tooltip" title="" class="btn btn-danger btn-simple btn-xs" data-original-title="Delete Article">
<i class="fa fa-times"></i>
</a>
</td>
</tr>
#endforech
By clicking the "Delete Article" Button I am redirected to a total different view. It seems like the #show method is executed.
My Routes:
Route::get('backend/articles/archive', 'ArticlesController#archive');
Route::resource('backend/articles', 'ArticlesController');
Route::get('backend/dashboard', [
'middleware' => 'auth',
'uses' => 'PagesController#dashboard'
]);
How can I fix this?
The reason is because you are using a tag.
Use the form tag with method equals to delete which will solve your problem.
#foreach($articles as $key => $article)
<tr>
<td class="td-actions text-right">
<a href="{{action('ArticlesController#edit',$article->id)}}"type="button" rel="tooltip" title="" class="btn btn-info btn-simple btn-xs" data-original-title="Edit Article">
<i class="fa fa-edit"></i>
</a>
{{ Form::open([ 'method' => 'delete', 'route' => [ 'items.destroy', $item->id ] ]) }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{{ Form::close() }}
</td>
</tr>
#endforech
Try to do like this
public function destroy($id) {
$article = Article::findOrFail($id);
$article->delete();
return view('dashboard')->with([
'flash_message' => 'Deleted',
'flash_message_important' => false
]);
}
You need to use delete method to call destroy() action, for example:
{!! Form::open(['method' => 'Delete', 'route' => ['article.destroy', $id]]) !!}
<button type="submit" class="btn">Delete article</button>
{!! Form::close() !!}
You can't use a href link here.
You are not passing the variables correctly.
$url = action('UserController#profile', ['id' => 1]);
Source: https://laravel.com/docs/5.3/helpers#method-action

Categories