File: /var/www/html/app/Livewire/Faq/Add.php
<?php
namespace App\Livewire\Faq;
use Livewire\Component;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class Add extends Component
{
public $gig_id;
public $question;
public $answer;
public $user_id;
protected $rules = [
'gig_id' => 'required|exists:gigs,id',
'question' => 'required|unique:faqs',
'answer' => 'required|string|max:255',
];
public function addGigFaq()
{
// Validate input data
try {
$this->validate();
// Store the FAQ data
$body = [
'gig_id' => $this->gig_id,
'question' => $this->question,
'answer' => $this->answer,
];
$apiEndpoints = new ApiEndpoints();
$headers = $apiEndpoints->header();
$response = Http::withHeaders($headers)
->withBody(json_encode($body), 'application/json')
->post(ApiEndpoints::addFaq());
if ($response->successful()) {
Session::flash('success', 'Successfully added');
return redirect()->to('/dashboard-faqs');
} else {
Session::flash('success', $response->json()['message']);
return redirect()->to('/dashboard-faq');
}
} catch (\Throwable $th) {
$this->addError('answer', $th->getMessage());
}
}
public function render()
{
$seller = session()->get('user');
$this->user_id = $seller['id'];
$gigs = DB::table('gigs')->where('user_id', $this->user_id)->get();
return view('livewire.faq.add', compact('gigs'));
}
}