Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Desssha's avatar

How can call two Different blade inside livewire Component class

hello i try to return view in another function in the same component i don't want use redirect , want use it like contoller one component for all method (create,edit) , i can do it or only should redirect create to creatComponent ?

<?php

namespace App\Http\Livewire;

use App\Models\Post;
use Illuminate\Support\Facades\File;
use Livewire\Component;
use Livewire\WithPagination;

class Posts extends Component
{
    use WithPagination;
    protected $paginationTheme = 'bootstrap';



    public function render()
    {
        $posts = Post::with('user','category')->orderBy('id','desc')->paginate(5);

        return view('livewire.posts',['posts' => $posts])
            ->extends('layouts.app')
            ->section('content');
    } //END METHOD

    public function create_post()
    {

        return redirect()->to('livewire/create/post');
    }

    public function edit_post($id)
    {
        $post = Post::whereId($id)->whereUserId(auth()->id())->first();
        if ($post){
            return redirect('livewire/edit/post/'.$id);
        }
       session()->flash('message',"you can't update not yours");

    }/* END METHOD */


    public function delete_post($id)
    {
        $post = Post::whereId($id)->whereUserId(auth()->id())->first();
        if ($post){
            if (File::exists('assets/images/'.$post->image)){
                 unlink('assets/images/'.$post->image);
            }
            $post->delete();

            Session()->flash('message_success',"post deleted successfully");
        }
        Session()->flash('message_error',"you can't delete not yours");
    } /* END METHOD */

    public function show_post($id)
    {
        $post = Post::whereId($id)->first();
        return view('livewire.show-post',['post' => $post]);
    }

}

look to last function show_post it not work not back for me this view i shold make redirct like was before but i not like use more file , there solve for it? thanks for help

0 likes
3 replies
Snapey's avatar

only return view from the render method . There you can switch view if you must, but you might like to think about the principle of single responsibility

1 like
Desssha's avatar

@Snapey i know that return view only from render and i ask there no way to use only one component for all method like controller will be better than create more route and files for component create,edite , so i asked , and thanks for answer

cg0012's avatar

tip: use model binding in your methods, you don't have to query it:

public function delete_post(Post $post)
    {
        if ($post){
            if (File::exists('assets/images/'.$post->image)){
                 unlink('assets/images/'.$post->image);
            }
            $post->delete();

declare a variable for when you want your additional blade content to show and your passed child model:

public $selectedPost ;
public $deletingPost = false;

public function mount()
{
	$this->selectedPost = Post::make();
}

public function deletePost(Post $post)
{
    $this->selectedPost = $post;
	$this->deletingPost = true;
}

//you will need to have a listener from your child component to turn off the additional content

protected $listeners = ['postDeleted']

public function postDeleted()
{
	$this->deletingPost = false;
}
			

look into livewire nested components within your main Post component: https://laravel-livewire.com/docs/2.x/nesting-components

and events: https://laravel-livewire.com/docs/2.x/events

within your livewire.posts blade file:

(a modal would be a good way to do what you're attempting)

<x-app-layout>
	<!-- posts listing with buttons for functions -->
	@foreach($posts as $post)
	    <span>{{ $post->name }}</span> <!-- or something -->
	
        <button type="button" wire:click="delete_post({{ $post->id }})">
		  Delete
	   </button>
   @endforeach

		<div>
			@if($deletingPost)
        			@livewire('delete-post', ['post' => $selectedPost])
			@endif
    	</div>
</x-app-layout>
1 like

Please or to participate in this conversation.