2017-04-17 43 views
0

enter image description here我加入兩個表(一個是用戶和第二個表是分支)並打印BranchAdmin名稱與其分支在單個視圖中,branchAdmin信息保存在用戶表中,但是當我在分支數據上執行編輯功能時,它將顯示一個空白頁面,並且只在用戶和分支ID匹配時才顯示編輯表單。所以當我編輯分支時,我如何管理它只關注分支標識而不是分支中外鍵的公司標識?如何管理ID使用laravel

分支控制器:

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'); 
} 
+0

你想要得到什麼確切地添加別名?它是http://stackoverflow.com/questions/43441770/how-to-show-2-tables-data-in-1-view-using-laravel/43449384#43449384?..我回答了這個 –

+0

看到這張照片在表branch->類型的branch-> id和user-> id(AdminName)是相同的,我不知道爲什麼? – Haider

+0

因爲你得到相同的ID –

回答

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(); 
+0

謝謝但是,告訴我,如果我也打印公司 - > ID在分支中的外鍵表,所以我如何得到最後一個ID從查詢是分支ID? – Haider

+0

我更新了我的awer,你可以使用別名找到第二個選項 –