Repeat Controller Variable? Private Property or View Composers

Repeat Controller Variable? Private Property or View Composers

Laravel Daily

3 года назад

14,178 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Ila
Ila - 25.05.2023 09:18

What is the advantage of using this? Is it better than a helper function?

Ответить
Koohyar Abedini Nassab
Koohyar Abedini Nassab - 22.02.2023 23:20

The last solution was exactly what I needed for my sidebar. Thanks. :)

Ответить
Jawad Noori
Jawad Noori - 14.08.2022 17:14

Thanks alot sir It worked very well for me

Ответить
Nur huda
Nur huda - 30.04.2022 09:13

thanks nice tutorial

Ответить
darknmy
darknmy - 27.04.2022 14:05

public $categories;

function getCategories() {
if (! $this->categories)
$this->categories = Category::all();

return $this->categories;
}

Ответить
Music Reel
Music Reel - 16.07.2021 04:36

Thanks so much sir God bless

Ответить
taha marmada
taha marmada - 25.06.2021 11:08

thanks a lot it works

Ответить
Jazim Abbas
Jazim Abbas - 27.04.2021 07:36

What admin ui you are using in this video??

Ответить
Elkin Ossa
Elkin Ossa - 02.12.2020 18:56

Using ViewComposers is very useful, I use it, but since I saw the event video I can't stop worrying about traceability and the case when a beginner sees the code, so it can be magical for him if he has no knowledge of ViewComposers.

Ответить
Jose Chirino
Jose Chirino - 19.11.2020 16:44

Thanks, that was very helpful

Ответить
Svetoslav Mutev
Svetoslav Mutev - 19.11.2020 08:18

ViewComposers Share is little tricky. It should be used only when some data is used on view that is used everywhere. Usually I use it for something that is looped at the app layout (languages / menu items / ...). For 2-3-4 pages I think that it is pointless to use ViewComposers.
Out of that > instead of using private properties as alternative, better make private method that returns what you want. There is no point to overload your constructor with eloquent selection..

Ответить
Normunds Skudra
Normunds Skudra - 18.11.2020 19:14

Service Injection seems much more traceable way to do this. I think @inject(...) will draw anyone's attention way faster when they need to trace that variable

Ответить
Ross Elliot
Ross Elliot - 18.11.2020 04:03

Hey, Povilas. I'd like to see a tutorial about how to override core Laravel classes, or how to override specific methods of core classes. An example would be the debug class which uses Symfony's VarDumper class if I wanted to change the styling of the dd command output. But it could be any core class. A couple of examples about how to implement it so the new functionality is loaded and available by default.

Ответить
Rodrigo
Rodrigo - 18.11.2020 00:48

Thanks! I wanted to find a way to make my controller functions more clean!

Ответить
Muhammed Sarı
Muhammed Sarı - 18.11.2020 00:21

View Composers add global state to the application which makes it a bad solution for projects that are being developed by multiple people. The best solution (imo) for maximized maintainability is using View Models / View Presenters.

Ответить
Ali Al Qahtani
Ali Al Qahtani - 17.11.2020 22:39

Hello, I would ask you about Helper Function, Is it doing like compsers?
because it is doing same thing ( pass variable on all project )
Thank you.

Ответить
Faizan Anwer Ali Rupani
Faizan Anwer Ali Rupani - 17.11.2020 21:54

I've developing in Laravel for more than a year and I finally found something I didn't know anything about. Thanks

Ответить
Roberto Garcia
Roberto Garcia - 17.11.2020 18:49

Load the categories in the constructor is not a good idea because if you have a lot of categories this will increase the load times then the view composers in laravel is a good way but be careful don't abuse remember every data loaded in the views in a normsl scenario comes from database and this suppose infrastructure usement and in the future translated like technical due aka code smell

Ответить
403gtfo
403gtfo - 17.11.2020 15:07

I am sure this makes 100 logical sense... but to me it feels like more abstraction for abstraction sake.

Ответить
Rick Lambrechts
Rick Lambrechts - 17.11.2020 15:02

This is a cool feature. Thank you for showing!

Ответить
M-ART-IN Szotkovski
M-ART-IN Szotkovski - 17.11.2020 13:02

I am so happy i found your Channel. So good and helpfull!

Ответить
Fuad Bakhsh
Fuad Bakhsh - 17.11.2020 12:41

Sir I need help to import date from excel and allocate to 3 different users to perform task. It is data entry project.

Ответить
Bumble Bity
Bumble Bity - 17.11.2020 12:38

How to acess App\Component\Wrapper class? Without (new SomeWrapperClass())->someMethod() it's impossible to use a class.

Ответить
poplach
poplach - 17.11.2020 12:15

I'd say binding variables in ViewComposers will give a lot of questions later on, checking view files and not understanding from where did categories come from.
I'd rather do it Adam Wathan's way, making a small private function in Categories model that returns 3 categories, so in the controller I call it something like Categories::getThreeCagegories() which makes code more clean and readable.

Ответить
Charles-Louis Girardot
Charles-Louis Girardot - 17.11.2020 11:39

Good reminder, thanks a lot Sir! Take care 👍🏼

Ответить
Fulufhelo Mukwevho
Fulufhelo Mukwevho - 17.11.2020 10:43

Great video

Ответить
Muhammad Imran
Muhammad Imran - 17.11.2020 10:19

Hi Povilas, I also have faced this kind of scenario when I need to pass languages to all the views I simply wrote view composer in web.php and I achieved the required functionality. I am also going to share code with you and i think this is most simple way to achieve this.

View::composer(['*'], function($view){
$languages = [];
$segments = request()->segments(1);
$queryParams = explode('?', request()->fullUrl());

foreach(cache('LANGUAGES') as $lang) {
$segments[0] = $lang['short_code'];
$languages[$lang['short_code']] = [
'title' => $lang['title'],
'url' => url(implode('/', $segments). ((count($queryParams) > 1) ? '?'.$queryParams[1]:''))
];
}
$view->with('languages', $languages);
});

Ответить
alfie villarin
alfie villarin - 17.11.2020 09:39

Sir I need your help for my project can you make a forum from laravel jetstream sir please

Ответить
alfie villarin
alfie villarin - 17.11.2020 09:35

Hi sir how are you? Sir can you please make a forum on laravel 8 jetstream

Ответить
ahmed kamel
ahmed kamel - 17.11.2020 09:25

I'm so happy that you got well I hope you completely better now

Ответить
Drey
Drey - 17.11.2020 09:13

Please do a video on laravel web-socket using jQuery and bootstrap 4, also a video chat using laravel web-socket and jQuery

Ответить
Yash Patel
Yash Patel - 17.11.2020 09:11

Yeah, Thank but I know

Ответить