Support the ongoing development of Laravel.io →
Database Eloquent Views
Last updated 2 years ago.
0

You can use

....
$data = Article::checkArticle()
....

Or

public function (Article $article){
$data = $article->checkArticle()
}

Don't need

$articles = new Article;
Last updated 9 years ago.
0

Yes, but in order to use $data = Article::checkArticle() in my controller

I need to make my method in my model a public static function (otherwise it will error out), something which in the Laracast said wasn't the best practice.

He said it was best to instantiate the object by

$articles = new Article

I'm learning so I may be misunderstanding

Last updated 9 years ago.
0

An object is usually one singular thing. You're confusing the issue by calling it

$articles = new Article();

How could $articles (plural) be pointing to a new instance of the model object Article?

laravel has all this built-in stuff to handle collections of objects, for example query scope http://laravel.com/docs/4.2/eloquent#query-scopes

So saying something like this makes more sense

$acticles = Article::latest();
foreach ($articles as $article) {
$article->check();
}
0

Ok, thanks.

I just wanted to know the different between these when accessing methods inside classes in Laravel

Article->latest()

and

Article::latest()

And does it matter?

I understand laravel has built in stuff (maybe i'm confusing the Laravel stuff with the OOP practice

Its just the laracasts I was watching (OOP) was to avoid using static functions and I just wanted clarity around it

Last updated 9 years ago.
0

If I remember correctly (from my Java class ... lol), static methods need not object to be instantiated in order to use, and therefore it is not recommended in certain situation such as the refers to "this" as well as other instance variables / methods.

Eloquent, Illuminate\Database\Eloquent\Model's find() function as well as the DB class which refers to Illuminate\Support\Facades\Db are actually a syntactical sugar of Illuminate\Database\Eloquent\Builder and Illuminate\Database\Connection respectably. So if you look at Builder class for example, the find function is actually not static method, but an instance method instead.

Laravel calls it Facade, somewhat similar to proxies which refers to other so to allow the framework to make cleaner syntax while maintaining testability.

At the very basic skeleton, it might be just doing something like this, (correct me if I am wrong. :D)

class Model {
   public function find($id) {
      // Finding record of $id
   }
}

class Article {
   public static function find($id) {
      $m = new Model; 
      return $m->find($id);
   }
}

Some of the OOP concepts that might help you. Borrowed from Java docs.

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Writing you code in class and object forms instead of functional programming in linear, using Class Inheritance such as extending the parent class to obtain more controls while not repeating yourself, using Interfaces to write contracts, and allow dependency injections. Encapsulate your code to user, such make use of private function, final keyword, setter/getter function to limit the action of users can have.

I am sure you don't want people to change your balance without any measures right? :D

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ottz0 ottz0 Joined 15 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.