HUGE_DICK_10_INCHES's avatar

How does laravel set compact data as usable variables

I am making some mvc and I am wondering how does laravel does it: I looked into files connected with view but I couldn't find out what was it

view('index', compact('user'));

and inside index view

<?=$user?>

When I pass data through my view method I get array where I need to use $data

Any help?

0 likes
16 replies
tykus's avatar

compact() and extract() are native PHP functions which Laravel is using to pass data into your views.

HUGE_DICK_10_INCHES's avatar

@tykus I know they are but I am wondering how does it passing that data through view make same naming accessible in view?

tykus's avatar

extractuses the array keys for the variable names

Cronix's avatar
Cronix
Best Answer
Level 67

compact() creates an array, which is passed to the view, where extract() is used to turn them back into their original php variables.

2 likes
jlrdw's avatar

@SkyCoder it wouldn't hurt reading the php manual on this subject also.

All laravel's magic is php in the background. Taylor worked hard to make shortcuts.

jlrdw's avatar

The basic php course Jeffrey has would be a good thing for you to take.

I was suggesting the php manual to see more on compact.

I don't anyone was s.a.

Also php is written in C language. Just a fyi.

tykus's avatar

you was not understandable

And that is smartass how? Not exactly the kindest way to treat people who are answering your questions...

HUGE_DICK_10_INCHES's avatar

I never said it is towards you I said his was not smartass and was understandable so if you was not understandable whos was smartass?

tykus's avatar

Sorry @skycoder I had my jlrdw-blocker extension enabled; I didn't see his replies :D

Should've made the smartass connection immediately!

jlrdw's avatar

@SkyCoder well I see you hardly completed any lessons, and you ask questions here which is okay.

But I never dreamed referring someone to the parent languages manual would be taken wrong.

I guess apparently you are one of those who does not want to learn the hard way you must just want quick copy and paste code.

Where you should study PHP for several months before even touching laravel.

Good luck in your endeavors.

@tykus I have left you alone. So now you say referring someone to php manual is s.a.

Just remember I did not say anything wrong to you, this time you seem to want to start on me. All I was doing is trying to help OP by suggesting the php manual because there are good footnotes on the subjects.

Okay, OP is not interested in the php manual, But really by me just referring OP to it I am s.a.

All I said:

it wouldn't hurt reading the php manual on this subject also. All laravel's magic is php in the background. Taylor worked hard to make shortcuts.

Well I apologize for referring OP to the php manual for more on a subject, I did not know OP was not interested in further reading on compact.

I will try my best to remember OP's name and make sure I do not bother OP again with any answer.

jlrdw's avatar

I usually do not use extract, rather:

    public static function make($path, $pageLinks = null, $layout, $title = null, $data = array())
    {
        if (!empty($data)) {
            foreach ($data as $variable => $value) {
                ${$variable} = $value;
            }
            unset($variable, $value);
        }
        // other code
       
    }
HUGE_DICK_10_INCHES's avatar

@jlrdw no offense but I see you as one kid from school that everyone talk shit to and now you are smart boy that is so angry on past and want to play revenge game on others....

I had a reason to not understand something and if I knew every detail from php.net I wouldn't be here.

It was not hard to write something like cronix did, he wrote it in the way so anyone even stupid people could understand and he helped me.

tykus did same but I was too stupid to understand and you did nothing, but to say something like it doesnt hurt to read.

Come on dude you look so old on this photo but you act like a brat....

Thanks to @Cronix and sorry to @tykus for not understanding :D

jlrdw's avatar

@SkyCoder I gave my answer after @Cronix. My intent was just further reading on the subject, nothing more, nothing less.

on this subject also.

Also meaning in conjunction with previous answer, not instead of that answer.

You read things into the message that wasn't there. I never said ignore the other answer and just read the PHP manual, I merely meant for further reading.

Also definition - in addition, additionally

Again Good luck in your endeavors, whatever they may be.

Please or to participate in this conversation.