Edit only single record with custom page
hi! im newbie in filament and php(laravel), so my question is next:
can you explain to me please how i can do edit page like there (https://laraveldaily.com/post/filament-edit-only-single-record-custom-page) but with filament 2.x
on our project we use exactly it.
for now i created a custom page via command(php artisan make:filament-page EditNotificationBoard) and its content is:
<?php
namespace App\Filament\Pages;
use Filament\Forms\ComponentContainer;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Pages\Page;
use Filament\Resources\Form;
/**
* @property Form $form
*/
class EditNotificationBoard extends Page
{
use InteractsWithForms;
public ?array $data = [];
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.pages.edit-notification-board';
public function mount(): void
{
$this->form->fill();
}
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('message')
->required(),
]);
}
}
the next is my blade.php file and its content is:
<x-filament::page>
<x-filament::form>
{{ $this->form }}
</x-filament::form>
</x-filament::page>
so, after all of that, i cant see my form on the page(looktat the screen)
Please, help me!!!
and my second question is:
could you show me how i can store data as json file on the disk(localy)?
Thanks in advance
Laravel Daily
Filament: Edit Only Single Record with Custom Page
22 Replies
Thanks in advance!
@awcodes thanks a lot!
could you please help me with my second question?
i really cant find any info about it. is it possible to store data localy as json file?
in your mount;
public function mount(): void
{
$arr = json_decode($json, true);
$this->form->fill($arr);
}
and when you save you can modify your save function
@S. Mert ÖZTÜRK thank you! i got you.
so could you please tell, did i do it right?
code snippet:
<?php
namespace App\Filament\Pages;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Pages\Page;
class EditNotificationBoar extends Page implements HasForms
{
use InteractsWithForms;
public $json = '';
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.pages.edit-notification-boar';
public function mount(): void
{
$arr = json_decode($this->json, true);
$this->form->fill($arr);
}
protected function getFormSchema(): array
{
return [
TextInput::make('message')
->required()
->maxLength(255)
->label(__('labels.notification_board.message'))
];
}
public function edit(): void
{
$data = $this->form->getState();
$this->json = json_encode($data);
$filePath = storage_path('app/public/data.json');
file_put_contents($filePath, $this->json);
}
}
and i would really appriciate it if you coudl help me to use this method to save data from form here "
code snippet:
<x-filament::page>
<x-filament::form submit="edit">
{{ $this->form }}
<x-filament::button type="submit">Зберегти</x-filament::button>
</x-filament::form>
</x-filament::page>
for now, it does some request, but nothing saved
can you make dd($this->form->getState()) on edit function
and please check your storage path for any creation file
like this?
no not assign, edit function first lane dd() direct
and click save button, look screen what are come
public function edit() {
dd($this->form->getState());
..... other codes here not need remove
}
unfortunately nothing changed
i check my storage path twice and didnt find anything
must the public folder already exist in the app folder? because i dont have the first one
now click your save button, you need to see your form post on screen
did you mean this?
no, your button orange color. click it
i clicked. 2nd and 3rd screens that what i got
i can cick one more time
in your view > form tag change this
wire:submit="edit"
<x-filament::form wire:submit="edit">
and try again
okay, let me a min
so i got it but it shows up for a few seconds
I wrote to you in a private message. If it is relevant, I can help.
okay we get form correct. So the error is on creating to json file.
Storage::disk('public')->put('data.json', json_encode($data));
you can try this
thanks every one🤗
issue is solved✅ . for other who has same problem as me, i'll load code to github later