search

Home  >  Q&A  >  body text

php - laravel输出变量到模板,变量在模板中再分一次类可以吗?这样可以只查询一次数据库

laravel输出变量到模板,变量在模板中再分一次类可以吗?这样可以只查询一次数据库。
比如:
下面是一个用户的后台首页控制器,返回该用户发表的所有文章,像这样:

    public function index()
    {
        $user=\Auth::user();
        $articles = $user->articles;
        
        return view('user.dashboard.index',  compact('articles'));
    }

下面的代码是在view中显示所有文章:

<p class="card">
    <p class="card-header">
        所有文章
    </p>
    @if ($articles!=null)
    <table class="table table-sm">
        <thead>
        <tr>
            <th>标题</th>
            <th>发布时间</th>
            <th>发布状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach ($articles as $article)
        <tr>
            <td>{{$article->title}}</td>
            <td>{{$article->created_at}}</td>
            <td>{{$article->status}}</td>
            <td>
                <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-eye"></i>详情</a>
                <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-edit"></i>编辑</a>
            </td>
        </tr>
        @endforeach
        </tbody>
    </table>
    @else
    <p>没有文章</p>
    @endif
</p>

可是,这些文章分为两类:
一类是“已发布”,数据库字段status,值为“1”,
一类是“未发布”,数据库字段status,值为“0”,

问题:
现在需要这两类文章分开显示,已发布的显示在一个card中,未发布的显示在一个card中,可以在模板中直接分类吗?这样的话就不用查两次数据库。

阿神阿神2902 days ago432

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-10 17:26:09

    只很容易,$articles 已经是 collections ,所以透过 where 即可过滤..
    如下:


    已发布

    <p class="card">
        <p class="card-header">
            已发布-所有文章
        </p>
        @if ($articles!=null)
        <table class="table table-sm">
            <thead>
            <tr>
                <th>标题</th>
                <th>发布时间</th>
                <th>发布状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            @foreach ($articles->where('status', 1) as $article)
            <tr>
                <td>{{$article->title}}</td>
                <td>{{$article->created_at}}</td>
                <td>{{$article->status}}</td>
                <td>
                    <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-eye"></i>详情</a>
                    <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-edit"></i>编辑</a>
                </td>
            </tr>
            @endforeach
            </tbody>
        </table>
        @else
        <p>没有已发布文章</p>
        @endif
    </p>

    未发布

    <p class="card">
        <p class="card-header">
            未发布-所有文章
        </p>
        @if ($articles!=null)
        <table class="table table-sm">
            <thead>
            <tr>
                <th>标题</th>
                <th>发布时间</th>
                <th>发布状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            @foreach ($articles->where('status', 0) as $article)
            <tr>
                <td>{{$article->title}}</td>
                <td>{{$article->created_at}}</td>
                <td>{{$article->status}}</td>
                <td>
                    <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-eye"></i>详情</a>
                    <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-edit"></i>编辑</a>
                </td>
            </tr>
            @endforeach
            </tbody>
        </table>
        @else
        <p>没有未发布文章</p>
        @endif
    </p>

    reply
    0
  • 迷茫

    迷茫2017-04-10 17:26:09

    可以在模板里面进行两次循环,每次使用if判断,Card A中只有状态是已发布才显示。

    reply
    0
  • 阿神

    阿神2017-04-10 17:26:09

    $articles = $user->articles; 才是查数据库的操作,模版中foreach只是对这个数据进行循环,不会查数据库

    reply
    0
  • Cancelreply