RFFRED
3 months ago
116
1
Laravel

Form: Accessing a model from another model

Posted 3 months ago by RFFRED

Hello,

I have a form calling several external modules.

In my page controller

public function edit(Request $request, Page $page)
{

        //Loads the page template which in turn loads the modules
        $page_data = $page->template()->first()->load_template_modules_data(Array('page' => $page));
  
        return view('rf-pages::admin.pages.edit', compact('page', 'page_data','request'))->withInput($request->input());

}

In my template model

    public function load_template_modules_data(array $data)
    {

        //Fetches the modules allocated to the template
        $template_modules = $this->get_modules();

        $modules_data = [];

        //for each module, compiles class names, extra info
        foreach($template_modules as $module)
        {
            
            $module_name = $module->module_name;

            $module_config_data = \Config::get($module_name);
            $module_onload_class = $module_config_data['onload'];

            //model loaded when the module starts
            $module = new $module_onload_class;
            
            // Initialise the module, load data from the database related to the page
            $modules_data[$module_name] = $module->init(array('page' => $data['page']));

        }

    //return all the modules used with information about display, functionality, visibility
        return array('template_modules' => $template_modules, 
                     'modules_data' => $modules_data);

    }

Is there a better way for the "template" model to know which "page" is doing the call without passing it as a function parameter?

Please sign in or create an account to participate in this conversation.