[SOLVED] Sage 10 - Components and functions in blade

Hi there!

I’m using Blade components a lot as they are amazing. I’ve done React before, and there’s one thing that I’m missing, how do I add a specific function to a component, and split out the value?

So for example, if you have a component product, you’ll pass all the props/values and you’ll have a populated product list using one |component/HTML, however, if you pass in a ‘created date’, you would need to create function, run the function and split it out as a ready made value ready to display.

How do I achieve this in Sage/blade?

This is my product component:

<?php

namespace App\View\Components;

use Roots\Acorn\View\Component;

class Product extends Component
{
    
    public $message;
    public $class;
    public $permalink;
    public $thumbnail;
    public $alt;
    public $title;
    public $wineCountry;

    public $createdDate;

    public $yearOfOrigin;
    public $alcohol;
    public $size;
    
    public $price;
    public $salePrice;
    public $regularPrice;
    public $isOnSale;

    public $test;

    public $isNewProduct;

    // function isNewProductFunction() {
    //     $newness_days = 30;
    //     $created = strtotime( $createdDate );
    //     if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
    //         $isNewProduct = true;
    //     }
    // }
    
    public function __construct(
        $message = null,

        $product = null,
        $class = null,

        $permalink = null,
        $thumbnail = null,
        $alt = null,
        $title = null,
        $wineCountry = null,

        $createdDate = null,
        // $isNewProduct = null,
        $yearOfOrigin = null,
        $alcohol = null,
        $size = null,

        $price = null,
        $salePrice = null,
        $regularPrice = null,
        $isOnSale = null,
        $test = null
       
    ) {
        $this->product = $product;
        $this->message = $message;
        $this->class   = $class;
        $this->permalink = $permalink;  
        $this->thumbnail = $thumbnail;
        $this->alt = $alt;
        $this->title = $title;
        $this->wineCountry = $wineCountry;


        $this->yearOfOrigin = $yearOfOrigin;
        $this->alcohol = $alcohol;
        $this->size = $size;

        $this->createdDate = $createdDate;
        // $this->isNewProduct = 

        $this->price = $price;
        $this->salePrice = $salePrice;
        $this->regularPrice = $regularPrice;
        $this->isOnSale = $isOnSale;
        $this->test = $test;
    }


    public function render()
    {
        return $this->view('components.product');
    }
}

 

Its as easy as:

 public function isNewProduct(): string
    {
        $newness_days = 30;
        $created = strtotime( $createdDate );
        if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
            return true;
        }
        
    }

Great article: https://dev.to/zubairmohsin33/laravel-7-functions-in-blade-components-2ok7

EDIT: Actually, I was missing public on the function/method whatever you want to call that.

This topic was automatically closed after 42 days. New replies are no longer allowed.