Laravel-datatables: 검색: idλ₯Ό 톡해 속성 검색

에 λ§Œλ“  2016λ…„ 06μ›” 01일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: yajra/laravel-datatables

문제 λ˜λŠ” κΈ°λŠ₯ μš”μ²­ μš”μ•½

λ§Žμ€ μ—”ν„°ν‹°μ˜ ν…Œμ΄λΈ”μ— μ†Œμœ  μ—”ν„°ν‹°μ˜ IDλ₯Ό μ €μž₯ν•˜μ—¬ μ£Όμž₯ν•˜λŠ” μΌλŒ€λ‹€ 관계. 예: 코디넀이터가 ν•˜λ‚˜ μ΄μƒμ˜ μ„Όν„°λ₯Ό κ΄€λ¦¬ν•©λ‹ˆλ‹€. html λΉŒλ”λ₯Ό 톡해 열을 μ„€μ •ν•œ ν›„ coodinator_id 열을 νŽΈμ§‘ν•˜κ³  μ ‘κ·Όμž λ©”μ„œλ“œλ₯Ό μ μš©ν•˜μ—¬ 코디넀이터 전체 이름을 κ²€μƒ‰ν•©λ‹ˆλ‹€.

public function getFullNameAttribute()
{
    return $this->first_name . ", " . $this->last_name;
}

λ‚΄ λͺ©ν‘œλŠ” 인증된 μ‚¬μš©μžκ°€ λ‚΄ 쿼리에 ν‘œμ‹œλœ λŒ€λ‘œ κ΄€λ¦¬μžκ°€ μ•„λ‹Œ ν•œ 인증된 코디넀이터에 μ†ν•œ μ„Όν„°λ§Œ 데이터 ν…Œμ΄λΈ”μ— ν‘œμ‹œλ˜λ„λ‘ ν—ˆμš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.
κ·ΈλŸ¬λ‚˜ 데이터 ν…Œμ΄λΈ”μ— λ‚˜νƒ€λ‚˜λŠ” Coordinator μ—΄μ˜ 값을 μž…λ ₯ν•˜λ©΄ μΌμΉ˜ν•˜λŠ” λ ˆμ½”λ“œλ₯Ό 찾을 수 μ—†λ‹€κ³  ν‘œμ‹œλ©λ‹ˆλ‹€.

이λ₯Ό μ–΄λ–»κ²Œ λ‹¬μ„±ν•©λ‹ˆκΉŒ?

여기에 문제/κΈ°λŠ₯ μš”μ²­μ„ μ„€λͺ…ν•˜μ‹­μ‹œμ˜€.

문제의 μ½”λ“œ μŠ€λ‹ˆνŽ«

ν•΄λ‹Ήν•˜λŠ” 경우 μ˜μ‹¬λ˜λŠ” 버그λ₯Ό μœ λ°œν•˜λŠ” μ½”λ“œ 사본을 ν¬ν•¨ν•˜μ‹­μ‹œμ˜€. λ§ˆν¬λ‹€μš΄ PHP μ½”λ“œ νƒœκ·Έλ₯Ό μ‚¬μš©ν•˜μ—¬ λΆ™μ—¬λ„£κΈ° ν˜•μ‹μ„ 지정할 수 μžˆμŠ΅λ‹ˆλ‹€.
<?php

namespace App\DataTables;
use Auth;
use App\Center;
use App\Coordinator;
use Yajra\Datatables\Services\DataTable;

class CentersDataTable extends DataTable
{
    // protected $printPreview  = 'path.to.print.preview.view';

    /**
     * Display ajax response.
     *
     * <strong i="13">@return</strong> \Illuminate\Http\JsonResponse
     */
    public function ajax()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->editColumn('action', function ($center) {
                return '<a href="edit-center/'.$center->id.'" <i class="material-icons sideicons-tables">create</i>'; 
               // <a href="field-technician-delete/'.$user->id.'" <i class="material-icons sideicons-tables">delete</i>
            })
            ->editColumn('coordinator_id', function ($user) {
                return Coordinator::find($user->coordinator_id)->full_name;
                })
            ->editColumn('gps_lat', function ($center) {
                    return $center->gps_location ;
                })
            ->make(true);
    }

    /**
     * Get the query object to be processed by datatables.
     *
     * <strong i="14">@return</strong> \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
     */
    public function query()
    {
        if(Auth::user()->hasRole('admin')){
        $centers = Center::query();
        return $this->applyScopes($centers);
        }
        else{
            $centers = Center::query()->where('coordinator_id',Auth::user()->userable->id);
            return $this->applyScopes($centers);
        }
    }

    /**
     * Optional method if you want to use html builder.
     *
     * <strong i="15">@return</strong> \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
             ->columns([
                'id' => ['title' => 'ID'],
                'center_name' => ['title' => 'Name'], 
                'gps_lat' => ['title' => 'Coordinates'],
                'contact_no' => ['title' => 'Contact Number'],
                'coordinator_id' => [
                            'title' => 'Coordinator',
                            'searchable' => true
                ],
                'created_at' => [ 'title' => 'Created'],
                 'updated_at' => ['title' => 'Updated'], 
                'action' => [
                             'data'           => 'action',
                            'name'           => 'action',
                            'render'         => null,
                            'orderable'      => false,
                            'searchable'     => false,
                            'exportable'     => false,
                            'printable'      => false
                             ]
             ])
             ->parameters([
                 'dom' => 'Bfrtip',
                 'buttons' => ['csv', 'excel', 'print','reload'],
             ]);
    }

    /**
     * Get columns.
     *
     * <strong i="16">@return</strong> array
     */
    private function getColumns()
    {
        return [
            'id',
            // add your columns
            'created_at',
            'updated_at',
        ];
    }

    /**
     * Get filename for export.
     *
     * <strong i="17">@return</strong> string
     */
    protected function filename()
    {
        return 'centers';
    }
}

