0

Before a user submission can go live, and admin needs to verify the data that is provided. This data is stored in the pivot table specialism_user.

Both the specialism and the user Resource have a BelongsToMany::make field on them:

class Specialism extends Resource
{
  public function fields(Request $request)
  {
    return [
        BelongsToMany::make('Gebruikers', 'Users', User::class)
            ->fields(function() {
              return [
                  Boolean::make('Goedgekeurd', 'approved'),
                  File::make('Diploma', 'diploma'),                   
                  File::make('Cijferlijst', 'cijferlijst'),                      
                  File::make('Overige', 'certificate'),                      
                  Date::make('Geldig', 'valid_until'),                      
                  Date::make('Diploma', 'earned_at'),                      
                  Text::make('Opmerking', 'note'),                      
              ];
            }),
    ];
  }
}
class User extends Resource
{
  public function fields(Request $request)
  {
    return [
        BelongsToMany::make('Registers', 'Specialisms', Specialism::class)
            ->hideFromIndex()
            ->fields(function() {
              return [
                  Boolean::make('Goedgekeurd', 'approved'),                      
                  File::make('Diploma', 'diploma'),                   
                  File::make('Cijferlijst', 'cijferlijst'),                      
                  File::make('Overige', 'certificate'),                      
                  Date::make('Geldig', 'valid_until'),                      
                  Date::make('Diploma', 'earned_at'),                      
                  Text::make('Opmerking', 'note'),                      
              ];
            }),
    ];
  }
}

And this data is actually shown below the resource: attached resource but the file fields show just a dash instead of a link to the real file.

The other option to view the data would be the eye icon, but this just shows the specialism resource if you were on the user resource and vice versa not the actual specialism_user.

Maybe it's the way the path is stored, but if I click the edit button the path seems to be there. This is where it's stored:

    if (request()->hasFile('certificaat')) {
        $path = request()->file('certificaat')->store("other/{$user->id}", "public");
        $connect->certificate = $path;
    }

    if (request()->hasFile('diploma')) {
        $diploma = request()->file('diploma')->store("diplomas/{$user->id}", "public");
        $connect->diploma = $diploma;
    }

    if (request()->hasFile('cijferlijst')) {
        $cijferlijst = request()->file('cijferlijst')->store("cijferlijsten/{$user->id}", "public");
        $connect->cijferlijst = $cijferlijst;
    }

    if ($connect->isDirty()) {
        $connect->save();
    }

And finally these are the models:

class User extends Authenticatable implements MustVerifyEmail
{
  public function specialisms()
  {
    return $this
        ->belongsToMany(Specialism::class)
        ->using(UserSpecialism::class, 'specialism_user')
        ->withPivot(['valid_until','earned_at', 'note', 'approved', 'diploma', 'cijferlijst', 'certificate', 'school_id', 'id'])
        ->withTimestamps();
  }
}
class Specialism extends Model
{
  public function users()
  {
    return $this
        ->belongsToMany(User::class)
        ->using(UserSpecialism::class, 'specialism_user')
        ->withPivot(['valid_until','earned_at', 'note', 'approved', 'diploma', 'cijferlijst', 'certificate', 'school_id', 'id'])
        ->withTimestamps();
  }
}

I hope somebody can point me in the right direction, because it feels like I've been running in circles trying the same things for ages :D

killerog
  • 1
  • 2

1 Answers1

0

Did you try the displayUsing method (https://nova.laravel.com/docs/3.0/resources/fields.html#field-resolution-formatting) on the File field?

BelongsToMany::make('Registers', 'Specialisms', Specialism::class)
    ->fields(function () {
        return [
            // ...
            File::make('certificate')
                ->displayUsing(function ($certificatePath) {
                    return '<a href="' . $certificatePath . '" target="_blank">Open file</a>';
                })->asHtml(),
            // ...
        ];
    })
Volker Rose
  • 1,808
  • 15
  • 16
  • I didn't try that, smart thinking. Sadly, the asHtml method only seems to exist on the Text field and not on the File. – killerog Apr 24 '20 at 07:39
  • I see. But I think you would be free to use a Text field for the `certificate`, maybe only on the detail view. You can determine the view (index, detail etc.) as follows: https://stackoverflow.com/a/60651503/341983 – Volker Rose Apr 24 '20 at 08:32