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...
I think this will help with your problem.
Try changing the ShowPosts listener to this.
That will do the magic.
Good day,
The problem is
render
that you do:In the line
$posts = Post::all();
you only get the Posts that are in the database at the time the page is rendered and therefore when creating or editing elements it will not be reflected in your view.One option would be to make a variable that can be available in the view when using
mount
, as Fabián put in his answer, another option is to create a property that gets the rows from the database and pass the result to the view, this would work for you very much if at some point you were interested in creating an advanced search (for example that searches fields from different columns) of your data.You could create a property
rowsQuery
to get the rows fromPost
and include for example some lookup, assuming you have a variablefilters
to filter the table and asearch
lookup field you could use the methodwhen
, what I would do is, if itfilters['search']
has some value then it looks in the table the elements where the value offilters['search']
appears in the columnname
(You could ignore this and only do$query = Post::all()
it if you wanted to always get all the rows). But you could add otherswhen
to be able to filter each of your columns.And another property
rows
to get the rows after the search filterNote: You could do everything from a property but what I do
getRowsQueryProperty
is first generate the query with N number of filters and thengetRowsProperty
get the rows and if desired or necessary, apply pagination or ordering on this property, that's why I separate it, so that one property is just for the query and one that outputs the rows after any sorting, paging or other functions you need/want.This new property
rows
is the one you are going to pass inrender
( Note: being properties you can use them as$this->rowsQuery
and$this->rows
)The advantage of this method is that if you create, edit or delete elements, it will be reflected in the view and I think it makes the code clear so that it can be reused in the future in another application.