Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel eloquent model how to pass in value from function inside controller to render()

How can one pass in the value from a function inside a controller to the render()?

For example:

class NewClass extends Component
{
   // $a is being passed in. Already defined
   public function DoSomething($a){
      return $a;
   }

   public function render()
   {
      return view('FirstPage', [
       'posts' => NewModel::
       // HOW TO PASS IN value of $a
       where('Value1', '=', $a)
  ]);
 }
}

EDIT:
I do not know how to call the method of DoSomething inside of the where(), as the $a is not defined in the render().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You must store the value in a class property or variable to be accessible within the render() method.

class NewClass extends Component {
    // Declare a class property to hold the value of $a
    protected $a;

    public function DoSomething($a){
        // Assign the value of $a to the class property
        $this->a = $a;

        return $a;
    }

    public function render()
    {
        return view('FirstPage', [
            'posts' => NewModel::where('Value1', '=', $this->a)->get()
        ]);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading