如何使用laravel在1个视图中显示2个表数据? [英] How to show 2 tables data in 1 view using laravel?

查看:79
本文介绍了如何使用laravel在1个视图中显示2个表数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个表(一个是用户",第二个是分支").我想用其分支管理员名称显示分支,并将分支管理员信息保存在用户表中.当我尝试在一个视图中显示两个表的数据时出现错误.告诉我我如何处理此问题.

There is two tables (one is Users and second is Branches). I want to show the Branch with its Branch Admin Name.And the branch Admin info is save in users table. There is an error When i am trying to show the data of Two table in one view. Tell me How i manage this issue.

查看:

<div class="branches col-xs-12 col-sm-12 col-md-9 col-lg-9">
    <input type="text" class="pull-right form-control search" placeholder="Search">

    <div class="spaces"></div>

    <table class="table table-bordered">
        <thead>
        <tr>
            <th>
                BranchName
            </th>
            <th>
                BranchAdmin
            </th>
            <th>
                Email
            </th>
            <th>
                Contact
            </th>
            <th>
                Location
            </th>
            <th>
                Action
            </th>
        </tr>
        </thead>
        <tbody>
        @foreach($branch as $brnch)
            <tr class="branchies-row">
                <td>
                    {{$brnch->branch_name}}
                </td>
                @foreach($user as $users)
                    <td>
                        {{$users->name}}
                    </td>
                @endforeach
                <td>
                    {{$brnch->email}}
                </td>
                <td>
                    {{$brnch->contact}}
                </td>
                <td>
                    {{$brnch->address}}
                </td>


                <td>
                    <a data-id="{{$brnch->id}}" class="delete-branch">Delete</a> /
                    <a href="/branch/edit/{{$brnch->id}}">Edit </a>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>

控制器:

public function getBranchinfo(){
    $user = User::where('type', '=', 'BranchAdmin');
    $branch = Branch::all();
    return view('Branch.branchinfo')->with('branch',$branch)->with('user', $user);
}

型号:

public function branch(){
    return $this->hasOne('App\Branch', 'user_id', 'id');
}

推荐答案

您必须在分支模型中建立关系:

You have to make the relationship in branch model:

public function user(){
return $this->belongsTo('App\User');

}

然后在刀片文件中,您的分支管理员名称为:

Then in blade file you get branch admin name as:

@foreach($branch as $brnch)
        <tr class="branchies-row">
            <td>{{ $brnch->user->branchadmincloumn }}</td>
        </tr>
@endforeach

branchadmincloumn是您的用户表中存储分支管理员名称的cloumn名称.因此,将branchadmincloumn更改为所需的列名. 希望对您有所帮助.

branchadmincloumn is the cloumn name from your users table where your branch admin name is saving. So change branchadmincloumn to your desired column name. Hope it helps..

这篇关于如何使用laravel在1个视图中显示2个表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