Laravel 4 model without eloquent - php

I'm just starting to learn about Laravel and trying to migrate from CI. I have a few simple questions.
Do I have to use eloquent when using a model?
In CI using model you just extends Model. Is there the same to use in Laravel? So far all the docs I found only state that I have to use eloquent if I want to use model. Eloquent is a bit confusing at the moment for me. So I just want to use simple query builder in my model.
I guess number 1 and 2 questions are somehow related.

You can use fluent:
DB::table("...");
Eloquent, which is based on PDO is ORM and more abstracted from raw queries.

Eloquent is a great tool, so I recommend familiarizing yourself with it if you get the chance.
You can build queries on your own using Laravel's query builder.
Read up on it here, I hope this answers your question and sorry if I misunderstood!
http://laravel.com/docs/queries
Good luck!

I think you can`t create a model without extends eloquent like laravel 4 Doc
class User extends Eloquent {}
because a model in laravel based into eloquent ORM unlike CodeIgniter
If you need to deal with database tables with creating models ,you can use DB class
like laravel 4 Doc
DB::select('') and DB::table('')

Related

CodeIgniter 4 Model and the Query Builder class for complex queries

Which is the best method to use for the CodeIgniter 4 among the Model and the Query Builder class? What are the limitations of the CodeIgniter 4 Model and the Query Builder?
I searched a lot about this question. But I did not get any clear answer. For complex queries currently, I am using the Query Builder class. But I want to know what is the best method among the CodeIgniter 4 Model and the Query Builder class. Your answers are highly appreciated.
It depends on your needs and preferences.
Model class - This is used by extending the base Model class, and you can add your own functions/methods. It is not as flexible as the Query Builder, and it only has pre-defined methods.
Query Builder - Easy to build complex queries. But more room for errors if didn't handle it. Ex SQL injection if you failed to validate it properly.

Repository pattern in Laravel using Eloquent

For a project at work I'm creating an API in Laravel. I wanted to use MongoDB as database driver which is new to me. So I want to use Eloquent with MySQL in the beginning and when I'm confident enough switch to MongoDB.
I was reading some tutorials about the repository pattern in Laravel and saw that some returned an eloquent model, like this one. It seems to me that when returning a model on for example create($data), you're limited to database drivers which are suitable with Eloquent. I was wondering if it is a good practice to return an eloquent model. Because if I want to use MySQL for now and in the future MongoDB (without the Laravel MongoDB package), I need to rewrite some code in the controllers because the use the eloquent models instead of an array (for example). Maybe a 'wrapper' between the model and your code is a possibility? Any good recommendations, tips or thoughts on this?
If you're wondering why I want to use MySQL for now and later switch to MongoDB, the reason is there is a time limit on the project. I'm still learning Mongo. So to fulfil the needs of my employer, I'm not using MongoDB until finishing the first version / prototype.
If you use eloquent and its methods, you should be good. Just do not use the DB::raw() method as it may break your query if it does not match the DB engine you are currently in.
I wrote a repository pattern article if you want to look at it (with tdd).
But if you really want future proof, you can mix Doctrine w Laravel since Doctrine is a Data Mapper Pattern. Link
With laravel-doctrine package, you persist the data in your db engine that you prefer.
Is something like laravel-mongodb good?
Extending Eloquent to use MongoDB instead of MySQL.

Is there any method like code first in Laravel?

I am searching for a way for creating database by my models structure in Laravel 5.5.
I know many Orm Like Doctorin Or ActiveRecord has this feature and so some micro Orm such as redbean has this feature but I cannot find a good reference for this ability in eloquent ORM of Laravel and if there is any method has it migrations which alter my existing database according to my model changes?

Do I need to create seperate model for each table in database

My question is more theoritical, as I am not quite sure if it is a better way to create a model in Laravel for each table on database, if yes, what would be the benefition of it?
I am using Laravel 4 Eloquent for the ORM.
Thanks
The basic answer is yes, you should have a model for each table.
But the long answer is "it depends". As for what "depends" is, it is something that comes with experience and also your design criteria. There is no 100% right answer that can be used everytime.
As a principle if you plan on accessing data from tables using Eloquent, then you generally need one eloquent model per table, so you can access the table using Eloquent functions.
As a principle you dont need a model if you never use Eloquent to access the data. i.e. perhaps you have a table that you only use the query builder on.

Using Fluent in a Model

I would like to know how to use Fluent Query Builder in a model? I cannot find any example on how to do it properly. All examples are using Eloquent ORM. From laravel's documentation, to create Eloquent model is by extending Eloquent class :
class User extends Eloquent {}
I understand Eloquent can be great but I am new to laravel right now and all Eloquent is doing is it's confusing me. How would I write the model? so that i can use it in my controller?
I come from codeigniter where I would write
class some_model extends CI_Model {}
and i was able to easily autoload this model into my control and take advantage of it. How is it done with fluent?
The Eloquent ORM extends Fluent, so all the fluent methods are available. Eloquent is just like some syntax sugar. I suggest you get used to it, it will keep your code cleaner.
Heres a example:
// Fluent query builder
DB::table('users')->where('id', '=', 1)->first();
// Eloquent
User::find(1);
// Generated SQL
select * from users where id = 1 limit 1;
Both generate the same SQL, and behind the scenes Eloquent is using Fluent. The main difference is that using Eloquent requires you to have models that extend the Eloquent model.
The important thing to understand is that Eloquent methods are available to you ONLY if you extend the Eloquent class. The fluent query builder methods are always available to you as long as you have specified a correct database for your application.
So why use Eloquent at all?
As in the above example Eloquent and Fluent generates the same SQL, but theres still major differences in the returned result when the response is more complex.
The Fluent query builder will return a "simple" response with just the values, theres no methods available. This is depending on your PDO settings.
Eloquent will do much more for your. You get methods available to you that comes straight from the Eloquent model. Theres also one great benefit here, Eloquent will return a collection that implements has many useful interfaces. This means you can do a lot with the returned data.
Heres some good reads:
Whats eloquent and Fluent?
Eloquent collections
Well, you can just use it like the docs are displaying. Example:
<?php
class Foobar extends Eloquent
{
public static function retrieve($code, $language)
{
return static::where('code', $code)->where('language', $language)->first();
}
}
$foobar = Foobar::retrieve('code', 'EN');
I'm using static::where because this is a static method. (no shit, Sherlock (-: )
You can also just use $this->where() or any other method then where() if the model is initialized. link to query builder docs

Categories