【问题标题】:Return logic from Controller into View Laravel 5从控制器返回逻辑到视图 Laravel 5
【发布时间】:2016-11-18 05:31:44
【问题描述】:

我有桌子:

REGIONS

 id | name
----+------------------
 1  | South Luzon
----+------------------
 2  | North West Luzon
----+------------------
 3  | North East Luzon

=====================================

BRANCHES

machinenum | name      | region_id 
-----------+-----------+-----------
108        | Alaminos  | 1
-----------+-----------+-----------
104        | Alexander | 3
-----------+-----------+-----------
131        | Santiago  | 3
-----------+-----------+-----------
114        | Apalit    | 1
-----------+-----------+-----------
137        | Baliuag   | 1
-----------+-----------+-----------
115        | Baguio    | 2
-----------+-----------+-----------
116        | Bantay    | 2
-----------+-----------+-----------
130        | San Jose  | 3

=======================================

USERS

id | name  | machinenum
---+-------+-------------
1  | user1 | 108
---+-------+-------------
2  | user2 | 104
---+-------+-------------
3  | user3 | 131

========================================

PENDINGS

user_id | docdate 
--------+------------
2       | 2016-07-14
--------+------------
1       | 2016-07-13
--------+------------
1       | 2016-07-14
--------+------------
3       | 2016-07-13

我想要的是显示按分支和区域分组的用户发送的所有待处理。所以我的查询就像选择所有区域,并在循环内选择与region_id、inner join 和users tbl 匹配的分支以获取user_id 并在其中选择与@987654326 匹配的所有待处理@ 并显示 docdate 如果查询返回 NOT NULL 否则显示 0。

这是我在控制器中的查询:

$regions = DB::select('SELECT * FROM regions');
        foreach ($regions as $region) {
            echo $region->name . "<br>";
            $branches = DB::select('SELECT b.machinenum, b.name AS bname, u.id as uid
                                    FROM branches AS b
                                    INNER JOIN users AS u ON b.machinenum=u.machinenum
                                    WHERE region_id=:id
                                    ORDER BY b.name ASC',
                                    ['id' => $region->id]);
            foreach ($branches as $branch) {
                echo $branch->bname . "<br>";
                $pendings = DB::select('SELECT * FROM pendings WHERE user_id=:id', ['id' => $branch->uid]);
                if ($pendings) {
                    foreach ($pendings as $pending) {
                        echo $pending->docdate . "<br>";
                    }
                    echo "<br>";
                } else {
                    echo "0 <br>";
                }
            }
            echo "<br>";
        }

结果将是:

South Luzon
Alaminos
2016-07-14 -- docdate

Apalit
0          -- return 0 if no pending
Baliuag
0          -- return 0 if no pending

North West Luzon
Baguio
0          -- return 0 if no pending
Bantay
0          -- return 0 if no pending

North East Luzon
Alexander
2016-07-13 -- docdate
2016-07-14 -- docdate

San Jose
0          -- return 0 if no pending
Santiago
2016-07-13 -- docdate

嗯,这正是我想要的。但是,该逻辑在我的控制器内部。我希望它出现在我的视野中。在我看来,如何返回该逻辑?有什么办法吗?在我看来,我能做的就是@foreach and @if。

这是我当前的代码(不要介意用户和分支之间的关系,我的模型中已经有了):

// Note! In controller I have:

$regions = Region::all();
$branches = Branch::all();
$pendings = Pending::all();
return view('pending.index', compact('regions', 'branches', 'pendings'));



<table class="table table-noborder table-extra-condensed">
    <thead>
        <tr>
            <th class="custom-td text-center">Date</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($regions as $region)
            <tr>
                <th colspan="19">{{ $region->name }}</th>
            </tr>
            @foreach ($branches as $branch)
                @if ($region->id === $branch->region_id)
                    <tr>
                        <td>{{ $branch->name }}</td>
                    </tr>
                    @foreach ($pendings as $pending)
                        @if ($branch->machinenum === $pending->user->machinenum)
                            <tr>
                                <td class="custom-td text-center">{{ $pending->docdate->format('d') }}</td>
                            </tr>
                        @endif
                    @endforeach
                @endif
            @endforeach
        @endforeach
    </tbody>
</table>

【问题讨论】:

    标签: php foreach laravel-5 laravel-5.2 laravel-blade


    【解决方案1】:

    让我们分解一下。

    您可以将这种关系映射到您的 Region 模型上,并将其称为“待处理”,因此您可以级联这些关系以使其更具可读性,只需调用 Region::with('branches')->get();让它返回你想要的东西。看看吧。

    我们将这样做。

    //Region.php
    function branches(){
        return $this->hasMany('App\Branch')->with('usermachines');
    }
    

    这是棘手的部分,让我们假设 users 表是分支和挂起之间的多对多关系表,并使用分支 hasManyThrough 为我们完成困难的部分:

    //Branch.php
    function usermachines(){
        return $this->hasManyThrough('App\Pending','App\User','machinenum','user_id')->with('pendings');
    }
    

    完成!完成所有这些设置后,您需要做的就是:

    在您的控制器中:

    $regions = Region::with('branches')->get();
    return view('pending.index', compact('regions'));
    

    在您看来: 我正在删除您的第二个 foreach 并使用 forelse,这就像 foreach 但当未决数为零时使用 else,请检查一下:

    <table class="table table-noborder table-extra-condensed">
    <thead>
        <tr>
            <th class="custom-td text-center">Date</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($regions as $region)
            <tr>
                <th colspan="19">{{ $region->name }}</th>
            </tr>
            @foreach ($region->branches as $branch)
                    <tr>
                        <td>{{ $branch->name }}</td>
                    </tr>
                    @forelse($branch->pendings as $pending)
                            <tr>
                                <td class="custom-td text-center">{{ $pending->docdate->format('d') }}</td>
                            </tr>
                    @empty
                            <tr>
                               <td>0</td>
                            </tr>
                    @endforelse
            @endforeach
        @endforeach
    </tbody>
    

    【讨论】:

    • 我收到了这个错误。不知道为什么:Builder.php 第 2345 行中的 BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::all()。并在您的tr 中添加td。
    • 对不起,应该是 get() 而不是 all()
    • 在你的@forelse,不是$branch-&gt;usermachines-&gt;pendings as $pending吗?因为我让它工作了,而不仅仅是$branch-&gt;pendings as $pending。
    • 它应该是未决的.. id 匆忙完成了这段代码并没有对其进行测试。很高兴你能让它工作!随时编辑我的答案。
    • 我会接受它作为正确答案。谢谢!只有一个问题,我如何过滤日期,我的意思是我想使用 where 子句,就像我想在特定日期显示所有 pendings 一样。我已经尝试过$regions = Region::with('branches)-&gt;where('branches-&gt;usermachines-&gt;pendings-&gt;created_at', '=', '2016-07-14 09:48:55')-&gt;get();,但我想它无法获取我想要使用where 子句的嵌套json数据。有什么想法吗?
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2015-08-16
    • 1970-01-01
    • 2020-06-07
    • 2015-01-18
    相关资源
    最近更新 更多