μ‹œμŠ€ν…œ 세뢀정보

  • 운영 체제 : OS X El Capitan 10.11.5
  • PHP 버전 : 7.0.0
  • 라라벨 버전 : 5.1.36 (LTS)
  • Laravel-Datatables 버전 : 6.11.5

λ¬Έμ•ˆ 인사,

question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

ν•  수 μžˆλŠ” νŠΈλ¦­μ€ js에 두 열을 λͺ¨λ‘ μΆ”κ°€ν•˜μ§€λ§Œ μ—¬μ „νžˆ 검색할 수 μžˆλ„λ‘ μΆ”κ°€ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

'last_name' => [
    'data' => 'coordinator.last_name',
    'visible' => false,
    'name' => 'coordinator.last_name',
],
'coordinator' => [
    'data' => 'coordinator',
    'name' => 'coordinator.first_name',
],

->addColumn('coordinator', function ($user) {
    return Area_Coordinator::find($user->area_coordinator_id)->full_name;
})

λͺ¨λ“  3 λŒ“κΈ€

μ½”λ””λ„€μ΄ν„°μ˜ 이름을 κ²€μƒ‰ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ? 이 경우 μ¦‰μ‹œ λ‘œλ“œ λ˜λŠ” 쑰인 쿼리 λ₯Ό μ‚¬μš©ν•˜μ—¬ μž‘λ™ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” λ‹Ήμ‹ μ˜ μ‚¬μ΄νŠΈμ˜ νŠœν† λ¦¬μ–Όμ— 따라 μˆ˜μ •λœ 열망 λ‘œλ”©μ„ μ‹œλ„ν–ˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 이제 λ‚΄ λ¬Έμ œλŠ” μ½”λ””λ„€μ΄ν„°μ˜ 전체 이름을 μ €μž₯ν•˜μ§€ μ•Šκ³  μ„±κ³Ό 이름을 λ³„λ„λ‘œ μ €μž₯ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. λ‚΄κ°€ ν˜„μž¬ 가지고 μžˆλŠ” 것:
질문

$centers = Center::query()->with('coordinator')->where('coordinator_id',Auth::user()->userable->id);
            return $this->applyScopes($centers);

κΈ°λ‘₯

'first_name' => [
                'data' => 'coordinator.first_name',
                'name' => 'coordinator.first_name',
                            'title' => 'Coordinator',
                            'searchable' => true,
                ],

νŽΈμ§‘ 방법

->editColumn('first_name', function ($user) {
                return Area_Coordinator::find($user->area_coordinator_id)->full_name;
                })

μ½”λ””λ„€μ΄ν„°μ˜ 이름을 톡해 검색할 수 μžˆμ§€λ§Œ, κ·Έ μ΄λ¦„μœΌλ‘œ κ²€μƒ‰ν•˜κ³  성을 ν•˜λ‚˜μ˜ μ—΄λ‘œ μ—°κ²°ν•˜λŠ” 방법이 μžˆμŠ΅λ‹ˆκΉŒ? μ•„λ‹ˆλ©΄ μ ‘κ·Όμž λ©”μ„œλ“œλ₯Ό μ μš©ν•˜κ±°λ‚˜ 데이터 ν…Œμ΄λΈ”μ— 성을 포함해야 ν•©λ‹ˆκΉŒ?

λ¬Έμ•ˆ 인사,

ν•  수 μžˆλŠ” νŠΈλ¦­μ€ js에 두 열을 λͺ¨λ‘ μΆ”κ°€ν•˜μ§€λ§Œ μ—¬μ „νžˆ 검색할 수 μžˆλ„λ‘ μΆ”κ°€ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

'last_name' => [
    'data' => 'coordinator.last_name',
    'visible' => false,
    'name' => 'coordinator.last_name',
],
'coordinator' => [
    'data' => 'coordinator',
    'name' => 'coordinator.first_name',
],

->addColumn('coordinator', function ($user) {
    return Area_Coordinator::find($user->area_coordinator_id)->full_name;
})
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