Laravel4: How can I use Eloquent/Model - php

I've read and followed documentation found here http://laravel.com/docs/eloquent
and I tried some of the examples i found here
here is the code that I have so far
model
dbeloquent.php
<?php
class dbeloquent extends Eloquent {
protected $table = "users";
public function showTbl()
{
dd(dbeloquent::$table);
}
}
//end of model
?>
route.php
<?php
Route::get('/', function () {
$model = new dbeloquent();
dd($model->someFunction());
});
?>
I want to show my tables first but here is what I'm having
Access to undeclared static property: dbeloquent::$table
somebody help me please

Your dbeloquent class is extending Model class in the background. Eloquent alias is pointing to Model class, in the app/config/app.php file
'Eloquent' => 'Illuminate\Database\Eloquent\Model'
protected $table property is extended from the abstract Model class, and it is not static, so you can't redeclare it (static or nostatic) .The way you can access property from base model is by using:
__get($key) method
But the problem is in which point of execution your $table property is visible, since it is protected and modified at run time.
At the end, it is not declared and defined to be used in such a way - Laravel is internally looking for that property. Try to trace calls, and you will probably find what is happening inside.
Don't complicate things more then they should be complicated.

Related

How does this Facade works

I'm working with Laravel 5.8 and it's an Online Store project written by other programmers.
Basically I have faced something weird that never seen before.
Let's say we have this at a Controller method:
$payment = CourseRegistrationFacade::insertCourseRegisterInformation($courseId,$date,$memberId,$userId);
And then we goto the CourseRegistrationFacade and it goes like this:
class CourseRegistrationFacade extends BaseFacade
{
}
So the whole class is empty but it extends another Facade which is BaseFacade and it goes like this:
class BaseFacade extends Facade
{
protected static function getFacadeAccessor()
{
return static::class;
}
protected static function shouldProxyTo($class)
{
app()->bind(self::getFacadeAccessor(), $class)
}
}
And that's it !
I don't really know where the heal is insertCourseRegisterInformation !!
So if you know how this Facade works, please let me know...
Here is the full code of BaseFacade.php:
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class BaseFacade extends Facade
{
protected static function getFacadeAccessor()
{
return static::class;
}
public static function shouldProxyTo($class)
{
app()->bind(self::getFacadeAccessor(), $class);
}
}
Search in the code for:
CourseRegistrationFacade::shouldProxyTo(
Most likely in the service provider that line is somewhere registering that facade to some concrete implementation of a class. Then check the contents of the class (the argument passed to shouldProxyTo).
Inside that class there should be a method called insertCourseRegisterInformation.
The way facades work is they resolve the class out of the container and then call the method you call statically.
So for example, let's say you have a UserService.php with a method register() and that class is mapped to a UserServiceFacade.php. When you do UserServiceFacade::register(), __callStatic will get the facade accessor (actual class) from the container, then call the register() method of that class.
You can understand better by inspecting __callStatic inside Facade.php.
Essentially UserServiceFacade::register() is the same as doing:
$userService = app()->make(UserService::class);
$userService->register()
By using the facade you can hide the concrete implementation and could possibly switch it to something else in the future by just changing it in a single place.
I think there must be a Provider exists for that Facade which is initializing its associated class and insertCourseRegisterInformation method definition must be declared in it. Please find that provider and then you'll find its associated class from that Provider code. I think you can find all registered providers from config/app.php
These reference articles might help you.
Reference 1:
https://grafxflow.co.uk/blog/mvc/adding-global-custom-class-facades-laravel-5
Reference 2: http://www.expertphp.in/article/how-to-create-custom-facade-in-laravel-52

Laravel Eloquent - accessing parent model from child model returns child model

Extending Eloquent models seems to be a thing people do. I have an interesting issue:
FooBase.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class FooBase extends Model {
protected $table = 'foo_bar';
}
FooExtends.php
namespace App\Models;
class FooExtends extends FooBase {
public function method() {
return FooBase::first(); // or even parent::first()
}
}
Calling (new FooExtends())->method() returns an instance of FooExtends instead of FooBase. (Just static methods affected, which may answer my own question, but one would think Laravel would handle this. Calling (new FooBase())->first() from within the child class works.) What's going on here?
PHP 7.3, Laravel 5.7
This is a really interesting PHP quirk that doesn't apply static context when calling an ancestor class.
Basically, the "static" call to FooBase::first() gets interpreted the same as parent::first(), because PHP knows that FooBase is the parent of the current class context FooExtends. And since calls to parent stay within the context of the current object, the first() call ends up being routed to __call() and not __callStatic() (which would create a new context using the FooBase class).
Really interesting thing to learn about PHP internals and class contexts. Thanks for giving me a reason to poke around. :)

