jak w temacie- do bazy danych dodaje pustą wartosc 'body'. jestem amatorem i nie ogarne tego bez Waszej pomocy. (ten if($this->newPost='') { return; } tez nie dziala bo niby nie powinno nic robic, jak jest pusty, ale dodaje...
<form wire:submit.prevent="addPost">
<input type="text" wire:model.lazy="newPost">
<button type="submit">
Dodaj
</button>
</form>
class Show extends Component
{
public $posts;
public $newPost;
public function mount()
{
$initPosts=Post::latest()->get();
$this->posts=$initPosts;
}
public function addPost()
{
if($this->newPost='')
{
return;
}
$createdPost = Post::create([
'body'=>$this->newPost, 'user_id' =>2]);
$this->posts->prepend($createdPost);
$this->newPost="";
}
public function render()
{
return view('livewire.posts.show');
}
}
model Post
class Post extends Model
{
use HasFactory;
protected $fillable= ['body', 'user_id'];
protected $guarded = [];
public function comments()
{
return $this->hasMany('App\Models\Comment');
}
public function creator()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
}
migracja posts
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->text('body');
$table->foreignId('post_id')
->references('id')
->on('posts')
->onDelete('cascade');
$table->foreignId('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});