dongyin2643 2019-07-27 13: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 14: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部