laravel / nova-issues

556 stars 34 forks source link

withMeta doesn't work for Cards #1270

Closed 4unkur closed 5 years ago

4unkur commented 5 years ago

I was trying to pass some data to Card.vue like it was described in the docs: https://nova.laravel.com/docs/1.0/customization/cards.html#registering-cards

But that didn't work, so I found a workaround:

I passed my data vie withMeta in NovaServiceProvider.php

    protected function cards()
    {
        return [
            (new Home)->withMeta(['key' => 'value']),
        ];
    }

and this did work. I'm not sure, maybe it's a bug in the docs?

bonzai commented 5 years ago

This issue board is for tracking bugs, not general development questions. Please direct general questions to #nova on Laravel's Discord or #nova on the Larachat Slack Channel.

4unkur commented 5 years ago

I reported a bug, this is not a question. withMeta is not working with cards like it is described in the docs. Check https://nova.laravel.com/docs/1.0/customization/cards.html#card-options

There is a method which passes some data via withMeta:

    public function currentVisitors()
    {
        return $this->withMeta(['currentVisitors' => true]);
    }

in the Card.vue I was not able to get currentVisitors, but was able to get it only when I passed it in NovaServiceProvider (described in the comment above)

bonzai commented 5 years ago

I can't confirm, so I don't think this is a bug.

1.

$ php artisan nova:card vendor/test-card

2. TestCard.php

<?php

class TestCard extends Card
{
    // (...)

    public function currentVisitors()
    {
        return $this->withMeta(['currentVisitors' => 9943]);
    }
}

3. User.php (resource)

public function cards(Request $request)
{
    return [
        (new TestCard)->currentVisitors(),
    ];
}

4. Card.vue

export default {
    props: [
        'card',
    ],

    mounted() {
        console.log(this.card.currentVisitors)
    },
}

5.

zrzut ekranu 2019-01-24 10 38 58

bucksterbuck commented 5 years ago

Should totally include the part in the docs, where you call the function when you instantiate the class. I got caught up on this for a while too.

kopiaman commented 5 years ago

seriously this important part

public function cards(Request $request)
{
    return [
        (new TestCard)->currentVisitors(),
    ];
}

never mention in documentation! i scratched my head for many days for this thing

riddla commented 5 years ago

I ended up setting the meta data in the constructor of my custom card as follows:

    public function __construct()
    {
        parent::__construct();
        $this->withMeta(['currentVisitors' => true]);
    }

And i second (third ;)) that this should be added to the documentation.

mikoop79 commented 5 years ago

is this the same for Tools? not much in the docs about inserting data here

jbrooksuk commented 5 years ago

https://github.com/laravel/nova-docs/pull/224

cameronelliott commented 4 years ago

I ended up setting the meta data in the constructor of my custom card as follows:

    public function __construct()
    {
        parent::__construct();
        $this->withMeta(['currentVisitors' => true]);
    }

And i second (third ;)) that this should be added to the documentation.

@riddla I don't think I can thank you enough for sharing this tip. I would have been lost trying to figure out how to pass this stuff for cards which are not associated with resources, ie User. Thank you!

jbrooksuk commented 4 years ago

The Nova documentation is open source, if you'd like to contribute :) https://github.com/laravel/nova-docs

LiamKarlMitchell commented 1 year ago

In vue template I had to put card. in front of the meta keys then I was able to use it.

v-if="card.shouldShowSomething"
{{card.something}}