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
Related
So this is my code for delete action in my webpage
{!!Form::open(['action' => ['PostsController#destroy', $post->id],'method'=>'POST', 'class' => 'float-right'])!!}
{{Form::hidden('_method','DELETE')}}
{{Form::submit('Delete',['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
now I want to put this <i class="fas fa-trash-alt"></i> font awesome icon in that button before the 'delete' text
So, how do I get this thing to happen, Help would be much appreciated.
Do manually using:
<button type="submit" class="btn btn-danger">
<i class="fas fa-trash-alt"></i> Delete
</button>
Or in Blade use:
{!! Form::button( '<i class="fas fa-trash-alt"></i> Delete', ['class'=>'btn btn-danger','type'=>'submit'] ) !!}
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() !!}
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>
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.
I need to have font awesome image on button label. So i cannot use <input type="submit">
<button type="submit" class="btn btn-success">
<i class="fa-trash"></i> delete
</button>
View on laravel
{{ Form::open(array('method'
=> 'DELETE', 'route' => array('admin.states.destroy', $value->id))) }}
{{ Form::submit(HTML::decode(FA::icon('trash')), array('class'
=> 'actions-line icon-trash','onclick'=>'return confirm("Are you sure?")')) }}
{{ Form::close() }}
This is not rendering html inside button
What is the proper way?
Something like
Form::button('<i class="fa fa-star"></i> Submit', array(
'type' => 'submit',
'class'=> 'actions-line icon-trash',
'onclick'=>'return confirm("Are you sure?")'
));
Tailor to your taste. The only thing to note is that the type is set to submit