Haider
Haider

Reputation: 99

how to manage the id's using laravel

enter image description hereI am join two tables (one is User and the second table is branch) and print the BranchAdmin name with its branch in the single view, the branchAdmin information is save in the User table, but when i perform the edit function on the branch data, it will show an empty page and only show the edit form when users and branch id is match. So how i manage that when i edit the branch it only concern with the branch id not with the company id that is foreign key in the branch?

Branch Controller:

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 View:

<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>

Branch Migration:

  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();
    });
}

User Model:

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

Upvotes: 0

Views: 202

Answers (1)

Abdou Rayes
Abdou Rayes

Reputation: 430

i think that the problem is that the two table have a field called id so if you wanna remove the conflict you can just inverse the order of your join so the last id you get from the query is the branch id

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

you can add aliases like this

$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();

Upvotes: 1

Related Questions