doukanzhuo4297 2014-08-26 22:45
浏览 22
已采纳

Laravel在不调用“all()”的情况下获取记录

I have been scratching my head over this one, hopefully a super simple answer.

I am using andrewelkins' (excellent!) start site to learn the ways of Laravel, which can be found over at https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site. This starter-site is a simple blog-system, to get to know Laravel.

In one of the controllers (to be specific: AdminBlogsController.php) I see the following code:

public function getIndex()
    {
    // Title
    $title = Lang::get('admin/blogs/title.blog_management');

    //Grab all the blog posts
    $posts = $this->post;

    // Show the page
    return View::make('admin/blogs/index', compact('posts', 'title'));
}

What bothers me, how can you call $this->post without calling something like all(); to get all records.

In every other tutorial I see something like $this->post->all(), but not here. Do note, that this code without the all() works.

  • 写回答

1条回答 默认 最新

  • duangaoe9401 2014-08-27 01:03
    关注

    That code is broken and the comment is misleading. The view does not receive any posts. To verify, search the view for the $post variable. You won't find it, because it's not being used at all.

    So then how do the posts render on the page?

    Simple answer: AJAX.

    The posts are not present on initial page load. Rather, they're loaded in through AJAX from this controller method, where you can clearly see the database calls!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本