使用Ajax Php laravel 5.3检索数据

时间:2017-01-02 10:07:08

标签: ajax laravel-5.3

根据我的情况,当我选择一位名为#slmc的医生时,我想检索与我选择的医生相关的数据。

这是我的刀片文件 - >管理员/ channel.blade

  <label id="lslmc" for="slmc" class="control-label">Choose a Doctor </label><br />
  <select class="form-control" id="slmc" name="slmc" onchange=" getPatient()" >
    <option value="">----Select the Doctor----</option>
    @foreach($Doctors as $Doctor)
      <option value ="{{$Doctor-> slmc}}">{{$Doctor->fname}} {{$Doctor->sname}}  </option>
    @endforeach
  </select>
</div>

<div id ="dd"></div>

然后这是控制器中的getPatient()函数

public function getPatient($slmc){
        $patients = DB::table('channel')->where('slmc', $slmc)->get();
        return $patients;
}

这是脚本函数中的Ajax调用。

function getPatient() {

    var patient = $("#slmc").val();

    $.ajax(
        {
            type: 'get',
            url: ('/Admin/channel/getPatient'),
            data: {'patients': patient},
            success: function (data) {
                $('#dd').html("");
                $('#dd').html(data);
            }
        }
    );
}

这是web.php

Route::get('/Admin/channel/getPatient{slmc}', 'channelController@getPatient' );

我的Ajax调用有问题吗?我是新手。

2 个答案:

答案 0 :(得分:1)

路线:

Route::get('/Admin/channel/getPatient', 'channelController@getPatient' );

控制器:

ChannelController.php :

use Illuminate\Http\Request;
class ChannelController extends Controller
{

    public function getPatient(Request $request){
        $patients = \DB::table('channel')->where('slmc', $request->patients)->get();
        return $patients;
    }
}  

答案 1 :(得分:0)

我能够做到 ajax函数就像这样

enter code here函数getPatient(){

var patient = $("#slmc").val();

$.ajax(
    {
        type: 'get',
        url: ('/Admin/channel/getPatient'),
        data: {'slmc': patient},
        success: function (data) {
            $('#dd').html("");
            var divHtml = "";
            divHtml += "<table class='table table-bordered table-hover'>";
            divHtml += "<tr>";
            divHtml += "<td>" + data[0].pname + "</td>";
            divHtml += "<td>" + data[0].address + "</td>";
            divHtml += "</tr>";
            divHtml += "</table>";
            $('#dd').html(divHtml);
            console.info(data);
        },
        error: function (data) {
            console.error(data);
        }
    }
);

}

我的控制器功能是

public function getPatient(Request $request){
        $slmc = $request->get('slmc');
        $patients = \DB::table('channel')->where('slmc', $slmc)->get();
        return $patients;
    }

web.php

Route::get('/Admin/channel/{slmc}', 'channelController@getPatient' );