Skip to content

Instantly share code, notes, and snippets.

@stevepop
Created March 25, 2020 13:15
Show Gist options
  • Save stevepop/a17189a3ef7a3f0a14e9bac76d1ef67b to your computer and use it in GitHub Desktop.
Save stevepop/a17189a3ef7a3f0a14e9bac76d1ef67b to your computer and use it in GitHub Desktop.
// post-index page
<div>
<livewire:post-create>
@foreach($posts as $post)
<livewire:post-single :post="$post" :key="$post->id">
@endforeach
</div>
// PostIndex.php
namespace App\Http\Livewire;
use App\Post;
use Livewire\Component;
class PostIndex extends Component
{
public function render()
{
return view('livewire.post-index', [
'posts' => Post::latest()->get()
]);
}
}
// Create Post
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class PostCreate extends Component
{
public $body;
public function addPost()
{
auth()->user()->posts()->create(['body' => $this->body]);
$this->body = '';
}
public function render()
{
return view('livewire.post-create');
}
}
// Post form
<div class="mb-4">
<form wire:submit.prevent="addPost">
<div class="form-group">
<label for="body" class="sr-only">Your post</label>
<textarea name="body" class="form-control" wire:model="body"></textarea>
</div>
<button type="submit" class="btn btn-primary">
Post
</button>
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment