How do I access a global model instance in laravel 4?

user1094128

In Laravel 4, how do I create an instance of a model and make it globally available? Even in views. I'm looking to do something similar to the way you get the User instance using Auth::User->name (the syntax I mean, not storing in a session) but in this case it would be ModelName::DefaultEntity->attribute.

A little more detail...

I am writing an application that will house multiple websites - a bit like a CMS. So I have a Website model. Each Website model will have a URL attribute so that when a user visits the URL the application can retrieve the Website model from the database and brand the website appropriately e.g. Title, logo, theme, etc...

I would like the current Website model to be available everywhere without having to create a new instance of Website in every controller/method. So in my layouts and views I could just say something like:

{{ Website::Website()->name }}

or

{{ CurrentWebsite::name }}

I have achieved the first one by making a static method in the Website model:

public static function current()
{
    return Website::find(1); // just to test it for now
} 

But with that, it will have to do a database query every time I say:

{{ Website::current()->name }}

Plus it doesn't feel right.

Can anyone help?

Kind regards,

Robin

Antonio Carlos Ribeiro

What you already have is good, but if you just want prevent that query from executing every time, you can cache it:

public static function current()
{
    return Website::remember(10)->find(1); // just to test it for now
}

Adding a listener to your routes.php:

 DB::listen(function($sql, $bindings, $time) { var_dump($sql); var_dump($bindings); });

And executing it:

{{ Website::current()->name }}

Will show the query in the first execution but not in the second, because it's cached.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I access the instance of an enclosing class?

From Dev

How do I access an instance of a class in Swift

From Dev

How can I access a model from a controller using laravel4?

From Dev

How do I call a model in Laravel 5?

From Dev

How do I access data from a model the present model belongs to?

From Dev

How do I access data from a model the present model belongs to?

From Dev

JS: How can I prevent access to the global variables do?

From Dev

How do I check if an argument is an instance of model or the class in Rails?

From Dev

How do I subscribe to a model instance in Sails.JS?

From Dev

How do I generate standardized model instance template code in django?

From Dev

How do I Bind an Instance of a Model to a Parameter in a Controller Action

From Dev

How do I set an instance variable on an Eloquent model?

From Dev

How do I Bind an Instance of a Model to a Parameter in a Controller Action

From Dev

How do I create an instance of a model in views.py?

From Dev

How do I make global helper functions in laravel 5?

From Dev

How do I access an unknown instance's slot using a string?

From Dev

How do I programatically access the current webdriver instance?

From Dev

How do I access the instance of an object that was loaded via storyboard?

From Dev

How do I access a static property in typescript from an instance of a subclass?

From Dev

How Do I Access a Property of bound Object Instance with Knockout

From Dev

How do I access the instance of an object that was loaded via storyboard?

From Dev

How do I access a static property in typescript from an instance of a subclass?

From Dev

How do I access a module from an instance method in Ruby?

From Dev

How do I create a new FCPDF instance using Laravel with UTF

From Dev

How do I access a query string parameter in loopback model?

From Dev

How do I access and update ng-model in controller?

From Dev

How do I add an attribute to an instance variable in rails 4?

From Dev

Laravel - Need to access a global variable set in BaseController from within a model

From Dev

Laravel: How do I create a custom pivot model?

Related Related

  1. 1

    How do I access the instance of an enclosing class?

  2. 2

    How do I access an instance of a class in Swift

  3. 3

    How can I access a model from a controller using laravel4?

  4. 4

    How do I call a model in Laravel 5?

  5. 5

    How do I access data from a model the present model belongs to?

  6. 6

    How do I access data from a model the present model belongs to?

  7. 7

    JS: How can I prevent access to the global variables do?

  8. 8

    How do I check if an argument is an instance of model or the class in Rails?

  9. 9

    How do I subscribe to a model instance in Sails.JS?

  10. 10

    How do I generate standardized model instance template code in django?

  11. 11

    How do I Bind an Instance of a Model to a Parameter in a Controller Action

  12. 12

    How do I set an instance variable on an Eloquent model?

  13. 13

    How do I Bind an Instance of a Model to a Parameter in a Controller Action

  14. 14

    How do I create an instance of a model in views.py?

  15. 15

    How do I make global helper functions in laravel 5?

  16. 16

    How do I access an unknown instance's slot using a string?

  17. 17

    How do I programatically access the current webdriver instance?

  18. 18

    How do I access the instance of an object that was loaded via storyboard?

  19. 19

    How do I access a static property in typescript from an instance of a subclass?

  20. 20

    How Do I Access a Property of bound Object Instance with Knockout

  21. 21

    How do I access the instance of an object that was loaded via storyboard?

  22. 22

    How do I access a static property in typescript from an instance of a subclass?

  23. 23

    How do I access a module from an instance method in Ruby?

  24. 24

    How do I create a new FCPDF instance using Laravel with UTF

  25. 25

    How do I access a query string parameter in loopback model?

  26. 26

    How do I access and update ng-model in controller?

  27. 27

    How do I add an attribute to an instance variable in rails 4?

  28. 28

    Laravel - Need to access a global variable set in BaseController from within a model

  29. 29

    Laravel: How do I create a custom pivot model?

HotTag

Archive