1

I started studying Laravel and ran into a problem using models. How to load them? For example in CodeIgniter i used it like $model = $this->load->model('some_model'). In Laravel when i call it from controller like Sites::OfUser() it work fine, but when i call Sites::getId() it says that method should be static... Is it possible to call method without static or i need to create facades for each model?

My model looks like this:

namespace Models;

use Eloquent;

class Sites extends Eloquent {

    public function scopeOfUser($query)
    {}

    public function getId($name)
    {}
}
Kin
  • 4,466
  • 13
  • 54
  • 106
  • As I commented on the answer autoload will not be called when you call a static function. For fun and giggles try something like this: new Sites(); $sites = Sites::ofUser(); and see if the class gets loaded. – Mathijs Segers Jun 03 '14 at 07:31
  • @MathijsSegers Is it possible that such a cool framework does not have an elementary-loading for models? – Kin Jun 03 '14 at 07:34
  • You want to call a method statically, so it would only make sense to declare it static. I don't see a problem with that. – MightyPork Jun 03 '14 at 07:45

3 Answers3

1

For static method--

$type = Sites ::scopeOfUser($query);

and if you want normal like codeingiter then use--

$model = new Sites ();
$type = $model->scopeOfUser($query);

Reena Shirale
  • 1,992
  • 1
  • 17
  • 15
0

You can of course make a static method in the model, and do some static work in it (get ID for name or whatever).

That's no problem.

However, you must declare it static if you want to use the ::, which you are doing not.

public static /* <-- this */ function getId($name)
{
    // Do work
    // return $result;
}
MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • PHP actually lets you call static functions if it's not defined as static. I know it's not cool and such though. It however does not, let you autoload classes which I assume laravel does. – Mathijs Segers Jun 03 '14 at 07:30
  • But i don't want to use static methods – Kin Jun 03 '14 at 07:31
  • Let me quote you: `but when i call Sites::getId()`. That is a static call. Laravel allows you to call scopes as static, because there it makes sense. It doesn't know what to make of your `getId()` method, for it is not a scope. I'm unsure what your intention is though. – MightyPork Jun 03 '14 at 07:39
0

If you want to access a method with ::, you will need to make it a static method or create a Facade.

The reason why Sites::OfUser() is "working" is because you have prefixed that method with scope.

Scopes allow you to easily re-use query logic in your models. To define a scope, simply prefix a model method with scope.

If you want to use Facades you can follow my answer here on how to create a Facade.

Community
  • 1
  • 1
Marwelln
  • 28,492
  • 21
  • 93
  • 117
  • Yes, i know ho to create facades, but think it;s quite a bad approach that i should create for each model the facade... – Kin Jun 03 '14 at 08:41
  • 2
    You're not forced to using it. You can use `(new Sites)->getId(...)` if you don't want to use `static` but a one-liner. – Marwelln Jun 03 '14 at 08:53