Laravel : Best way to call a collection of elements on all my pages (menu)

laravel collection
laravel collection methods
laravel collection to array
laravel-menu
laravel url
laravel create collection
laravel menu from database
laravel add to collection

I have a menu in a header.blade.php file.

I did something like this :

@php $elements = \App\Http\Controllers\ElementController::getElements(); @endphp
@foreach ($elements as $element)
@endforeach 

It works but it's make me feel bad to use @php in my template.

I'm pretty sure this is a better way to load a collection of Elements for all my page. I was thinking about using AppServiceProvider->boot() function ? Would it be the cleanest way ?

Thanks

AppServiceProvider looks better option.

   <?php

    namespace App\Providers;

    use Illuminate\Support\Facades\View;

    class AppServiceProvider extends ServiceProvider
    {

     public function boot()
    {
     $elements = \App\Http\Controllers\ElementController::getElements();

     View::share('elements', $elements);
     }


    public function register()
   {
    //
   }
 }

Or using BaseController

  class BaseController extends Controller
 {
 public function __construct()
 {
  $elements = \App\Http\Controllers\ElementController::getElements()

   View::share('elements', $elements);
 }
 }

Or Create blade file(elements.blade.php for your case), and put your variables

  <? $elements = \App\Http\Controllers\ElementController::getElements(); ?>

and include this file in whereever you want.

 @include('chat')

 {{use_it}}

Collections - Laravel, The all method returns the underlying array represented by the collection: You may also call the first method with no arguments to get the first element in the The method accepts the page number as its first argument and the number of items to Laravel Partners are elite shops providing top-notch Laravel development� Laravel benefit #1: Laravel makes implementing authentication very simple. Almost everything is configured out-of-the-box. Laravel also provides a simple way to organize authorization logic and control access to resources. Integration with Mail Services. Mail service can be used to send notifications to users’ emails after various events.

For JSON

If you are using json to render the view. Add it in your base controller:

public function response($data)
{
    $elements = ...;
    return response(array_merge($data, ['elements' => $elements]));
}

And then whenever you need to return the data, all you have to do is pass the data array to the $this->response($your_datas) method and the response with the elements would be returned.

For Views:

If you are using render view. just use

View::share('key', 'value');

check this reference

lavary/laravel-menu: A quick way to create menus in , This registers the package with Laravel and creates an alias called Menu . However you can create your own rendering method using the right methods and attributes. Alternatively, you can pass the ID as an element of the options array when adding the menu item: The all() method returns a Laravel Collection. All necessary plugins; Live Preview Download. Vuexy. Vuexy is one of the best-selling and trending Laravel admin dashboard in Themeforest. Vuexy has been selling for 4 thousand times having a great positive review. However, this one is not a laravel admin dashboard template only, it is a package of 5 in 1 bundle.

You are right, Using service provider is the best way! They are called view composers in Laravel, you can find it in the docs

Here is a sample

Register a new service provider and do this

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Star means all views, but i would recommend you pass it to the General layout instead
        View::composer('*', function ($view) {
           $data = \App\Http\Controllers\ElementController::getElements();
           $view->with('collectionName',$data);
        });
    }
}

Laravel features you may not know about, Use the artisan down command to put the Laravel app down, but allow access to certain IP(s) to define how many links either side of the current page are displayed; Use Use the fluent dump() method within your chained Collection calls; Use All of the above, plus a few others such as Route::group(. For convenience, Laravel supports these URLs as an alternative to configuring your database with multiple configuration options. If the url (or corresponding DATABASE_URL environment variable) configuration option is present, it will be used to extract the database connection and credential information.

Laravel: How to Make Menu Item Active by URL/Route – Quick , Probably one of the most common operation on every website is to assign “active ” CSS class to the menu item which is currently opened. It is quite easy to use and will cover all your needs. Just set default set of assets somewhere (e.g. AppServiceProvider) and then in your controller add additional js files that you need only for this particular page. This is the easies way to manage your stack of assets. Of course you can do that manually.

Creating multiple pages with navigation menus, In this article we'll talk about web site navigation and menus. There are several types of menus you will be called upon to create in Sitemaps: Large lists of links that point to all the different pages of a The easiest way is to wrap the anchor in an element and then set that element's width using CSS. That's all great until something breaks and you have no idea what the hell is going on because (for example) your method call on a model collection is falling through to the query builder. Both are valid criticisms, but I don't feel like they're nearly enough to condemn Laravel as a whole, especially when both features are 100% optional.

Laravel Collections: PHP Arrays On Steroids ― Scotch.io, Laravel collections are what PHP arrays should be, but better. In this Eloquent ORM calls return data as collections Use the filter method to get a list of all the users that * are below the age of 35. The intersect() method removes the elements in a collection that are not in a passed array or collection. Laravel Controller Validation In short I'm validating my input via Controller Validation in Laravel. After the validation passes I get all the data Laravel-5 - Creating a folder once user registred laravel 5

Comments
  • Have you attempted passing the values of this element in your controller function via view() function?
  • Thanks for the help :)
  • @djoo Welcome :)
  • Thanks for the help :)
  • Thanks for the help :)
  • Just a note! don't call a controller like that! make a separate class! write the logic in it then inject it in Controller and in service provider (It's really bad practice), Good luck