Flynsarmy / laravel-db-blade-compiler

Render Blade templates from Eloquent Model Fields
170 stars 38 forks source link

Laravel 9.36.3: Illegal offset type in isset or empty #58

Open mbeckerle-xqueue opened 2 years ago

mbeckerle-xqueue commented 2 years ago

Hi,

with an update from Laravel 9.34.0 to 9.36.3 I get this error when creating a view from database: Illegal offset type in isset or empty

I compared the files before and after the update and this is the commit that breaks this project: https://github.com/laravel/framework/commit/331bf9c2e393e9b459a05c0d01d89cf9fdd20282

In DbView (https://github.com/Flynsarmy/laravel-db-blade-compiler/blob/master/src/Flynsarmy/DbBladeCompiler/DbView.php) the path is set to be the model: $this->path = $model;

Later the content is set to $this->path->__db_blade_compiler_content_field which is then passed over getContents() to the engine: $this->engine->get($this->path, $this->gatherData())

Laravels compiler engine expects a string in "public function get($path, array $data = [])" and since that commit they even use that path an array key, which fails in the end, was currently a model is used.

I am not so deep into all the internal template handling, so I hope my findings can at least help you to fix this.

Marcus

Flynsarmy commented 2 years ago

If you send a PR I'd be happy to merge :)

mbeckerle-xqueue commented 2 years ago

Hi,

unfortunately I only found the source of the problem but I do not know how to solve it in a correct way so it still / again works. I was hoping you see the above information and have already a solution in your mind, once you are done reading :D

hoekie commented 2 years ago

Hi, I've the same problem after an update to laravel 9.36.3: Illegal offset type in isset or empty. Unfortunately, I don't have the knowledge to solve this...

Please, can you give us a solution to this?

Thanks in advance.

Richard

mbeckerle-xqueue commented 2 years ago

Just for your information: I temporarily "solved" this by fixing my Laravel Framework to the latest 9.35 version

"laravel/framework": "9.35.1",

The critical commit was added in the first 9.36 release. Unfortunately I also have not really found a solution, so I hope for @Flynsarmy to better understand how it should be right.

hoekie commented 2 years ago

Hi @mbeckerle-xqueue,

I will try this in a couple of days... Thanks for your comment on this!

I hope it will be fixed soon.

Thanks!

Richard

sagarsoani commented 2 years ago

Is this issue solved ? i found this also on this 9.40.1 version. @Flynsarmy @hoekie

Marcel-Sass commented 1 year ago

i can propose a quickfix for someone to include in this package:

in Flynsarmy/DbBladeCompiler/DbBladeCompilerEngine.php add

/**
     * @param string $compiled_path
     * @param array $data
     * @return string
     */
    public function getContent(string $compiled_path, array $data)
    {
        return $this->evaluatePath($compiled_path, $data);
    }

and update \Flynsarmy\DbBladeCompiler\DbView::getContents to

protected function getContents()
    {
        $field = $this->config->get('db-blade-compiler.model_property');
        $this->path->{$field} = $this->content_field;
        $compiler = $this->engine->getCompiler();
        $compiler->compile($this->path);

        return $this->engine->getContent($compiler->getCompiledPath($this->path), $this->data);
    }

maybe this could be improved but it works for now

hoekie commented 1 year ago

Cool! Thanks @Marcel-Sass

I will try this in the next few days!

devansh-webkul commented 1 year ago

Is anyone fixing this, otherwise we have no option we need to remove the dependency?

Because we need to update the dependencies for our users.

amouchaldev commented 1 year ago

No Solution Until Now ??

adityaerlangga commented 1 year ago

I'm fixing it with change laravel version to "laravel/framework": "9.35.1" in your_app_name/composer.json and changing APP_URL in .env from APP_URL=http://localhost:8000 to APP_URL=http://127.0.0.1:8000. Might this help you all

RamceI commented 1 year ago

The problem is with the path src/Illuminate/View/Engines/CompilerEngine.php in the function get() line 61 inside if(!isset($this->compiledOrNotExpired[$path])) the variable $path is not sent as an array but full path and that's why it can't produce an array.

As solved is if(!isset($path))

mrtsven commented 1 year ago

Hey for everyone trying to render blade, you could use: https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates Hopefully this helps!

zfhassaan commented 1 year ago

i can propose a quickfix for someone to include in this package:

in Flynsarmy/DbBladeCompiler/DbBladeCompilerEngine.php add

/**
     * @param string $compiled_path
     * @param array $data
     * @return string
     */
    public function getContent(string $compiled_path, array $data)
    {
        return $this->evaluatePath($compiled_path, $data);
    }

and update \Flynsarmy\DbBladeCompiler\DbView::getContents to

protected function getContents()
    {
        $field = $this->config->get('db-blade-compiler.model_property');
        $this->path->{$field} = $this->content_field;
        $compiler = $this->engine->getCompiler();
        $compiler->compile($this->path);

        return $this->engine->getContent($compiler->getCompiledPath($this->path), $this->data);
    }

maybe this could be improved but it works for now

Thank you for sharing. It solved the issue.

Flynsarmy commented 1 year ago

It looks like you closed the PR. Was there a problem with it?

Flynsarmy commented 1 year ago

If someone could test and confirm the issue is now fixed I'll close this issue.

edwinricaurte commented 1 year ago

I'm getting an error on Laravel 10 (using @section)

error

edwinricaurte commented 1 year ago

The only quick solution for this specific issue on Laravel 10, is adding the View Factory class to the data variable on

vendor/flynsarmy/db-blade-compiler/src/Flynsarmy/DbBladeCompiler/DbBladeCompilerEngine.php

public function getContent(string $compiled_path, array $data) { $data['__env'] = app(\Illuminate\View\Factory::class); return $this->evaluatePath($compiled_path, $data); }

And updating function getContents() on the the file (@Marcel-Sass suggestion):

vendor/flynsarmy/db-blade-compiler/src/Flynsarmy/DbBladeCompiler/DbView.php

protected function getContents() { $field = $this->config->get('db-blade-compiler.model_property'); $this->path->{$field} = $this->content_field; $compiler = $this->engine->getCompiler(); $compiler->compile($this->path);

 return $this->engine->getContent($compiler->getCompiledPath($this->path), $this->data);

}

Screenshot 2023-04-13 at 1 54 43 PM
bastos71 commented 1 year ago

Could you please publish a new release with the fix merged in master ? thx !

Flynsarmy commented 1 year ago

It sounds like there's an issue on Laravel 10 (latest version). That'll need to get fixed before I'll make a new release. If someone wants to send a PR I'll merge.

bastos71 commented 1 year ago

The package does not work with Laravel 9 & Laravel 10 for now At least the last commit on master makes it compatible with Laravel 9 (wich is my use case, and I assume for others as well), it would be great to publish a release that fixes at least half of the problem 🙂

Flynsarmy commented 1 year ago

Done