在控制器中使用路由

时间:2020-05-08 14:30:18

标签: ajax laravel

我正在尝试获取数据(作业)并显示每个作业,我有作业的路由和作业的路由,但是当我在控制器中使用路由时,我得到的路由未定义,即使一切都很好,请检查我的问题

ajax呼叫

<script>
    $(document).on('click','.submit', function(e) {  
        var category = $("input[name=category_id]").val();
       var location= $(".js-example-basic-single option:selected" ).val();
       console.log(location);

          $.ajax({
          type:'get',
          data: {location : location,category : category},
          url:'/job_listing',
          success:function(data) {
                  $('.job-list').html(data);
                  console.log(category);
                  console.log(location);
               }
       })

    });

cotroller中的功能

public function show_offers(Request $request){
        $category = $request->category;
        $location= $request->location;
        if($request->ajax()) {

            $data = Job_offer::where([
                'category_id' => $category,
                'location_id' => $location,

         ])->orwhere('location_id','=',$location)->orwhere('category_id','=',$category)->get();



            // ->orWhere('location_id','LIKE','%'.$input.'%')->with('location')->get();

                    $output = '';

                    if (count($data)>0) {

                        $output = '';
                        foreach ($data as $row){

                            $output .= '<div class="job-info"> <div class="job_offer-img"><img class="offer-img" src='.'img/'.$row->offer_image.'></div>';
                            $output .= '<div class="job-title"> <span class="job"> <a href="'.route('single/offer', $row->id).'" class="job-offer-title">'.$row->offer_title.'</a></span>';
                            $output .= '<span class="location">'.$row->location->Name.'</span></div>';
                            $output .= '<div class="job-contrat"> <span class="contract"> '.$row->type_emploi.'</span></div></div>';

                        }
                        $output .= '';


                    }
                    else {

                        $output .= '<li class="list-group-item">'.'No results'.'</li>';
                    }

                    return $output;

                    }

路线

Route::get('single_offer/{offer_id}','job_seeker\Job_offers@single_offer')->name('single/offer');
Route::get('job_listing','job_seeker\home_job_seeker@show_offers');

2 个答案:

答案 0 :(得分:0)

您的路线必须位于routes/api.php文件中,而不是routes/web.php文件中。

答案 1 :(得分:0)

像这样使用

href='."{{route('single/offer',['offer_id'=>$row->id])}}".'
相关问题