Didn"t forge 2018-03-28 13:43 采纳率: 25%
浏览 8

Laravel Post + Ajax

I'm having a issue with laravel, i have a SPA running and i need to do a form with search and filters by category.

I'm doing this via POST, but i need to run the query again with the filters, with ajax so the page don't reload.

But how can i do that? I need to run just that query and reload the div, the ajax part is okay, but i don't know how to run just that query without running every query to "populate" the content from the page.

Sorry if wasn't clear enough, my english is not to good.

Thanks in advice!

Controller:

public function index()
{
    $data['servicos']                   = DB::select('SELECT * FROM servicos WHERE ativo = 1 ORDER BY ORDEM ASC');
    $data['servicos_processo']          = DB::select('SELECT * FROM servicos_processo WHERE ativo = 1 ORDER BY ID ASC');
    $data['servicos_fun']               = DB::select('SELECT * FROM servicos_fun WHERE ativo = 1 ORDER BY ID ASC');
    $data['curriculum_historico']       = DB::select('SELECT * FROM curriculum_historico WHERE ativo = 1 ORDER BY ID DESC');
    $data['curriculum_educacao']        = DB::select('SELECT * FROM curriculum_educacao WHERE ativo = 1 ORDER BY ANO ASC');
    $data['curriculum_coding_skills']   = DB::select('SELECT * FROM curriculum_coding_skills WHERE ativo = 1 ORDER BY ID ASC');
    $data['curriculum_other_skills']    = DB::select('SELECT * FROM curriculum_other_skills WHERE ativo = 1 ORDER BY ID ASC');
    $data['curriculum_testimonials']    = DB::select('SELECT * FROM curriculum_testimonials WHERE ativo = 1 ORDER BY ID ASC');
    $data['certificados_last']          = DB::select('SELECT * FROM certificados WHERE ativo = 1 ORDER BY data_conclusao DESC LIMIT 9');
    return view('main', $data);
}

public function certificados()
{
    $nome = Request::input('filtro-certificados');
    $data['certificados_last']          = DB::select("SELECT * FROM certificados WHERE ativo = 1 AND nome LIKE '%{$nome}%' ORDER BY data_conclusao DESC LIMIT 9");
    return $data;
}

View:

 <div class="latest-posts media-grid masonry" data-layout="masonry" data-item-width="340">

                @foreach($certificados_last as $certificado)
                <article class="hentry media-cell">
                    <div class="media-box">
                        <img src="images/certificados/{{$certificado->id}}.svg" style="padding:60px" alt="post-image">
                        <div class="mask"></div>
                        <a href="certificado/{{$certificado->id}}"></a>
                    </div>

                    <header class="media-cell-desc">
                            <span title="2013" class="date">
                                <span class="day">{{$certificado->dia_conclusao}}</span>{{$certificado->mes_conclusao}}</span>
                        <h3>
                            <a href="certificado/{{$certificado->id}}">{{$certificado->nome}}</a>
                        </h3>
                    </header>

                </article>
                @endforeach

            </div>
  • 写回答

1条回答 默认 最新

  • weixin_33719619 2018-03-28 13:54
    关注

    Send the post request using axios, jQuery, or whatever client you choose. Include your search and filter parameters.

    In Laravel, create the post route that points to a controller method. This method should just return the data directly- do not return a view.

    For example:

    public function ajaxSearch(Request $request)
    {
        $data = User::where('name', $request->query);
    
        return $data;
    }
    

    Then, use your JavaScript to replace with or insert the new data.

    • Check out Vue if you haven't already.

    Note: Laravel auto-converts the response to jSON.

    Use the done() method, like so when using jQuery:

    $.post( '/myRouteHere', { name: 'John', time: '2pm' })
        .done(function() {
    
            // insert / update data
    
        }).fail(function() {
    
            alert( "error" );
    
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度