У меня есть две таблицы: club_member и club_membership. В club_membership хранится год обновления клуба. Club_member просто хранит основную c информацию о клубе. Вот их модель.
ClubMember. php:
<?php
namespace App\Models;
use App\Traits\OptimisticLocking;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use OwenIt\Auditing\Contracts\Auditable;
class ClubMember extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
use SoftDeletes;
use OptimisticLocking;
protected $guarded = [];
public function getLocalizedNameAttribute($value)
{
return \LaravelLocalization::getCurrentLocale()=='zh-Hant'?$this->name_tc:$this->name_en;
}
public function getLocalizedCompanyNameAttribute($value)
{
return \LaravelLocalization::getCurrentLocale()=='zh-Hant'?$this->company_name_tc:$this->company_name_en;
}
public function memberships() {
return $this->hasMany(ClubMembership::class, 'club_member_id');
}
public function latest_membership() {
return $this->hasOne(ClubMembership::class, 'club_member_id')->orderByDesc('id');
}
public function scopePreloadValues($query, $excludeInactive = false){
$query = $query->select(
'id as id',
'username',
'code',
'name_tc',
'name_en',
'company_name_tc',
'company_name_en',
'category',
'ci_br_num',
'ci_br_attachment',
'tel',
'fax',
'email',
'website',
'address_tc',
'address_en',
'contact_1_name',
'contact_1_title',
'contact_2_name',
'contact_2_title',
'attachment',
'submission_date',
'deleted_at'
);
if (!$excludeInactive)
$query = $query->withTrashed();
return $query->orderBy('deleted_at')->orderBy('id')
->get()
->toArray();
}
}
ClubMembership. php:
<?php
namespace App\Models;
use App\Traits\OptimisticLocking;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use OwenIt\Auditing\Contracts\Auditable;
class ClubMembership extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
use SoftDeletes;
use OptimisticLocking;
protected $guarded = [];
public function payment(){
return $this->hasOne(Payment::class, 'club_membership_id');
}
public function scopePreloadValues($query, $excludeInactive = false){
$query = $query->select(
'id as id',
'year',
'code',
'effective_date',
'expiry_date',
'deleted_at'
);
if (!$excludeInactive)
$query = $query->withTrashed();
return $query->orderBy('deleted_at')->orderBy('id')
->get()
->toArray();
}
public function getStatusAttribute() {
return $this->expiry_date >= Carbon::today() ? __('common.active') : __('common.expired');
}
}
Итак, я хотел бы использовать laravel красноречивый способ присоединиться к этим двум столам. Когда я хочу найти год вхождения, мне нужно сначала найти запись в таблице club_membership в порядке возрастания и получить первую запись. Затем я получаю club_member.id в таблице club_membership и ищу в таблице club_member. Наконец, я показываю все детали club_member в представлении.
ClubMemberSearchController. php:
<?php
namespace App\Http\Controllers\Panel\ClubMember;
use App\Http\Controllers\Controller;
use App\Models\ClubMembership;
use App\Models\SystemCode;
use Illuminate\Http\Request;
use App\Models\ClubMember;
class ClubMemberSearchController extends Controller
{
public function index(Request $request) {
$variables['club_member_type'] = SystemCode::PreloadValues('club_member_type');
$query = ClubMember::query();
$query_year = ClubMembership::query();
if ($request->has('name') && $request->get('name')!=null)
{
$query = $query->where('name_en','like','%'.$request->get('name').'%')
->orWhere('name_tc','like','%'.$request->get('name').'%')
->orWhere('company_name_en','like','%'.$request->get('name').'%')
->orWhere('company_name_tc','like','%'.$request->get('name').'%');
}
if ($request->has('code') && $request->get('code')!=null)
{
$query = $query->where('code','like','%'.$request->get('code').'%');
}
if ($request->has('email') && $request->get('email')!=null)
{
$query = $query->where('email','like','%'.$request->get('email').'%');
}
if ($request->has('tel') && $request->get('tel')!=null)
{
$query = $query->where('tel','like','%'.$request->get('tel').'%');
}
if ($request->has('fax') && $request->get('fax')!=null)
{
$query = $query->where('fax','like','%'.$request->get('fax').'%');
}
if ($request->has('year_of_entry') && $request->get('year_of_entry')!=null)
{
$query_year = $query_year->where('year_of_entry','like','%'.$request->get('year_of_entry').'%');
}
if ($request->has('category') && $request->get('category')!=null)
{
$query = $query->where('category', '=', $request->get('category'));
}
if ($request->has('is_active') && $request->get('is_active')!=null && $request->get('is_active')!=0)
{
$query = $query->with('latest_membership.payment')->where('expiry_date', '>', now());
}
if ($request->has('sortOrder') && $request->get('sortOrder')!=null && $request->has('sortBy') && $request->get('sortBy')!=null)
{
$query = $query->orderBy($request->get('sortBy'),$request->get('sortOrder'));
}
else
{
$query = $query->orderByDesc('id');
}
$query = $query->with('latest_membership.payment');
$items = $query->paginate();
return view('panel.club_member.search', [
'items' => $items,
'variables' => $variables,
]);
}
}
Я знаю, как построить запрос в той же таблице с помощью Eloquent ORM. Как я могу выполнить поиск результатов таблицы соединений с помощью Eloquent ORM? Большое спасибо.