How to share scopes between classes in Laravel 5

I have several Laravel models which have the same functionallity.
I'm trying to implement some sort of ::All() functionallity but with another logic behind it.
For example: all my models have an "Active" boolean flag, which means that I get all of my languages like: $language = Language::where('active', 1)->orderBy('name')->get();. The same goes for hobbies, semesters, etc.
I'm trying to do something like this in my base_model from which all other models extend:
public static function getActive()
{
return this::where('active', 1)->orderBy('name')->get();
}
this would save me lots and lots of redundant code, but as a newbie I'm struggling with the code.
How can I dynamically define the Model I want to retrieve?
Any ideas?
You can use Laravel query scopes for this. For example:
//your base model
class BaseModel extends Model
{
//every class inheriting from this will have this scope
public function scopeActive($query)
{
return $query->where('active', 1)->orderBy('name')->get();
}
}
//your child models will inherit the scope from the parent class
class Language extends BaseModel
{
//your model's methods
}
//use the scope to get all the active languages
$languages = Language::active();

Is there any method/way to find out all the functions of some class in laravel?

I am understanding Laravel(5.1 version although 5.2 now recently comes) framework...and studying it deeply...What I am trying to ask let me elaborate through some example: I have created the model named Blog:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Blog extends Model {
protected $fillable = ['title','body'];
}
Now in my controller I am accessing the function create() of this class in my controller store() method like: public function store(BlogRequest $request)
{
$input = Request::all();
Blog::create($input);
return redirect('blogs');
} As you can see that in above controller method we are accessing the static function/method create() i.e Blog::create($input) ..so the question is as there are so many other methods exists like create() method of a model(Blog) class which extends the Model class...is there any way/strategy/function to find out/know all the functions of this Model Class...
Yes! You can refer to the API documentation.
For example, I searched for model and found Illuminate\Database\Eloquent\Model, which is the class your models extend and there it is the create static method.
You can change the Laravel version on the top left and filter for classes, namespaces, interfaces and traits on the top right. Pretty neat!
Edit:
You can use the getMethods method on the ReflectionClass to list all available methods for a given class.
For example:
$methods = (new ReflectionClass('\App\Blog'))->getMethods();
dd($methods);

Extended class does not see property of parent Codeigniter

I call a CodeIgniter controller method -imgupload- from jquery ajax. This controller extends my custom front controller.
class newad extends My_Controller{
public function __construct() {
parent::__construct();
}
public function imageupload() {
$this->load->library("uploadhandler");
}
The imgupload method calls the uploadhandler class which extends from newad.
class uploadhandler extends newad {
The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected.
The inheritance chain looks like this: My_Controller->newad->uploadhandler.
Any idea why I cant access those properties?
In short the answer is you do not need to extend Controller class here. You can just pass the value to your library as a parameter.
$params = array('user_ud' => $this->userID, 'otehr' => 'other');
$this->load->library('uploadhandler', $params);
//Now from your library
class uploadhandler{
public function __construct($params)
{
// Do something with $params
}
//.. Your code...//
}
Now about your question:
The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected. The inheritance chain looks like this: My_Controller->newad->uploadhandler. Any idea why I cant access those properties?
As inheritance chain are ok, you can access property of My_Controller from your library but not the value of the Current controller, because these two are different object.
So here is my answer how can we access the value? One way I have already mentioned. That will be enough if you need to share some property with the library. But what if you need to access all the Controller instance. There is a function to get the reference of controller instance get_instance(). You can use this function anywhere and get access of all public property controller. If you need to access any private property of controller the define a geter function to access that.
Explanation
First of all you need to learn basic about OOP. Learn about Class, Object, Inheritance..
When I said property of My_controller is different from the same property the you accessed from uploadhandler, it may confused you if you are not familiar with class and object. Here is two instance(object) of different class.
For short let say you have some classes like: Vehicle, Car, Machine and Person. All they have common attributes say name, weight ..
So, can we just inherit Any of these class from other??
Simple answer is no. We can't(!) define a Person class extending from Others. So how can we decide which incoherence would legal. If you can say Foo is a Bar you can write Foo class extending from Bar. Now from your case, It is obvious uploadhandler is not a controller. So Never Extend a anything from something that is not something.
NB: The answer is generic. If you need any specific clarification, just ask, I can update my answer

Categories