dongyu9667 2015-10-01 09:25
浏览 72
已采纳

Laravel 5如何使用集合获取许多随机行

Based on how get random row laravel-5 I adjusted my query, but still not receiving the expected result.

I have 20 articles in the database and want to get randomly only 3 of them and show them on the page.

My class is just like the following:

public function article()
{
    $article = DashArticle::where('category', '=', 0)->get()->random(3);

    $articleTitle0       = $article[0]->titel;
    $articleAutor0       = $article[0]->autor;
    $articleAbstract0    = $article[0]->abstract;
    $articleSource0      = $article[0]->source;
    $articleTitle1       = $article[1]->titel;
    $articleAutor1       = $article[1]->autor;
    $articleAbstract1    = $article[1]->abstract;
    $articleSource1      = $article[1]->source;
    $articleTitle2       = $article[2]->titel;
    $articleAutor2       = $article[2]->autor;
    $articleAbstract2    = $article[2]->abstract;
    $articleSource2      = $article[2]->source;

    return compact(
        'articleTitle0', 'articleAutor0', 'articleAbstract0', 'articleSource0',
        'articleTitle1', 'articleAutor1', 'articleAbstract1', 'articleSource1',
        'articleTitle2', 'articleAutor2', 'articleAbstract2', 'articleSource2'
    );
}

And here part of the view:

<div id="a-slide" class="carousel slide auto panel-body">
    <ol class="carousel-indicators out">
        <li class="active" data-slide-to="0" data-target="#a-slide"></li>
        <li class="" data-slide-to="1" data-target="#a-slide"></li>
        <li class="" data-slide-to="2" data-target="#a-slide"></li>
    </ol>
    <div class="carousel-inner">
        <div class="item active" style="padding: 0 16px;">
            <p style="font-size: 16px; font-weight: bold;">{!! $articleTitle0 !!}</p>
            <p class="text-muted">{!! $articleAutor0 !!}</p>
            <p><strong>Abstract:</strong><br />{!! $articleAbstract0 !!}</p>
            <p>
                <a href="$articleSource !!}" target="_blank">
                    {!! $articleSource0 !!}
                </a>
            </p>
        </div>

        <div class="item" style="padding: 0 16px;">
            <p style="font-size: 16px; font-weight: bold;">{!! $articleTitle1 !!}</p>
            <p class="text-muted">{!! $articleAutor1 !!}</p>
            <p><strong>Abstract:</strong><br />{!! $articleAbstract1 !!}</p>
            <p>
                <a href="{!! $articleSource1 !!}" target="_blank">
                    {!! $articleSource1 !!}
                </a>
            </p>
        </div>

The problem is when I let the random = 3 ($article = DashArticle::where('category', '=', 0)->get()->random(3); I get following error: "Undefined offset: 1"

and when I user random = 20 (the total number of rows in the table) it works but I don't become any random articles but always the first 3 lines of the table.

I would appreciate any help! Thanks!

  • 写回答

3条回答 默认 最新

  • dtrphb5597 2015-10-01 09:42
    关注

    The problem is that random method preserves keys from original collection. To avoid this you should get a pure values:

    $article = DashArticle::where('category', '=', 0)->get()->random(3)->values();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