如何使用laravel管理id

时间:2017-04-17 12:24:43

标签: php laravel

enter image description here我加入两个表(一个是User,第二个表是分支)并在单个视图中打印BranchAdmin名称及其分支,branchAdmin信息保存在User表中,但是当我对分支数据执行编辑功能,它将显示一个空白页面,并仅在用户和分支ID匹配时显示编辑表单。那么我在编辑分支时如何管理它只关注分支ID而不是公司ID是分支中的外键?

分公司控制器:

public function getBranchinfo(){
    $branch = DB::table('branch')
        ->join('users', 'users.id', '=', 'branch.user_id')
        ->where('users.type', '=', 'BranchAdmin')
        ->get();
    return view('Branch.branchinfo')->with('branch',$branch);
}

BranchDetails查看:

<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>
                id
            </th>
            <th>
                Branch-Name
            </th>
            <th>
                AdminName
            </th>
            <th>
                Email
            </th>
            <th>
                Contact
            </th>
            <th>
                Location
            </th>
            <th>
                OpenHours
            </th>
            <th>
                Action
            </th>
        </tr>
        </thead>
        <tbody>
        @foreach($branch as $brnch)
            <tr class="branchies-row">
                <td>
                    {{$brnch->id}}
                </td>
                <td>
                    {{$brnch->branch_name}}
                </td>
                <td>
                     Note->(In this i will take the name of user who is Admin of branch.) like this ($brnch->user_name)
                    {{$brnch->user_id}}
                </td>
                <td>
                    {{$brnch->email}}
                </td>
                <td>
                    {{$brnch->contact}}
                </td>
                <td>
                    {{$brnch->address}}
                </td>
                <td>
                    {{$brnch->open_hours}}
                </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 up()
{
    Schema::create('branch', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('company_id')->unsigned();
        // here you need to define on deleteCascade, when this records delete, this will also
        // delete it's records from other tables that related to this one via a foriegn key.
        $table->foreign('company_id')->references('id')->on('company')->onDelete('cascade');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->String('branch_name');
        $table->String('email');
        $table->String('address');
        $table->String('contact');
        $table->String('open_hours');
        $table->timestamps();
    });
}

用户模型:

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

1 个答案:

答案 0 :(得分:1)

我认为问题是两个表有一个名为id的字段,所以如果你想删除冲突,你可以反转你的连接顺序,这样你从查询得到的最后一个id是分支id

$branch = DB::table('user')
    ->join('branch', 'branch.user_id', '=', 'users.id')
    ->where('users.type', '=', 'BranchAdmin')
    ->get();

你可以添加像这样的别名

$branch = DB::table('user')
->select(DB::raw('brunch.*, brunch.id as branch_id, user.*'))
->join('branch', 'branch.user_id', '=', 'users.id')
->where('users.type', '=', 'BranchAdmin')
->get();