IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Laravel PHP Discussion :

Datatable relation belongsTo


Sujet :

Laravel PHP

  1. #1
    Membre actif
    Homme Profil pro
    développeur
    Inscrit en
    Octobre 2004
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : développeur
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Octobre 2004
    Messages : 479
    Points : 281
    Points
    281
    Par défaut Datatable relation belongsTo
    Bonjour,

    J'ai un modèle cahier_charge et un modèle signe_qualite avec une relation 1,n entre les deux.

    J'utilise Datatables pour afficher les données cahier_charge.
    J'aimerais afficher LIBELLE_SIGNE_QUALITE, mais ça affiche la clé ID_SIGNE_QUALITE.

    Je ne vois comment faire pour afficher le libellé et pas la clé.

    La classe CahierChargeDataTable
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    <?php
     
    namespace App\DataTables;
     
    use App\Models\CahierCharge;
    use Yajra\DataTables\Services\DataTable;
    use Yajra\DataTables\EloquentDataTable;
    use Yajra\DataTables\Html\Column;
     
    class CahierChargeDataTable extends DataTable
    {
        /**
         * Build DataTable class.
         *
         * @param mixed $query Results from query() method.
         * @return \Yajra\DataTables\DataTableAbstract
         */
        public function dataTable($query)
        {
            $dataTable = new EloquentDataTable($query);
     
            return $dataTable->addColumn('action', 'cahier_charges.datatables_actions');
        }
     
        /**
         * Get query source of dataTable.
         *
         * @param \App\Models\CahierCharge $model
         * @return \Illuminate\Database\Eloquent\Builder
         */
        public function query(CahierCharge $model)
        {
            return $model->newQuery();
     
        }
     
        /**
         * Optional method if you want to use html builder.
         *
         * @return \Yajra\DataTables\Html\Builder
         */
        public function html()
        {
            return $this->builder()
                ->columns($this->getColumns())
                ->minifiedAjax()
                ->addAction(['width' => '120px', 'printable' => false, 'title' => __('crud.action')])
                ->parameters([
                    'dom'       => 'Bfrtip',
                    'stateSave' => true,
                    'order'     => [[0, 'desc']],
                    'buttons'   => [
                        [
                           'extend' => 'create',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-plus"></i> ' .__('auth.app.create').''
                        ],
                        [
                           'extend' => 'export',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-download"></i> ' .__('auth.app.export').''
                        ],
                        [
                           'extend' => 'print',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-print"></i> ' .__('auth.app.print').''
                        ],
                        [
                           'extend' => 'reset',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-undo"></i> ' .__('auth.app.reset').''
                        ],
                        [
                           'extend' => 'reload',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-refresh"></i> ' .__('auth.app.reload').''
                        ],
                    ],
                     'language' => [
                       'url' => url('../resources/lang/fr/DataTables/French.json'),
                     ],
                ]);
        }
     
        /**
         * Get columns.
         *
         * @return array
         */
        protected function getColumns()
        {
            return [
                'LIBELLE_CAHIER_CHARGE' => new Column(['title' => __('models/cahierCharges.fields.LIBELLE_CAHIER_CHARGE'), 'data' => 'LIBELLE_CAHIER_CHARGE']),
                'ID_SIGNE_QUALITE' => new Column(['title' => __('models/cahierCharges.fields.ID_SIGNE_QUALITE'), 'data' => 'ID_SIGNE_QUALITE']),
                'DATE_CAHIER_CHARGE' => new Column(['title' => __('models/cahierCharges.fields.DATE_CAHIER_CHARGE'), 'data' => 'DATE_CAHIER_CHARGE']),
                'FICHIER_CAHIER_CHARGE' => new Column(['title' => __('models/cahierCharges.fields.FICHIER_CAHIER_CHARGE'), 'data' => 'FICHIER_CAHIER_CHARGE'])
            ];
        }
     
        /**
         * Get filename for export.
         *
         * @return string
         */
        protected function filename()
        {
            return 'cahier_chargesdatatable_' . time();
        }
    }

    Le modèle CahierCharge
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    <?php
     
    namespace App\Models;
     
    use Eloquent as Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
     
    /**
     * Class CahierCharge
     * @package App\Models
     * @version April 22, 2020, 6:57 am UTC
     *
     * @property \App\Models\SigneQualite idSigneQualite
     * @property \App\Models\EstConforme estConforme
     * @property integer ID_SIGNE_QUALITE
     * @property string DATE_CAHIER_CHARGE
     * @property string LIBELLE_CAHIER_CHARGE
     * @property string FICHIER_CAHIER_CHARGE
     */
    class CahierCharge extends Model
    {
        use SoftDeletes;
     
        public $table = 'cahier_charge';
     
        const CREATED_AT = 'created_at';
        const UPDATED_AT = 'updated_at';
     
     
        protected $dates = ['deleted_at'];
     
     
        protected $primaryKey = 'ID_CAHIER_CHARGE';
     
        public $fillable = [
            'ID_SIGNE_QUALITE',
            'DATE_CAHIER_CHARGE',
            'LIBELLE_CAHIER_CHARGE',
            'FICHIER_CAHIER_CHARGE'
        ];
     
        /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
        protected $casts = [
            'ID_CAHIER_CHARGE' => 'integer',
            'ID_SIGNE_QUALITE' => 'integer',
            'DATE_CAHIER_CHARGE' => 'string',
            'LIBELLE_CAHIER_CHARGE' => 'string',
            'FICHIER_CAHIER_CHARGE' => 'string'
        ];
     
        /**
         * Validation rules
         *
         * @var array
         */
        public static $rules = [
            'LIBELLE_CAHIER_CHARGE' => 'required'
        ];
     
        /**
         * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
         **/
        public function idSigneQualite()
        {
            return $this->belongsTo(\App\Models\SigneQualite::class, 'ID_SIGNE_QUALITE');
        }
     
    }
    Nom : Capture.PNG
