Coder Social home page Coder Social logo

Comments (11)

georgehanson avatar georgehanson commented on June 20, 2024 3

@njoguamos that makes sense, passing an array would probably be a good feature to have.

from inertia-laravel.

njoguamos avatar njoguamos commented on June 20, 2024 2

@georgehanson Thanks for noting that. It was a typo and have updated my comment.

However, I dug deeper to Inertia and found out that withViewData does not work array

    public function withViewData($key, $value)
    {
        $this->viewData[$key] = $value;
        return $this;
    }

Instead it should be as suggested by this PR

    public function withViewData($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $value) {
                $this->viewData[$k] = $value;
            }
        } else {
            $this->viewData[$key] = $value;
        }
        
        return $this;
    }

I hope am clear 😄

from inertia-laravel.

nckrtl avatar nckrtl commented on June 20, 2024 1

Hi @njoguamos

You can already pass an array, withViewData is not needed.

public function index() {
    return Inertia::render('Contact/Index', [
        'title'       => 'Contact Us | My App',
        'description' => 'Contact us with your comments, questions, and suggestions',
        'meta'        => 'Some other SEO meta'
    ]);
}

If you'll define the props (title, description and meta) in your Index.vue the values will become available.

from inertia-laravel.

connecteev avatar connecteev commented on June 20, 2024 1

+1 on this...unless I'm missing a way to update the content in the tag for SEO?

from inertia-laravel.

enzonotario avatar enzonotario commented on June 20, 2024 1

@connecteev check this: https://github.com/inertiajs/inertia-laravel#accessing-data-in-root-template

from inertia-laravel.

njoguamos avatar njoguamos commented on June 20, 2024

I believe the idea of @reinink having withViewData is to be able to pass variables to Laravel blade template.

@nckrtl the solution you provided works only if one want to pass data to vue (which by the way is okay) but I want the array data to be accessible to my app.blade.php

from inertia-laravel.

nckrtl avatar nckrtl commented on June 20, 2024

aha, in that case I misunderstood your question.

from inertia-laravel.

njoguamos avatar njoguamos commented on June 20, 2024

😄 Not really, I am the one who did not explain well.

from inertia-laravel.

georgehanson avatar georgehanson commented on June 20, 2024

@njoguamos I noticed there is a comma after specifying the Vue Component name. The render method on the ResponseFactory has the second argument default to an empty array if it has not been provided, so this exception shouldn't be thrown. Have you tried changing it from this:

public function index() {
    return Inertia::render('Contact/Index',)->withViewData([
        'title'       => 'Contact Us | My App',
        'description' => 'Contact us with your comments, questions, and suggestions',
        'meta'        => 'Some other SEO meta'
    ]);
}

To this:

public function index() {
    return Inertia::render('Contact/Index')->withViewData([
        'title'       => 'Contact Us | My App',
        'description' => 'Contact us with your comments, questions, and suggestions',
        'meta'        => 'Some other SEO meta'
    ]);
}

from inertia-laravel.

connecteev avatar connecteev commented on June 20, 2024

@enzonotario thanks

from inertia-laravel.

njoguamos avatar njoguamos commented on June 20, 2024

@reinink has updated withViewData to support array see commit

from inertia-laravel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.