Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DNABeast's avatar
Level 27

Passing a variable to a custom component.

Let's say I've got an object. Say, a bag. And a bag can have either a toy or a piece of fruit in it. I set the item type on the bag $bag['type'] = 'fruit'

Then I have a list of either toys or fruit in a Repeater.

I have a custom component called ToyOrFruitInput. and in this I want to know what the type of my item is based on the bag that it's in. Is there a way to pass a variable to a custom component (other than the state) or alternatively, can I get the value from the parent object whilst in the context of the custom component?

I want my component to have something like

@if ($component->parent['type'] == 'fruit'){
	{{ $getState() }} is a piece of fruit.
@else 
	{{ $getState() is a toy.
@endif
0 likes
1 reply
LaryAI's avatar
Level 58

To pass a variable to a custom component in a framework like Laravel Livewire or Vue.js, you can use props or attributes. Below, I'll provide a solution for both Laravel Livewire and Vue.js.

Laravel Livewire

In Laravel Livewire, you can pass data to a component using props. Here's how you can achieve this:

  1. Define the Component:

First, create a Livewire component called ToyOrFruitInput.

// app/Http/Livewire/ToyOrFruitInput.php
namespace App\Http\Livewire;

use Livewire\Component;

class ToyOrFruitInput extends Component
{
    public $type;

    public function mount($type)
    {
        $this->type = $type;
    }

    public function render()
    {
        return view('livewire.toy-or-fruit-input');
    }
}
  1. Create the Blade View:

Create a Blade view for the component.

<!-- resources/views/livewire/toy-or-fruit-input.blade.php -->
<div>
    @if ($type == 'fruit')
        {{ $getState() }} is a piece of fruit.
    @else
        {{ $getState() }} is a toy.
    @endif
</div>
  1. Use the Component in a Parent View:

Pass the type variable to the component from the parent view.

<!-- resources/views/parent-view.blade.php -->
@php
    $bag = ['type' => 'fruit'];
@endphp

<livewire:toy-or-fruit-input :type="$bag['type']" />

Vue.js

In Vue.js, you can pass data to a component using props. Here's how you can achieve this:

  1. Define the Component:

Create a Vue component called ToyOrFruitInput.

// components/ToyOrFruitInput.vue
<template>
  <div>
    <div v-if="type === 'fruit'">
      {{ getState() }} is a piece of fruit.
    </div>
    <div v-else>
      {{ getState() }} is a toy.
    </div>
  </div>
</template>

<script>
export default {
  props: {
    type: {
      type: String,
      required: true
    }
  },
  methods: {
    getState() {
      // Your logic to get the state
      return 'Some state';
    }
  }
}
</script>
  1. Use the Component in a Parent Component:

Pass the type variable to the component from the parent component.

// ParentComponent.vue
<template>
  <div>
    <ToyOrFruitInput :type="bag.type" />
  </div>
</template>

<script>
import ToyOrFruitInput from './components/ToyOrFruitInput.vue';

export default {
  components: {
    ToyOrFruitInput
  },
  data() {
    return {
      bag: {
        type: 'fruit'
      }
    };
  }
}
</script>

Both of these solutions allow you to pass a variable to a custom component and use it within the component to determine the type of item in the bag.

Please or to participate in this conversation.