when using the @script blade directive, you don't need to use the script tag anymore just use
@script
//
@endscript
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello to everyone, when in my PHP Blade View I use:
@script
<script>
</script>
@endscript
It returns an Exception which is: "Using $this when not in object context" Why is that? My controller doesn't have even anything inside other than the render method and the mount method.
when using the @script blade directive, you don't need to use the script tag anymore just use
@script
//
@endscript
@salfel No, the error still persist.
@martinbean Im trying to use a Livewire script in a not "Livewire" view, but It still should work?
@woweya Well use it as documented, then? https://livewire.laravel.com/docs/installation#manually-including-livewires-frontend-assets
You should have a single @livewireScripts directive, not @scripts. And there’s no ending tag.
@martinbean Well, I am following the documentation:
in my layout.blade.php that it's the main layout for HTML:5, before the </body>, I used @livewireScripts but not in the webpage im using @script @endscript. Still it doesn't work
@martinbean There's a @script and @endscript that comes with Livewire: https://livewire.laravel.com/docs/javascript#using-javascript-in-livewire-components
@MohamedTammam I know, the problem was not anything you said, but the problem was the fact that it was not inside a livewire view.
@woweya i had the same problem today. So what is the general rule here? I can't figure it out. I mean @script helper definitely can be inside non-livewire blade component
Why are you using $this? Do you mean $wire?
@woweya, have you found a solution?
@woweya me too, had the same problem today. do you find solution for this?
When you add @script (Docs ), you no longer need to use $this(Available ONLY inside a Livewire View) in your JS, you should use $wire to access the Livewire Component. You havent stated what your purpose is or what you are trying to do, so can't really help you. Would be a lot better if you actually stated what you wanted to do with this and posted at least some code. Next time please add more details to the question, rather than not even saying what $this is used for, or what you are trying to do. You use @script tags IN LIVEWIRE BLADE VIEWS ONLY, otherwise it doesnt work, simple as that. If you need to add regular JS, just @push it to your main layout scripts.
-------------------------
If you are using @script in a Livewire Blade Component View, then:
If you have a Livewire variable public $postId this can be accessed in the JS like: $wire.postId. If you have a public function submitPost() then you can call $wire.submitPost in the @script.
Here's some other stuff you can do with $wire
THIS IS THE PURPOSE OF @script, I recommend reading the documentation about JS and AlpineJS more for workarounds in whatever it is you are trying to do.
Please or to participate in this conversation.