dan3460's avatar

View()->share() vs compact()

@snapey had a comment on a discussion about a blade returning 1. He said that uses the share method to pass data to the view. I have been using the compact() function when i'm passing data to the view (for example a drop down). Are there significant differences between the two methods?

BTW this is the post: https://laracasts.com/discuss/channels/laravel/blade-echoing-returning-1

0 likes
23 replies
Nakov's avatar

The point that he makes there is that you can use view()->share() from within other methods and share data with the view that is being rendered instead of returning the data from one method and pass it into the view helper. So here is an example of both:

public function index()
{
    $this->prepareData();

    $anotherData = $this->otherData();

    return view('index', compact('anotherData'));
}

// using view share
private function prepareData()
{
    $data = Model::all();

    view()->share('data', $data);
}

private function otherData()
{
    return Model::all();
}

So both $data and $anotherData will be available in the index.blade.php. Using share is good, but it is a bit of magical, and you need to be comfortable with Laravel and know what you do to not get confused by it. The same goes with using View Composers. Those are good for hiding the logic and prepare the data, but you might get lost if you don't know what you are doing, especially for new comers.

2 likes
jlrdw's avatar

Please look up compact in the PHP manual. It is not a laravel helper.

dan3460's avatar

Thanks, i understand. I see where you can easily get confused that you are calling for some data on a view that is coming from a buried function. One more question is i may: The OP of the thread had something like this

View::share('data', $data1);
View::share('otherData',$otherData);
.
.
.

return view('home);

That would not be different from:

return view('home',compact('data1','otherData');

Is this correct? as the share() occurred in the same function as the calling of the view.

Thanks again, learned something new today.

chatty's avatar

view()->share() is more effective when you pass a variable to a blade partial that is reused on multiple places.

for example look at this image

notification

you can see the bell in the nav bar right? the icon shows how many notifications you have. since the nav bar is universal, you don't wanna pass the notification in every controller

that's why view()->share() is there to get rid of the repetitive code to pass data

Nakov's avatar

@raravel Yup, for that you can use View Composer maybe, instead of having a function in a base controller that will share the data with the view and having to remember to call that method each time you need the data. Using view composer you will set which views should have access to the data you are passing, for example a partial called notification.

Snapey's avatar

Just to confuse you further, I never use compact, preferring;

return view('home')
        ->withData1($data1)
        ->withOtherData($otherData);

I like the fluent syntax rather than arrays. It means this is also nice;

return view('home')
        ->withData1(Data1::all())
        ->withCategories(Categories::all());

These are trivial examples, but again, it shows the flexibility and the fact that I can easily return data sets without first copying them into temporary variables.

jlrdw's avatar

Well a more simple answer is:

Jeffrey in a video says there are several ways of doing it, if I recall, he uses array, I use compact.

Watch some of Jeffrey's free videos.

Nakov's avatar

@jlrdw I don't think that you understood the question in first place, the guy asks what is the difference, it is obvious that there are couple of options, so that's what we share here. No one said that compact is a laravel helper (hence your first reply does not make sense). And Jeffrey goes back and forward on many of his videos he uses compact and in some he uses array on others he uses with, so your second reply also does not make sense.

The simplest answer is to go with whatever seems comfortable to him, but respect symmetry as well, meaning to use one approach in the project.

jlrdw's avatar

@Nakov okay, but a question.

The answer above mine, why wasn't that jumped on by you as well, why just my answer. It didn't mention share difference. Just curious.

And how does your reply to me help the OP?

And referring OP to Jeffrey's videos is wrong, why?

share is for more than one view, compact is just passing to currently called view,

there

Nakov's avatar

@jlrdw I have nothing against you my friend, I am just saying that we are giving him all options to share data with the view, he didn't asked for it but that's an option too what snapey shared. And I didn't say it is wrong to refer to videos, I just said that Jeffrey goes back and forward using each option, so in this case I don't know how that will help the OP. That's all. Have a nice day.

Edit, if you looked at the link for the other question you wouldn't add your edit :)

Snapey's avatar

@jlrdw

share is for more than one view, compact is just passing to currently called view,

No. You are wrong

Go see the other question.

This is about passing data to the view from the controller and the OP asking why I suggested that view()->share() in the controller was used and what the difference is

1 like
Snapey's avatar

The question is not about view composers.

jlrdw's avatar

There's still an example in the article.

jlrdw's avatar

share is for more than one view, compact is just passing to currently called view,

No. You are wrong

So the following from the documentation

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method.

You are saying I am reading that sentence completely wrong.

Share is for more than one but compact it's just for current View you are saying that's completely wrong is that not correct.

If I'm reading that directly from the documentation wrong then the docs needs a pull request.

Snapey's avatar

No I am saying that this question is not about View Composers. You introduced them. They are not relevant to the question.

You are saying I am reading that sentence completely wrong.

No course not, I am saying its got f'all to do with the question.

READ THE QUESTION

1 like
jlrdw's avatar

Ok the question:

  • Are there significant differences between the two methods?

Answer yes

Share is if you want to share some data various (times, places, etc)

Compact is one time use for current view being called.

I read the question and answered the question.

So the difference is intended usage.

I kind of figured the intent of use was a given, I was wrong.

Snapey's avatar

OK you're pissing me off now.

The OP asked me what I meant in a previous post. Don't you dare to tell me what I meant in my own answer

You are still not reading or understanding the question.

Try it. In a controller write

view()->share('hello', 'bloody hell it works');

Then try it in the view

thats {{ $hello }} by the way

Then you tell me what share means

jlrdw's avatar

Don't you dare to tell me what I meant in my own answer

That was not my intent. I was reading the question from this post:

Are there significant differences between the two methods?

is all. And I was just attempting to go by what Taylor said in the docs.

But I will try your example.

dan3460's avatar

I didn't mean to open a can of worms. It seems to me that in almost everything in life, there are mode than one way to skin a cat. My task is to learn more about view()->share().

Please or to participate in this conversation.