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

LYiub's avatar
Level 1

Sending PHP variables from Controller to model as PHP variables

Hi, I'm trying to pass a PHP variables from controller to modal direct. First I have the blade view (index) which have datatable inside it, (index) blade lunched by function index() method.

Once I click on one user of DataTable, A modal will shown with the PHP variables about this user, and I want to get the data by PHP as well.

all_data.blade.php

<button id="create_new_lead" data-toggle="modal" data-target="#exampleModal" class="btn btn-info btn-lg">New Lead </button>

Controller:

function index ()
{
    $id = 222;  

    $info = Info::find($id);
    $details = Detail::find($id);
    $others = Other::find($id);
    
    return view('all_data')->with('info', $info)->with('details', $details)->with('others', $others);
}


My model:


<div id="exampleModal" class="modal fade exampleModal-xl modal-lead" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            
    @foreach($info as $info_in)
        {{$info->name}}
    @endforeach

    @foreach($details as $details_in)
        {{$details_in->description}}
    @endforeach

    @foreach($others as $others_in)
        {{$others_in->age}}
    @endforeach
    </div>
    </div>
</div>

So, once I click on the button, I want to change the ID value in controller or by any other way, so I can ensure that the modal will show the new values. Is there another way to pass values from controller to modal, but the PHP values in modal should be in PHP, not JS to handle some operations on them later inside the modal. Any suggestion.

Another question, Is there away to open the modal by ajax, and get the values inside the modal from controller and pass them as PHP values, but sure not handle them inside modal as JS. ??

0 likes
4 replies
michapietsch's avatar
Level 10

PHP is rendered on the server. You modal template is also already rendered when it gets to the browser.

You could manipulate the model with JS. You use Bootstrap and likely want to use jQuery. But you need to fetch the data!

So it doesn't help you much to render and load the modal initially. You could as well leave empty placeholders in the modal.

Two options:

You can fetch only the data via Ajax. So it's processed on the server and you get what you need. Then you fill the empty placeholders elements in the modal before opening the modal.

Or you make an Ajax request to get fully rendered HTML from the server and replace the modal content before calling it.

towhid's avatar

@lyiub , i dont understand what you want to output your project ?

could you tell me the scenario please ..

thank you

LYiub's avatar
Level 1

I want on click on the button, to send a request to the controller and get the data for that user, but this data (which is more than 6 arrays of objects) should be used inside the modal as PHP, not JS

michapietsch's avatar

If you want to use PHP, you need to render the template on the server.

I mentioned two ways to do this. What do you think about it?

Please or to participate in this conversation.