laravel-db-blade-compiler icon indicating copy to clipboard operation
laravel-db-blade-compiler copied to clipboard

Laravel 9.36.3: Illegal offset type in isset or empty

Open mbeckerle-xqueue opened this issue 3 years ago • 22 comments

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

mbeckerle-xqueue avatar Oct 21 '22 08:10 mbeckerle-xqueue

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

Flynsarmy avatar Oct 21 '22 09:10 Flynsarmy

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

mbeckerle-xqueue avatar Oct 21 '22 09:10 mbeckerle-xqueue

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

hoekie avatar Oct 23 '22 14:10 hoekie

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.

mbeckerle-xqueue avatar Oct 24 '22 07:10 mbeckerle-xqueue

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

hoekie avatar Oct 24 '22 21:10 hoekie

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

sagarsoani avatar Nov 21 '22 08:11 sagarsoani

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

Marcel-Sass avatar Dec 02 '22 14:12 Marcel-Sass

Cool! Thanks @Marcel-Sass

I will try this in the next few days!

hoekie avatar Dec 12 '22 21:12 hoekie

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.

devansh-webkul avatar Dec 15 '22 06:12 devansh-webkul

No Solution Until Now ??

amouchaldev avatar Dec 17 '22 21:12 amouchaldev

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

adityaerlangga avatar Dec 24 '22 03:12 adityaerlangga

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))

RamceI avatar Dec 27 '22 17:12 RamceI

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

mrtsven avatar Jan 03 '23 09:01 mrtsven

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.

zfhassaan avatar Feb 17 '23 11:02 zfhassaan

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

Flynsarmy avatar Feb 17 '23 12:02 Flynsarmy

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

Flynsarmy avatar Feb 17 '23 12:02 Flynsarmy

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

error

edwinricaurte avatar Apr 12 '23 20:04 edwinricaurte

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

edwinricaurte avatar Apr 13 '23 17:04 edwinricaurte

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

bastos71 avatar Jul 28 '23 16:07 bastos71

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.

Flynsarmy avatar Jul 29 '23 04:07 Flynsarmy

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 🙂

bastos71 avatar Jul 31 '23 07:07 bastos71

Done

Flynsarmy avatar Jul 31 '23 08:07 Flynsarmy