duanduo0520 2018-03-02 15:30
浏览 159

如何将数据传递给视图,并在laravel中返回response()以及ajax成功的数据

i have a grid in which all users are shown . the grid is created and shown by ajax , it has 3 buttons . add , edit , remove . i can add and delete , but since in the update.blade.php i have the user image ( which is shown by a route and controller like : <img src="{{ route('userimage',['id' => $pass_the_id ])}}"/> , the problem now is how do i pass variable like $pass_the_id that i'm about to use in the view blade ?` , this is the code that brings up the modal ( when edit button which is right in front of user name and id is clicked . i can get user name , family and so on and show it in the modal . but how do i pass the $id to the image route that i mentioned above ?

    $('body').delegate('#student-info #edit','click',function (e) {
    var id = $(this).data('id');
    $.get("{{ URL::to('student/edit') }}",{id:id},function (data) {
        $('#frm-update').find('#id').val(data.id);
        $('#frm-update').find('#name').val(data.name);
        $('#frm-update').find('#family').val(data.family);
        $('#frm-update').find('#username').val(data.username);
        $('#frm-update').find('#birth_date').val(data.birth_date);
        $('#frm-update').find('#phone').val(data.cell_phone);
        $('#frm-update').find('#email').val(data.email);
        $('#frm-update').find('#gender').val(data.gender);

        $('#student-update').modal('show');
    })
}) 

here is the laravel Controller function that is called on URL::to('student/edit') wich i have mentioned in the above $.get code :

    public function edit(Request $request) {
    if($request->ajax()) {
        $contact = User::find($request->id);
        return response($contact);
    }
}

the responsibility of above controller function which is called during $.get is to get the information based on the id that i have passed . ( my grid already shows each user id in the the grid table i've created . but i don't know how to pass it to the src="{{ route('userimage',['id' => $pass_the_id ])}}"

here is what userimage route is doing : bellow is the route :

    Route::get('userimage/{id}','ImageController@showimage')->name('userimage');

and this is its code :

    public function showimage($id) {
    if(UserImages::selectuserimage($id) !== null) {
        $imageData = UserImages::selectuserimage($id);
        $info = $info = base64_decode($imageData);
        $img = Imgs::make($info);
        $img->encode('jpg',80);
        return $img;
    }
    else return public_path('images/Atehran.jpg');
}
  • 写回答

1条回答 默认 最新

  • doulachan8217 2018-03-28 06:44
    关注

    On Ajax Success call to pass data to view with return response() in laravel this is the simple way with an example -

    js function

    $(document).on('change', '[name="abc_field"]', getCitiesByState);
    
     function yourAjaxCall() {
        var data_to_pass = $(`[name='get_data_selector']`).val();
        var $selector    = $(`[name='selector']`);
        $.ajax({
            url: route('your_route'),
            type: 'GET',
            data: {
                data: data_to_pass
            },
            dataType: "json",
            success: function success(data) {
                $selector.empty().append(data);
            },
        });
    }
    

    call to controller from ajax call

    public function your_route_function()
    {
        $result = Abc::where('column',request('data'))->get();
    
        return response()->json([
            'result' => view('viewfilename.fields._option', ['options' => $result])->render(),
        ]);
    }
    

    view path "resources\viwes\viewfilename\fields_option.blade" to render

    <option value="">{{ $defaultText or 'Select' }}</option>
    @foreach($options as $value => $name)
        <option value="{{ $value }}">{{ $name }}</option>
    @endforeach
    

    Hope this will help you to render view through ajax call.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)