z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.14k stars 2.82k forks source link

Show with relationships #5470

Closed AngelAvelarBonilla closed 2 years ago

AngelAvelarBonilla commented 2 years ago

Description:

I have a meat ingredient model:

class Meat extends Model implements HasNiceClassName {

  use HasNutritionFacts;
  use HasImagePath;
  use HasName;

  protected $hidden = ['created_at', 'updated_at'];

  static function getNiceClassName() : string {
    return 'Meat';
  }

}

And a nutrition fact model:

class NutritionFact extends Model {

  protected $hidden = [
    'id', 'ingredient_class', 'created_at', 'updated_at'
  ];

}

The meat ingredient has one nutrition fact:

trait HasNutritionFacts {

  public function nutritionFacts() {
    return $this->hasOne(NutritionFact::class, 'ingredient_id')
      ->where('ingredient_class', get_class($this));
  }

}

I want to display the nutrition facts in the show page of the meat ingredient. Here is what I have so far:

protected function detail($id)
    {
        $show = new Show(Meat::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('image_path', __('Image path'))->image();
        $show->field('is_flat', __('Is flat'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        $show->nutrition_fact('Nutrition Facts', function ($nutrition_fact) {
            $nutrition_fact->setResource('/admin/ingredients/nutrition-facts');

            $nutrition_fact->field('calories','Calories');
        });

        return $show;

    }

However, the calories field does not show.

Steps To Reproduce:

qahtansaid commented 2 years ago

dont use field(), but ->yourrealtion()->yourcolumninrelation()

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.