Pass data from partial include to Composer


reviewing the documentation, it seemed that, to pass variable from partial to composer, was enough to

@include('partial.file', ['data1' => 'value', 'data2' => 'value2'])

and in

 public function with()
        return [
            'myVar' => $this->my_function($data),

public function my_function($data) {

//access to $data 


this seems not to work anymore, as it generates an error

Declaration should be compatible with Composer->with()

so… what now?
how can I access those values?


I think you have two things going on here.

One is that with() is protected, not public, hence the complaint about “Declaration should be compatible”.

The second is that to access the current data, you need to use $this->data. If your with() example you’re pulling the $data variable out of thin air. It would need to be something more like:

public function myFunction() {
  return $this->data['data1'];

ok, it’s not working :smiley:

real case scenario:


public function with()
        return [
            'pinnedPosts' => $this->getPinnedPosts(),
            'id' => $this->getID(),

    public function getID()
        return $this->data['pageID'];

@include('partials.pinned', $data = ['pageID'=> get_the_ID()])

ID: {!! $id !!}

but the result is…



What debugging steps have you already tried? Have you checked the value of the data property in the composer?

sorry, you are right.

 return $this->data;

returns this:
Illuminate\Support\Fluent Object ( [attributes:protected] => Array ( ) ) 1