I am trying to render the posts table every time a post is created.
For this I have created the CreatPost component as follows:
class CreatePost extends Component
{
public $title, $corpo;
public function save()
{
Post::create([
'title' => $this->title,
'corpo' => $this->corpo
]);
session()->flash('success', 'This is the message');
$this->reset(['title','corpo']);
$this->emit('postCreate');
}
public function render()
{
return view('livewire.create-post');
}
}
and the ShowPosts component like so...
class ShowPosts extends Component
{
protected $listeners = [
'postCreate' => 'render'
];
public function render()
{
$posts = Post::all();
return view('livewire.show-posts',compact('posts'));
}
}
Every time I create the post, the component saves the post in the database correctly, sends the session message correctly, resets the form data correctly and sends the 'postCreate' event correctly as shown in the image...
Only the ShowPosts component doesn't render again. When refreshing the page with f5, the new saved post is correctly displayed.
I leave the composer.json file so you can see the different versions of each library.
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.75",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5",
"laravel/ui": "^3.4",
"livewire/livewire": "^2.10"
},
"require-dev": {
"facade/ignition": "^2.5",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^5.10",
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
Thanks for the help...