ldjson

6 months ago

realtydev

Code

use App\Repositories\PropertyRepository;

use Spatie\SchemaOrg\Schema;
$propertyRepo = new PropertyRepository();
$property = $propertyRepo->findByExternalId('324216592')->data;

    // a description for SEO
 function headerDescription($data)
{
    $description = $data['unparsedAddress'] . ' is a ' . $data['propertySubType'] . ', ' . 	   $data['propertyType'] . ' property listed for $' . number_format($data['listPrice']);
    $description .= ' The property is ';
    $description .= $data['livingArea'] ?? '-';
    $description .= ' sq. ft with ';
    $description .= $data['bedroomsTotal'] ?? '-';
    $description .= ' bedrooms and ';
    $description .= $data['bathroomsTotal'] ?? '-';
    $description .= ' bathrooms';
    return $description;
}


if ($property) {
  $faqs[] = [
    'question' => 'What is '.$property['unstructuredAddress'].'?',
    'answer' => headerDescription($property),
  ];
}

if ($property['photosCount'] > 1) {
  $faqs[] = [
    'question' => 'How many photos are available for this home?',
    'answer' => 'Realty.com has '.$property['photosCount'].' photos of '.$property['unstructuredAddress'].'.',
  ];
}

if ($property['yearBuilt']) {
  $faqs[] = [
    'question' => 'When was this home built?',
    'answer' => $property['unstructuredAddress'].' was built in '.$property['yearBuilt'].'.',
  ];
}

if (isset($property['unparsedAddress'])) {
  $faqs[] = [
    'question' => 'What’s the full address of this home?',
    'answer' => 'The full address for this home is '.$property['unparsedAddress'].'.',
  ];
}

if (isset($property['listingContractDate'])) {
  $faqs[] = [
    'question' => 'How long has this home been listed on Realty.com?',
    'answer' => $property['unstructuredAddress'].' has been listed on Realty.com since '.\Carbon\Carbon::parse($property['listingContractDate'])->format('D F d, Y'),
  ];
}


//dd($faqs);


$faq_ldjson = Schema::fAQPage()
    ->mainEntity(array_map(function($faq) {
        return Schema::question()
            ->name($faq['question'])
            ->acceptedAnswer(
                Schema::answer()
                    ->text($faq['answer'])
            );
    }, $faqs))->toScript();

dd($faq_ldjson);

Output

"<script type="application/ld+json">{"@context":"https:\/\/schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is 2940 Olive Street?","acceptedAnswer":{"@type":"Answer","text":"2940 Olive Street, Denver, CO 80207 is a Single Family, Residential property listed for $456,400 The property is 737 sq. ft with 2 bedrooms and 1 bathrooms"}},{"@type":"Question","name":"How many photos are available for this home?","acceptedAnswer":{"@type":"Answer","text":"Realty.com has 31 photos of 2940 Olive Street."}},{"@type":"Question","name":"When was this home built?","acceptedAnswer":{"@type":"Answer","text":"2940 Olive Street was built in 1952."}},{"@type":"Question","name":"What’s the full address of this home?","acceptedAnswer":{"@type":"Answer","text":"The full address for this home is 2940 Olive Street, Denver, CO 80207."}},{"@type":"Question","name":"How long has this home been listed on Realty.com?","acceptedAnswer":{"@type":"Answer","text":"2940 Olive Street has been listed on Realty.com since Wed November 10, 2021"}}]}</script>"
Video course: PHP Package Development