想显示作品集图像,但是我遇到了一些错误

时间:2019-11-05 08:27:27

标签: javascript php ajax laravel

想要显示投资组合图片,但我遇到一些错误以及如何解决此问题?

错误

  

未定义变量:投资组合(查看:   C:\ xampp \ htdocs \ sigma-delta \ resources \ views \ front_end \ project.blade.php)

请参阅此链接 https://flareapp.io/share/V7jQoLPb#F48

controller

public function Projectaction(Request $request){

    $query=$request->get('query');   
       if($query == 'All')
       {
           $data['portfolio']=DB::table('category')
                ->join('projects', 'category.id', '=', 'projects.Category_id')
                ->select('projects.*', 'category.*')->get();
       }
       else if($query != 'All')
       {
           $data['portfolio']= DB::table('category')
              ->join('projects', 'category.id', '=', 'projects.Category_id')
              ->select('projects.*', 'category.*')
              ->where('category.category_name', $query)
              ->get();
       }

       return Response::json(array('response'=>true,'html' =>
              View::make('front_end/project',$data)->render()));

    }

Route

    Route::get('Project','FrontController@project');
    Route::get('projectaction','FrontController@projectaction');
    Route::get('Contact-Us','FrontController@Contact');

html view

<div class="portfolio masonry p-3-cols p-style3" id="masonry">
    @foreach($portfolio  as  $portfolios)
        <div class="portfolio-item ">
            <figure>
                <img alt="" src="{{$portfolios->image}}">
                <figcaption>
                     <div class="port-captions">
                          <h4><a href="single-portfolio.php">{{$portfolios->project_name}}</a></h4>
                     </div>
                     <div class="icon-links">
                          <a href="single-portfolio.php" class="link"><i class="fa fa-link"></i></a>
                          <a href="{{$portfolios->image}}" class="zoom" title="Quality Products for 
                    Companies"><i class="fa fa-search-plus"></i></a>
                    </div>
                </figcaption>           
            </figure>
        </div>
    @endforeach

ajax

$(document).ready(function() {
    fetch_customer_data("All");

    function fetch_customer_data(query) {
        $.ajax({
            type: 'get',
            url: '{{URL::to("projectaction")}}',
            data: {
                query: query
            },
            dataType: 'json',
            success: function(data) {
                console.log('success');
                $('#portfolio').html(data.html);
            },
            error: function(data) {
                console.log('error');
                console.log(data.responseText);
           },
            done: function() {
                $('#portfolio').unblock();
           }
        });
    }
    $(".filter-button").click(function() {
        var value = $(this).attr('data-filter');

        if (value == "All") {
            fetch_customer_data(value);
            $('.filter').toggle();
        } else {
            $(".filter").not('.' + value).toggle();
            $('.filter').filter('.' + value).toggle();
            fetch_customer_data(value);
        }
    });
    if ($(".filter-button").removeClass("active")) {
        $(this).removeClass("active");
    }
    $(this).addClass("active");

});

1 个答案:

答案 0 :(得分:1)

尝试稍微重构控制器:

public function Projectaction(Request $request)
{
    $query = $request->get('query');

    if ($query == 'All') {
        $portfolio = DB::table('category')
            ->join('projects', 'category.id', '=', 'projects.Category_id')
            ->select('projects.*', 'category.*')
            ->get();
    } else {
        $portfolio = DB::table('category')
            ->join('projects', 'category.id', '=', 'projects.Category_id')
            ->select('projects.*', 'category.*')
            ->where('category.category_name', $query)
            ->get();
    }

    return Response::json([
        'response' => true,
        'html' => View::make('front_end/project', ['portfolio' => $portfolio])->render(),
    ]);
}