dongyin2643 2019-07-27 21:35
浏览 91
已采纳

建议:如何使用laravel动态加载帖子[关闭]

I need a recommendation. I want to have a sidebar with all the titles of the posts and by clicking on any of them that this is loaded with its content in the main section of the page. The idea is that when loading the page not all the contents of each post are loaded, but only when clicking. What system can I use if I am using Laravel?

enter image description here

  • 写回答

1条回答 默认 最新

  • duan_2000 2019-07-27 22:24
    关注

    You can return your post to the view using your controller:

    public function someFunction()
    {
        $posts = Post::all();
        return view('your-view', compact('posts'));
    }
    

    Now, within your blade template, use the laravel @foreach blade directive to create your links to each post:

    Wihtin your view, where you want to display the links to the posts in your sidebar:

    @foreach($post as $post)
        <a href="{{ route('posts.show', $post->id) }}>
            {{ $post->title }}
        </a>
    @endforeach
    

    This code will create a link to each of the posts you have. In order to the route('posts.show', $post->id) work, you need to have a resource route for your post controller or define this route:

    Route::get('posts/{post}', 'YourPostController@show')->name('posts.show');
    

    Then, just write your show method withing your post controller:

    public function show($id)
    {
        $post = Post::find($id);
        return view('your-view', compact('post');
    }
    

    Hope it helps.

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

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数