Home >Backend Development >PHP Tutorial >How to Redirect URL or Route using Laravel Livewire 3

How to Redirect URL or Route using Laravel Livewire 3

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-06 00:57:38222browse

How to Redirect URL or Route using Laravel Livewire 3

In this post, I will show How to Redirect URL or Route using Laravel Livewire 3.

In this example, we will create a PhotoUpload Livewire component. This component will feature a form with a file input field and include image validation. The user can select an image, which will then be uploaded to the storage folder using the WithFileUploads trait. After upload image we will redirect route with flash message. You Can Learn How To Session Flash Messages in Laravel Livewire 3

How to Redirect URL or Route using Laravel Livewire 3 Example

Step 1: Create PhotoUpload Component

Now here we will create a Livewire component using their command. So run the following command to create an add more component.
php artisan make:livewire PhotoUpload
Now they created files on both paths:

app/Livewire/PhotoUpload.php

resources/views/livewire/photo-upload.blade.php

Now, both files we will update as below for our contact us form.

app/Livewire/PhotoUpload.php

<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\Image;

class PhotoUpload extends Component
{
    use WithFileUploads;

    public $photo;

    public function render()
    {
        return view('livewire.photo-upload');
    }

    public function submit(){
        $this->validate([
            "photo" => "required|image"
        ]);

        $filepath = $this->photo->store("photos");

        $image = Image::create([
            "title" => "Test",
            "filepath" => $filepath
        ]);

        session()->flash("success", "Image uploaded successfully");

        return redirect()->route("home");
    }
}

Read More

The above is the detailed content of How to Redirect URL or Route using Laravel Livewire 3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn