LoginSignup
0
0

More than 3 years have passed since last update.

How to list and filter related column on Laravel-Admin

Posted at

I'll try to get question table value from answer table this time.

Code

CREATE TABLE `questions` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `content` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
);

 CREATE TABLE `answers` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `question_id` int(11) NOT NULL,
  `answer` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
);
app/Models/Answer.php
    public function question(){
        return $this->belongsTo(Question::class);
    }
app/Admin/Controllers/AnswerController.php
    protected function grid()                                                      
    {
        $grid = new Grid(new Answer); 
        $grid->column('id', __('Id'))->filter();
        $grid->column('question.content', __('question')); // Here is the point.
        $grid->column('created_at', __('Created at'));

        $grid->filter(function($filter){
            $filter->equal('question.content');
        });

        return $grid;                                                              
    }                                                                              

Reference

One to one
Model grid filters

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0