简体   繁体   English

在 Laravel v5.3 中加入多个表

[英]Join with multiple table in Laravel v5.3

I'm having trouble to join with multiple table.我无法加入多个表。 At first, I just join 'profil_pengguna' table with 'users' table.起初,我只是将 'profil_pengguna' 表与 'users' 表连接起来。 (There is no problem) But then, it getting error when I want to join with another table which is 'activity_log' table because I want to use 'created_at' field (from activity_log table) in my index.blade (没有问题)但是,当我想加入另一个表“activity_log”表时,它会出错,因为我想在我的 index.blade 中使用“created_at”字段(来自 activity_log 表)

user_id and subject_id are fk for id (in users table). user_id 和 subject_id 是 id 的 fk(在 users 表中)。

In fact I'm still new to using Laravel.事实上,我对使用 Laravel 还是很陌生。 So my task is to add some requirements to the existing code.所以我的任务是向现有代码添加一些要求。 Hoping that someone can help/guide me:) thanks in advance.希望有人可以帮助/指导我:) 在此先感谢。

Here's my code in UserController这是我在 UserController 中的代码

public function index()
    {
        $users = $this->userRepository->createModel()->with('roles')
            ->leftjoin('profil_pengguna', 'users.id', '=', 'profil_pengguna.user_id')
            ->leftjoin('activity_log', 'users.id', '=', 'activity_log.subject_id');

        if(Input::has('nama')) {
            $users->where('profil_pengguna.nama','like','%'.Input::get('nama').'%')->orWhere('email','like','%'.Input::get('nama').'%')->orWhere('no_mykad','like','%'.Input::get('nama').'%');
        }

        if(Input::has('status')) {
            $users->where('users.status','=','%'.Input::get('status').'%');
        } else {
            $users->where('users.status','=','1');
        }
            $users = $users->orderBy('users.id', 'desc')->paginate(10);

               Session::put('users_url',request()->fullUrl());
            //    echo session::get('users_url');

        return view('Centaur::users.index', ['users' => $users]);
    }

code for index.blade index.blade 的代码

<div class="panel-body">
                <div class="option pull-right" id="option" style="padding:5px; margin-bottom:5px;display:none;" >
                {!! Form::open(['route' => 'users.index', 'class' => 'form-inline', 'method' => 'GET']) !!}
                <div class="form-group">
                {!! Form::text('nama', null, ['class' => 'form-control', 'placeholder' => 'Nama / Email Pengguna / Kad Pengenalan']) !!}
                @if ($errors->has('nama')) <span class="help-block"> {{ $errors->first('nama') }} </span> @endif
                </div>
                <div class="form-group">
                {!! Form::select('status', ['0' => 'Dipadam', '1' =>'Aktif'], null, ['class' => 'form-control', 'placeholder' => 'Sila pilih...']) !!}
                @if ($errors->has('status')) <span class="help-block"> {{ $errors->first('status') }} </span> @endif
                </div>
                <button type="submit" class="btn btn-primary">
                Cari
                </button>
                {!! Form::close() !!}
                </div>
                <div class="clearfix"></div>
                @if($users->count() > 0)
                <div class="table-responsive">
                <table class="table table-borderless">
                    <thead>
                        <th>#</th>
                        {{-- <th></th> --}}
                        <th>Nama</th>
                        <th>Email</th>
                        <th>Peranan</th>
                        <th></th>
                        <th>Kemaskini Terakhir</th>
                        <th></th>
                        <th></th>
                    </thead>
                    <tbody>
                    <?php $bil = 1 ?>
                        @foreach ($users as $user)
                        <tr>
                            <td style="vertical-align: middle;">{{  (($users->currentPage() - 1 )*$users->perPage()) + $bil }}</td>
                            {{-- <td style="vertical-align: middle;"><img src="//www.gravatar.com/avatar/{{ md5($user->email) }}?d=mm" alt="{{ $user->email }}" class="img-circle" style="width:50px; border:solid 1px #C5C5C5;"></td> --}}
                            <td style="vertical-align: middle;"><a href="{{ route('users.show', $user->id)}}">@if($user->profil->nama){{ $user->profil->nama }}@endif</a>
                            <br><small>{{ $user->no_mykad }}</td>
                            <td style="vertical-align: middle;">{{ $user->email }}</td>
                            <td style="vertical-align: middle;">@if ($user->roles->count() > 0)
                                {{ $user->roles->implode('name', ', ') }}
                            @else
                                <em>Tidak Ditetapkan Peranan</em>
                            @endif</td>
                            <td></td>
                            <td style="vertical-align: middle;">{{ $user->activity_log->created_at }}
                            <br><small>{{ $user->activity_log->created_at->diffForHumans() }}</td> 
                            <!-- Edit Button -->
                            <td style="vertical-align: middle;">
                                <a class="btn btn-default pull-right" href="{{ route('users.edit', $user->id) }}">
                                    Kemaskini
                                </a>
                            </td>
                            <!-- Delete Button -->
                            <td style="vertical-align: middle;">
                                <form class="delete" action="{{ route('users.destroy', $user->id) }}" method="POST">
                                        <input type="hidden" name="_method" value="DELETE">
                                        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                                        <input type="submit" value="Padam" class="btn btn-danger">
                                    </form>
                            </td>
                        </tr>
                    <?php $bil++ ?>
                        @endforeach
                    </tbody>
                </table>
                </div>
                @elseif(Request::has('nama') == '1')
                    <div class="wello text-center">
                        <h4><i class="fa fa-search"></i></h4>
                        <h4>Tiada pengguna dengan nama "{{ Request::get('nama') }}" dijumpai</h4>
                    </div>
                @elseif($users)
                    <div class="wello text-center">
                        <h4><i class="fa fa-users"></i></h4>
                        <h4>Tambah Pengguna Baru</h4>
                        <a href="{{ route('users.create') }}" class="btn btn-success">Tambah pengguna</a>
                    </div>
                @endif
                {{ $users->appends($_REQUEST)->render() }}
            </div>

Hi By Carefully looking at your code and the table structure which you havent shared, so supposing it this way that table name is users and it contains id as primary key, the updated code is this您好仔细查看您的代码和您尚未共享的表结构,因此假设表名是用户并且它包含 id 作为主键,更新的代码是这样的

$users = $this->userRepository->createModel()->with('roles')
            ->leftjoin('users', 'users.id', '=', 'profil_pengguna.user_id')
            ->leftjoin('users', 'users.id', '=', 'activity_log.user_id');

Might be another possibility where you are saving subject_id in activity log table so you need to add one more join with subject masters table可能是您在活动日志表中保存 subject_id 的另一种可能性,因此您需要再添加一个与主题主表的联接

$users = $this->userRepository->createModel()->with('roles')
            ->leftjoin('users', 'users.id', '=', 'profil_pengguna.user_id')
            ->leftjoin('users', 'users.id', '=', 'activity_log.user_id')
->leftjoin('subject', 'subject.id', '=', 'activity_log.subject_id');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM