Certainly, here's an example of how you might set up the folder structure and code in Laravel Filament 2.0 to create a master-detail page that pulls data from two API endpoints:
App
├── Http
│ ├── Livewire
│ │ ├── MasterDetail.php
│ │ └── Details.php
│ └── Controllers
│ ├── MasterController.php
│ └── DetailController.php
└── Filament
├── Resources
│ ├── MasterResource.php
│ └── DetailResource.php
├── Forms
│ └── DetailForm.php
└── Tables
└── MasterTable.php
In the Http/Livewire folder, you would have two Livewire components: MasterDetail and Details. MasterDetail would be responsible for displaying the list of master records, while Details would be responsible for displaying the detail view. Here's an example of the code for MasterDetail:
namespace App\Http\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
class MasterDetail extends Component
{
public $masters;
public function mount()
{
$response = Http::withToken('bearer_token')
->get('http://example.com/api/masters');
$this->masters = collect($response->json());
}
public function selectMaster($id)
{
$this->emit('masterSelected', $id);
}
public function render()
{
return view('livewire.master-detail', [
'masters' => $this->masters
]);
}
}
In the Http/Controllers folder, you would have two controllers: MasterController and DetailController. These would be used to handle any backend functionality required by the application, such as pulling data from the API endpoints.
In the Filament/Resources folder, you would have two Filament resources: MasterResource and DetailResource. MasterResource would be responsible for displaying the list of master records in the Filament admin panel, while DetailResource would be responsible for displaying the detail view. Here's an example of the code for MasterResource:
namespace App\Filament\Resources;
use Filament\Resources\Resource;
use App\Filament\Resources\Tables\MasterTable;
use App\Filament\Resources\Forms\DetailForm;
class MasterResource extends Resource
{
public static $icon = 'heroicon-o-user-group';
public static function label()
{
return 'Masters';
}
public static function table()
{
return new MasterTable();
}
public static function form()
{
return new DetailForm();
}
}
In the Filament/Resources/Tables folder, you would have a Filament table: MasterTable. This would be used to display the list of master records in the admin panel. Here's an example of the code for MasterTable:
namespace App\Filament\Resources\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns;
use Illuminate\Support\Collection;
class MasterTable extends Table
{
public function columns(): array
{
return [
Columns\Text::make('name')->primary(),
Columns\Text::make('email'),
Columns\Text::make('phone')->searchable(),
];
}
public function rowsPerPage(): int
{
return 10;
}
public function getUsers(): Collection
{
$response = Http::withToken('bearer_token')
->get('http://example.com/api/masters');
return collect($response->json());
}
}
In the Filament/Resources/Forms folder, you would have a Filament form: DetailForm. This would be used to display the detail view and allow the user to edit the detail record. Here's an example of the code for DetailForm:
namespace App\Filament\Resources\Forms;
use Filament\Forms\Form;
use Filament\Resources\Forms\Components;
use Illuminate\Support\Facades\Http;
class DetailForm extends Form
{
public function components()
{
return [
Components\TextInput::make('name')
->required()
->placeholder('Enter name'),
Components\TextInput::make('email')
->required()
->email()
->placeholder('Enter email'),
Components\TextInput::make('phone')
->required()
->placeholder('Enter phone number'),
];
}
public function beforeCreate()
{
$this->data['idb'] = $this->getMasterId();
}
public function beforeUpdate()
{
$this->data['idb'] = $this->getMasterId();
}
private function getMasterId()
{
$id = request()->input('id');
$response = Http::withToken('bearer_token')
->get("http://example.com/api/masters/$id");
$master = collect($response->json());
return $master['idb'];
}
}
Lastly, in the Http/Livewire folder you can define the blade views for each component:
The master-detail.blade.php to display the list of master records and emit the event when a record is clicked:
<div>
@foreach ($masters as $master)
<div class="flex my-2 cursor-pointer" wire:click="selectMaster({{ $master->id }})">
<div class="w-2/12 text-gray-700 text-sm font-bold">#{{ $master->id }}</div>
<div class="w-5/12 text-gray-700 text-sm">{{ $master->name }}</div>
<div class="w-5/12 text-gray-700 text-sm">{{ $master->email }}</div>
<div class="w-4/12 text-gray-700 text-sm">{{ $master->phone }}</div>
</div>
@endforeach
</div>
The details.blade.php to display the detail form:
<div>
<x-filament::form :submit="$form->submit()">
{{ $form->render() }}
</x-filament::form>
</div>
I hope this helps you get started with building your master-detail page in Laravel Filament 2.0! Let me know if you have any questions or need further assistance.