Affichages : 351
Taille : 9,6 Ko

  2. #2
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 496
    Points : 12 596
    Points
    12 596
    Par défaut
    Dans la doc il dit ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
    return DataTables::eloquent($model)
                    ->addColumn('intro', function(User $user) {
                        return 'Hi ' . $user->name . '!';
                    })

  3. #3
    Membre actif
    Homme Profil pro
    développeur
    Inscrit en
    Octobre 2004
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : développeur
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Octobre 2004
    Messages : 479
    Points : 281
    Points
    281
    Par défaut
    Après avoir tourné les choses dans tous les sens, j'ai enfin réussi.
    J'avais aussi fait un mauvais suppr dans le code, il manquait un return

    La classe CahierChargeDataTable devient (fonctions dataTable et getColumns)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    <?php
    
    namespace App\DataTables;
    
    use App\Models\CahierCharge;
    use Yajra\DataTables\Services\DataTable;
    use Yajra\DataTables\EloquentDataTable;
    use Yajra\DataTables\Html\Column;
    
    class CahierChargeDataTable extends DataTable
    {
        /**
         * Build DataTable class.
         *
         * @param mixed $query Results from query() method.
         * @return \Yajra\DataTables\DataTableAbstract
         */
        public function dataTable($query)
        {
            $dataTable = new EloquentDataTable($query);
    
            return $dataTable->addColumn('action', 'cahier_charges.datatables_actions')
                             ->addColumn('LIBELLE_SIGNE_QUALITE', function(CahierCharge $cahier) {
                                                                         return $cahier->idSigneQualite->LIBELLE_SIGNE_QUALITE;
                                                                    }
                                          );
        }
    
        /**
         * Get query source of dataTable.
         *
         * @param \App\Models\CahierCharge $model
         * @return \Illuminate\Database\Eloquent\Builder
         */
        public function query(CahierCharge $model)
        {
            return $model->newQuery();
    
        }
    
        /**
         * Optional method if you want to use html builder.
         *
         * @return \Yajra\DataTables\Html\Builder
         */
        public function html()
        {
            return $this->builder()
                ->columns($this->getColumns())
                ->minifiedAjax()
                ->addAction(['width' => '120px', 'printable' => false, 'title' => __('crud.action')])
                ->parameters([
                    'dom'       => 'Bfrtip',
                    'stateSave' => true,
                    'order'     => [[0, 'desc']],
                    'buttons'   => [
                        [
                           'extend' => 'create',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-plus"></i> ' .__('auth.app.create').''
                        ],
                        [
                           'extend' => 'export',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-download"></i> ' .__('auth.app.export').''
                        ],
                        [
                           'extend' => 'print',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-print"></i> ' .__('auth.app.print').''
                        ],
                        [
                           'extend' => 'reset',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-undo"></i> ' .__('auth.app.reset').''
                        ],
                        [
                           'extend' => 'reload',
                           'className' => 'btn-primary-table btn btn-default btn-sm no-corner',
                           'text' => '<i class="fa fa-refresh"></i> ' .__('auth.app.reload').''
                        ],
                    ],
                     'language' => [
                       'url' => url('../resources/lang/fr/DataTables/French.json'),
                     ],
                ]);
        }
    
        /**
         * Get columns.
         *
         * @return array
         */
        protected function getColumns()
        {
            return [
                'LIBELLE_CAHIER_CHARGE' => new Column(['title' => __('models/cahierCharges.fields.LIBELLE_CAHIER_CHARGE'), 'data' => 'LIBELLE_CAHIER_CHARGE']),
                'LIBELLE_SIGNE_QUALITE' => new Column(['title' => __('models/cahierCharges.fields.LIBELLE_SIGNE_QUALITE'), 'data' => 'LIBELLE_SIGNE_QUALITE']),
                'DATE_CAHIER_CHARGE' => new Column(['title' => __('models/cahierCharges.fields.DATE_CAHIER_CHARGE'), 'data' => 'DATE_CAHIER_CHARGE']),
                'FICHIER_CAHIER_CHARGE' => new Column(['title' => __('models/cahierCharges.fields.FICHIER_CAHIER_CHARGE'), 'data' => 'FICHIER_CAHIER_CHARGE'])
            ];
        }
    
        /**
         * Get filename for export.
         *
         * @return string
         */
        protected function filename()
        {
            return 'cahier_chargesdatatable_' . time();
        }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [CakePHP] type de relation : belongsTo ou hasMany through Data?
    Par laSista dans le forum Bibliothèques et frameworks
    Réponses: 9
    Dernier message: 29/09/2014, 09h59
  2. [EJB2.1 Entity] [CMR] Relation One to Many
    Par hamed dans le forum Java EE
    Réponses: 2
    Dernier message: 31/12/2003, 14h26
  3. Réponses: 2
    Dernier message: 26/09/2003, 15h54
  4. Problème avec mes tables de relation...
    Par mmike dans le forum PostgreSQL
    Réponses: 4
    Dernier message: 02/06/2003, 15h16
  5. Réponses: 3
    Dernier message: 21/05/2003, 11h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo