I'm working on project and in first page of my application is a block that shows one of the posts in random order, but there is also a button to refresh this random post and show another instead without user needs to refresh the page.
Now my question is, how to make that button works?
Here is my controller:
public function index() {
$randomfood = Food::where('status', 1)->inRandomOrder()->take(1)->get();
return view('welcome', compact('randomfood'));
}
This is my view:
@foreach($randomfood as $randfood)
<div class="col-md-2"><a href="{{ route('showrecipe', $food->slug) }}"><img src="{{url('/')}}/images/{{$randfood->image}}" class="thumbnail img-responsive" alt="food image"></a></div>
<div class="col-md-8">
<h5><a href="{{ route('showrecipe', $food->slug) }}">{{$randfood->title}}</a></h5>
{!! str_limit($randfood->description, 100) !!}
</div>
<div class="col-md-2 text-center">
<p>Don't like?</p>
<a class="bhover" href="#">Find another recipie</a>
</div>
@endforeach
UPDATE:
JS code now is like this after checked from http://www.jslint.com/
$(document).ready(function () {
$( "a.bhover" ).click(function() {
$.ajax({
url: "{{route('food.randompost')}}",
data: {"token_": "{{Session::token()}}"}, //session token, neccesary to use POST request
type: "food",
success: function (data) {
$("#live_content").empty(); //clean the actual post
$("#live_content").append(data); // add new post from the controller
},
error: function(data){
//handle errors here
}
});
});
});
});
Error in console:
SyntaxError: expected expression, got '}'[Learn More]