is Collection a Laravel instance or PHP instance? - php

is collection a laravel instance or PHP instance.
I always hear people say a Eloquent return a laravel collection so I want clearance on this

The Collection class in Laravel is a part of the Laravel framework. Therefore, if you attempt to use the Collection class in a pure PHP project (without the use of Laravel) you will not be able to use the Collection class by default.
You can make use of the Collection class without using all of Laravel. You'll see it's part of the Illuminate/Support components on github. https://github.com/illuminate
There are also composer packages that will install the Collection class for you all on it's own. https://github.com/tightenco/collect. So, I don't think it's correct to say it's a "Laravel Instance", but unless you install it on your project, you won't have access to the class without Laravel.
Laravel makes use of a lot of components that Laravel did not write on it's own. Symphony, is a major example of this.

Related

Where to Place My Package in Lumen Framework

If I want to use some custom class in Lumen, where should I place them? The Laravel official document does not mention this in any of application structure, service container or package development. Actually I found the document quite confusing to some extents.
For example, I want to set up a service called Invitation, I know I need to register this class in InvitationServiceProvider but where should I place the Invitation.php which the actual class exists in? This package is used for some specific application thus I do not want to put it in composer packagist.
BTW, the version of Lumen Framework is 5.2.
So finally I created a folder named Service under app and just pull all the libraries inside...

How to add method to vendor class?

Could you tell me guys how to add a new method to vendor class for example for:
vendor/illuminate/html/FormBuilder.php
Whats the proper way to extend my laravel classes? I guess craeting new class somewhere in app folder?
The best way to extend the actual Laravel packages is to create your own package that require and extends (in your case) the Illuminate FormBuilder object. First check out the official docs here.
Be aware that the Form Builder is deprecated and no longer supported in the last Laravel releases. You can use the LaravelCollective Package instead.
So, let's recap... To achieve your goal you should:
Create your own package that requires the LaravelCollective component
Extend the LaravelCollective FormBuilder adding your own methods
Register your package into Laravel Container binding your own Service Container

Using own database wrapper in Symfony

Recently I started learning some Symfony as a side "project". I know with Doctrine you can use MySQL. However, I got my own database class using PDO. How can I implement and use my own database class on Symfony? And in which folder should I place it?
I used the download from here: http://symfony.com/download and the tutorial from http://symfony.com/doc/current/book/installation.html. I couldn't find anything about using an own database class.
You can use it like any other class in php, or you can create symfony service for that class (you have everything about services in symfony2 documentation)
According symfony2 best practices you can put your class in any folder in AppBundle. But if you want to reuse your class on multiple projects you should put it on packagist, and instal it with composer.
But if just you want to learn symfony you should consider using doctrine, because most of the symfony projects are using it.

Install script using Laravel models, possible? [duplicate]

Is it possible to use Eloquent without Laravel or does somebody knows a equally easy to use ORM?
Yes you can. A while ago Dan Horrigan released a package called Capsule for Laravel 4 which allowed Eloquent to be used independently and with minimal setup. The package itself has been merged with the L4 core so you no longer need to use the package.
If you refer to the illuminate/database repository there is a nice little introduction on using Eloquent without the framework.
Here is a small excerpt of getting it up and running.
$capsule = new Illuminate\Database\Capsule($config);
$capsule->bootEloquent();
$capsule->connection()->table('users')->where('id', 1)->first();
Update
Dan Horrigan has since removed his Capsule implementation as it is now built directly into Eloquent. Refer to the above illuminate/database link for more details on how to use Capsule.
Check out https://github.com/Luracast/Laravel-Database it provides full eloquent support including artisan migrations and more for the latest Laravel 8.* components.
It uses capsule and lazy loads the components when they are used.
Disclosure: I'm the author of this repository
In Laravel 4.*, Eloquent is automatically independent because it's shipped with Dan Horrigan's Capsule. You don't need to download any extras. For a how to please visit: https://github.com/illuminate/database/blob/master/README.md

How do I use Symfony's Doctrine model classes outside of Symfony?

Is there a way to use Doctrine using the model classes I've already setup for my Symfony applications without having to call Symfony with all that overhead?
This is more to satisfy a curiosity than anything else. For all the scripts I've used, I've just been able to instantiate Symfony (which typically turns out nice since I have all of the features that I'm used to working with on this particular project. But there has to be a way to load Doctrine and use the Symfony model classes without Symfony... Right?
Doctrine isn't dependet on symfony. Doctrine is a "framework" on its own. It has it's own autoloading and can therefore work with it's classes like a regular PHP app. You can integrate Doctrine with other frameworks if you want (like CodeIgniter or Zend). So you have every freedom you need without the need to do some tedious migration of your models/data/... from one system to another.
I've come to the conclusion there really isn't a way to use the model classes from Symfony elsewhere. With a little work, you can port over the classes to a new Doctrine model (even if you use the generator, since the main model class just extends the base which extends sfDoctrineRecord (from the API docs, you can see which functions will need to be removed).
Otherwise, there isn't a practical way of doing that.
Anytime I need to access the Symfony model, I'm making a task or plugin since I do typically need part of Symfony's functionality.
As far as Symfony2 goes, just looking at the documentation makes me want to run screaming. It's not mature in any form or fashion (but, then again, neither is Symfony "legacy"). So, I'm not sure if the process would be any easier there.

Categories