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

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

WBOY
WBOYOriginal
2016-06-06 20:10:341175browse

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

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

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

<code><div class="card">
    <div class="card-header">
        所有文章
    </div>
    @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
</div></code>

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

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

回复内容:

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

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

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

<code><div class="card">
    <div class="card-header">
        所有文章
    </div>
    @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
</div></code>

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

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

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


已发布

<code><div class="card">
    <div class="card-header">
        已发布-所有文章
    </div>
    @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
</div></code>

未发布

<code><div class="card">
    <div class="card-header">
        未发布-所有文章
    </div>
    @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
</div></code>

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn