Search code examples
datatableslaravel-5.1formhelper

Send form from controller to view using datatables in laravel 5.1


This is my code :

public function getRolesData()
    {
        $roles = Role::All();
        return Datatables::of($roles)      
            ->addColumn('action', function ($role) {
                return "{!! Form::open(array('method'=>'DELETE', 'route' => array('admin.role.destroy',".$role->id."))) !!}
                        {!! Form::submit('Delete', array('class'=>'btn btn-danger')) !!}
                        {!! Form::close() !!}
                        ";
                })            
                ->make(true);
    }

In 'action' column in view, I get the same code :

{!! Form::open(array('method'=>'DELETE', 'route' => array('admin.role.destroy',1))) !!} {!! Form::submit('Delete', array('class'=>'btn btn-danger')) !!} {!! Form::close() !!} 

No submit button is appeared ! what's the mistake in my code ?


Solution

  • In your example you're using blade sintax which won't work inside your controller as it's not a blade file.

    Try:

    return  \Form::open(array('method'=>'DELETE', 'route' => array('admin.role.destroy',".$role->id."))) .
            \Form::submit('Delete', array('class'=>'btn btn-danger')) .
            \Form::close();
    

    Alternatively, you could move the form in to a blade file

    e.g. views/admin/role/partials/datatables-form.blade.php (or wherever makes sense for your app) and just return that view file instead.

    i.e.

    return view('admin.role.partials.datatables-form', compact('role'))
    

    admin/role/partials/datatables-form.blade.php

    {!! Form::open(array('method'=>'DELETE', 'route' => array('admin.role.destroy',".$role->id."))) !!}
    {!! Form::submit('Delete', array('class'=>'btn btn-danger')) !!}
    {!! Form::close() !!}
    

    Hope this helps!