how to keep laravel livewire wire:submit.prevent from reloading the page - laravel

I have a pretty simple livewire component that helps a user store a vanity URL
<form wire:submit.prevent="save">
<div class="form-group form-inline">
<span>{{ config('app.url') }}/request/</span>
<input type="text" id="vanity" wire:model.defer="vanityUrl" name="vanityUrl">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">#if($saved) Saved #else Save #endif</button>
<button type="button" id="copy-vanity" class="btn btn-secondary">Copy</button>
</div>
</form>
And the save action is pretty simple as well.
public function save()
{
$user = Auth::user();
$this->validate();
$user->update(['vanity_slug' => Str::slug($this->vanityUrl)]);
$this->vanityUrl = Str::slug($this->vanityUrl);
$this->saved = true;
}
This works locally but not in production. I am using Forge, php7.4, L8, its all the same between the environments except the URLs. I have livewire:scripts and livewire:styles loaded, and again it works locally but not in prod.
In prod, the page reloads. I have been trying to figure this out for a couple days now... driving me crazy. TIA

Cached Views
The most typical issue when Livewire isn't working after deploying to a production environment (or for that matter, after setting it up in localhost as well), is that your views, specifically layouts/app.blade.php is cached before the #livewireScripts directive is loaded. This means that it will not render #livewireScripts or #livewireStyles as the component, but will output the literal string.
This is fixed by simply running php artisan optimize:clear, to clear your cache (specifically the view cahce).
Missing Installation of Livewire
However, there are some cases where Livewire is not installed - or not installed properly. You can ensure that Livewire is installed through composer by running composer show -D while in the root directory of your Laravel application. Look for livewire/livewire. If its not there, then install it (see https://laravel-livewire.com/docs/2.x/installation). To install Livewire through composer, run the following command.
composer require livewire/livewire
Using Alpine.js v3 and Livewire v2w
If you have updated to Apline.js v3, you need to use Livewire v2.5 or higher. Simply update your Livewire version.
Faulty Installation of Livewire
The last scenario I've heard of so far with this kind of problem, is that Livewire is installed - but its still not working properly. This is the most uncommon from what I have seen so far. This happens when the scripts that Livewire hooks into is not working as intended (say, a form with wire:submit.prevent="submit" is submitting the actual form instead of sending the AJAX request), but Livewire is installed and #livewireScrtips is rendering as it should. For whatever reason this happens, I'm not sure (as I haven't experienced it first hand), but its quite simple to fix.
The solution for these scenarios is to reinstall Livewire - and you do that by simply removing and require the package again. After the reinstall, its a good idea to clear the cache, just to ensure that the view is not cached without the Livewire-directives.
composer remove livewire/livewire && composer require livewire/livewire && php artisan optimize:clear

Related

Laravel 8 Livewire wire:click:prevent Not Working

Please Someone please I am in the middle of my final year project but this is not working I am trying for 3 days I am new so I don't know how to ask a question so if you need something please ask me Thanks
This is the view for Shop and i have used Composer require Hardevine/shoppingcart
This is the ShopComponent for Shop
When I ran Laravel 8 on IIS, I had a similar problem. The problem was that the browser couldn't access some of the livewire js files, and the solution was:
Go to D:\Projects\Laravel\laravel8ecommerce\vendor\livewire\livewire\config\livewire.php
and set
'asset_url' => "your_url",
and the problem was disappeared.
I had same problem. To solve this issue what I did was enclose my blade template files with a <div> containing an id="main" like so:
<div id="main">
your blade template code
<div>
Instead Of This
wire:click:prevent=“store({{$product->id}},’{{$product->name}}’,{{$product->regular_price}})”
You need to use this in your blade file
#livewireStyles
your code will be in this area
#livewireScripts
Try This For Calling Store Function
wire:click.prevent="store({{$product->id}},{{$product->name}},{{$product->regular_price}})”
Or
wire:click="store({{$product->id}},{{$product->name}},{{$product->regular_price}})”
My solution was based on #KumTem answer
Inside the livewire blades located at app\resources\views\livewire\sample_blade.php
<div id="main">
... templates here
</div>

Laravel API route - exception: "Symfony\Component\HttpKernel\Exception\NotFoundHttpException"

I'm making a Blog for school on my portfolio website, now I'm doing this in VueJS and Laravel and for this I need API routes.
Now I want to delete a comment with a specific ID but when I push the delete button it gives the error:
exception: "Symfony\Component\HttpKernel\Exception\NotFoundHttpException"
The error is in the {routeCommentID} part of the next route:
Route::post('/deleteComment/{routeCommentID}', 'CommentController#delete');
What did I do wrong? Because when I remove that it works fine, but I need this part because I have to remove a comment with a specific ID.
Run php artisan route:list and check if route like '/deleteComment/{routeCommentID}' exists and whether you use that route in your Vue application.
for deleting a post it's better to use
Route::delete('/deleteComment/{id}', 'CommentController#delete');
and checkout your blade for deletion
it should be something like below
<form action={{ 'wanted route' }} method="post">
#csrf
#method('delete')
// your code
</form>

Vue components not showing up in laravel project

I am working in Laravel , and Vue components are showing/updating in local but not showing in server .
I don't know what the problem is,because I have all the packages of vue and node installed on the server.
app.js picture is:
i used npm run dev ,npm run production npm run watch They all worked without error but still show nothing
please help me What can I do؟
You didn't call your component in dashboard.blade.php file.
<div id="app">
<login-component></login-component>
</div>
Or if you want to call example component then:
<div id="app">
<login-component></login-component>
</div>

Laravel 5.6. A small part of blade file not updated in cache

I have a blade view file in my project.
The HTML structure is something like :
<div class="a">
<div class="b">
</div>
<div class="c">
</div>
<div>
The problem is that whenever I update anything inside class c, the changes are not rendered.
I have cleared configuration cache as well as view cache as :
php artisan config:clear && php artisan config:cache
php artisan view:clear && php artisan view:cache
This doesn't help either.
Upon further inspection, I found out that, the cache file in /project_root/storage/framework/views is separately kept for a specific part of the page only, in my case the div with class c.
I have no idea how it ended up keeping a separate cache for a small segment of a page. Can anybody suggest me a solution.
EDIT : I have tried deleting the entire cache folder /project_root/storage/framework/views as well in vain.
Other relevant informations :
Hosted server : Nginx on ubuntu
The sendfile option in nginx.conf is ON. If this is relevant.
Some ideas:
clear browser cache
check permissions to storage directory (it should be 777)
be sure that your css is compiled (if you use webpack, laravel mix or gulp or something else)
check if your css is loaded with your changes
change css file name (read about notation main.css?3.4.1 - each time when you make change in css or js it changes value after "?")
After change:
<link rel="stylesheet" href="style.css?v=3.4.2">
And:
<?php $cssVersion = "3.4.2"; ?>
<link rel="stylesheet" href="global.css?v=<?php echo $cssVersion; ?>">
If you use PHP Storm:
open menu File → Settings
go to Deployment → Options section
then uncheck option Preserve files timestamps
Good luck!

Laravel (5.2) Blade - How to prevent a Blade Directive in an email address from being parsed?

Update This is a bug in Laravel 5.2 & 5.3
I've got a weird one here. A user's email address on our client's system has a domain with the following substring in it '#parent'. I am not including the whole thing just for the sake of privacy.
Because #parent is a Blade directive, Laravel seems to either process or ignore the #parent and strips it out of the rendered email address on the page.
For example, let's say the email address is john#parentstuff.com. Laravel will render the following on the page: johnstuff.com. See how it removes #parent from the email?
What I've tried to fix it:
1. {!! $user->email !!}
2. {{ e($user->email) }}
I know that this is an issue with Blade as AJAX & jQuery rendered content with this same email address is displayed just fine elsewhere on the site.
UPDATE
Upon further investigation, it appears this may be a bug in how Blade processes the #parent directive. I set up 3 new Laravel projects for the following versions: 5.2, 5.3 and 5.4. The project in question is a Laravel 5.2 project, FYI.
I created the following setup for each of the above mentioned versions to test the bug.
Route
Route::get('/test', function () {
$foo = 'john#parentingstuff.org';
return view('test')->with('foo', $foo);
});
Templates
Base
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<div class="container">
#yield('content')
</div>
</body>
</html>
Test View
#extends('test-base')
#section('content')
{{$foo}}
#endsection
Outputs
L5.2: johningstuff.org
L5.3: johningstuff.org
L5.4: john#parentingstuff.org
Theories
One very interesting case I noticed while running these tests was that having the {{$foo}} variable inside of a #section directive vs moving it outside of the #section directive led to two different outputs.
Example
{{$foo}}
#section('content')
{{$foo}}
#endsection
The output of this was...
john#parentingstuff.org
johningstuff.org
Conclusion
So, does anyone know how to patch this bug in Laravel 5.2 or Laravel 5.3? Currently, upgrading to L5.4 is not an option for our client.
Anyone have any clever tricks they can think of that might help?
I believe the problem is elsewhere. You are doing probably something more than you wrote.
Sample controller method content
return view('test', ['email' => 'john#parentstuff.com'];
Sample view:
Email is: {{ $email }}
Result is:
Email is: john#parentstuff.com
so I believe you are doing something more than you wrote.
Something very screwy is going on in your setup, because that shouldn't be possible. Laravel processes Blade instructions before interpreting any variables.
I tested with a very simple example:
Route::get('test', function() {
$foo = 'john#parentstuff.com';
return view('test')->with('foo', $foo);
});
and a Blade template of:
{{ $foo }}
and it works just fine. #parent is not interpreted.
This is a bug in Laravel verions prior to 5.4. See the following:
https://github.com/laravel/framework/issues/10068
https://github.com/laravel/framework/pull/16033
This is a bug in Laravel 5.1 too.
A quick solution could be to change the # sign to
#
to fix it.

Resources