「已注销」 2017-09-06 06:58
浏览 40
已采纳

在laravel 5.5中创建随机按钮

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

enter image description here

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]
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何判断一个数是否为超级素数
      • ¥15 list的ad方法出错求解答
      • ¥20 关于mmap将物理空间映射到用户空间再落盘时使用O_DIRECT方式会提示bad address的问题如何解决?
      • ¥15 无人机px4下载环境编译出错,如何解决?
      • ¥15 您好,请问您的那个pcap转成pcd文件的matlab代码我下载了运行没问题,也生成了点云图,但是无法存储pcd文件,请问怎么保存导出pcd文件呀
      • ¥260 已有标注好的图像数据集,如何构建深度卷积网络训练分类
      • ¥20 matlab图像错误
      • ¥30 如何写通过uni.uploadFile上传图片到ASP后端接收的代码
      • ¥15 selenium模糊定位
      • ¥15 硬件断点的问题,真的被整无语了