laravel 5.2传递id with onclick不能正常删除()

时间:2017-06-22 09:27:24

标签: php ajax laravel onclick

为什么我无法传递带有id的数据,当我在delete()上执行代码时为null 我错过了一些帮助我的东西:( 这是我的dataController

<?php

namespace App\Http\Controllers;
use App\topic; //use model topic
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables; //datatable
use App\Http\Requests;
use Carbon\Carbon;

class dataController extends Controller
{
    public function listPerintah($list,Request $request)
    {
        switch ($list) {
            case 'delete':
                $id = $request['id'];
                $topic = topic::find($id);
                $topic->delete();
                return $topic->name.'Berhasil Dihapus'; 
                break;

            default:
                return view('datatable.index');
                break;
        }
    }

    //dataTable
    public function listData(Request $request)
    {   
        $table = topic::select(['topics.*', 'users.name as user_name'])
            ->join('users', 'users.id', '=', 'topics.user_id')
            ->get();

        $datatables = Datatables::of($table);
        if($keyword = $request->get('search')['value'])
        {
            $datatables->filterColumn('rownum', 'whereRaw', '@rownum + 1 like ?', ["%{$keyword}%"]);
        }
        return $datatables
                ->editColumn('user_name', function($table) {
                    return 'Oleh: '. $table->user_name;
                })
                ->editColumn('created_at', function($table) {
                    return Carbon::parse($table->created_at)->format('d F Y H:i:s');
                })
                ->addColumn('action',function($table){
                    return
                    '<a title="hapus" href="javascript:" onclick="deleteBtn('.$table->id.',\''.$table->title.'\')" class="btn btn-fill btn-xs btn-danger"><span class="fa fa-remove"></span></a>
                    <a title="ubah" href="'.url("datatable/edit?id=".$table->id).'"  
                    class="btn btn-xs btn-primary"><span class="fa fa-pencil"></span>
                    </a>' ;
                })
                ->make(true);
    }


}

1 个答案:

答案 0 :(得分:0)

尝试使用$request->input('id');代替$request['id']

获取请求数据

$request->all()这用于获取所有传递的输入

如果你想从参数

获取id

like delete / {id} use可以从函数参数

中获取id
class dataController extends Controller
{
    public function listPerintah($list,Request $request,$id)