G
G
GaserV2016-04-19 23:11:47
JavaScript
GaserV, 2016-04-19 23:11:47

Search problem on jquery ajax + laravel?

Good evening! Such situation. Made a live-search for the admin panel. My search is implemented on the BLOG page, where the TITLE and DATE of PUBLICATION are displayed in the table. I need the search results to replace all displayed articles in the admin. At the moment this is a problem. It turns out that the full blog view is returned, only instead of $articles, articles corresponding to the search results are returned. Is it possible somehow to return not the whole view, but only the results of the search in the $articles variable? Or not? Or tell me a more thoughtful way for this purpose) thanks!)

$('.search-field').keypress(function(){
    var words = $(this).val().trim();

    $.ajax({

      url: "/search",
      type: "post",
      data: {words: words},

      success: function(data) {
        $('html').html(data);
        $('.search').addClass('visible');
        $('.search-field').val(words);
      }

    })	
    
  })

public function search() 
  {
    $words = Request::get('words');
    $results = Articles::where('title', 'LIKE', '%' . $words . '%') -> get();
    return View::make('blog')->with('articles', $results);
  }


<div class="articles">
      @foreach($articles as $article)
      <div class="articles-row rem">
        <a href="/edit-article-{{ $article['id'] }}">
          {{ $article['title'] }}
          <span class="delete-icon" onclick="removeArticle(event,this,{{ $article['id'] }})"></span>
          <span class="date">{{ $article['date'] }}</span>
        </a>
      </div>
      @endforeach
    </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-04-20
@Denormalization

Why return a View? Why not just return json with the results ( response()->json($articles) )
And then just output to js?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question