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.

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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路