It's hard to explain what I want exactly but I've gotta try to...
Laravel Eloquent inspired me to write a simple php class to work with databse.
As we know We can do this in laravel:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get();
Also we do that:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->count();
Also we can do that:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->first();
Even we can do that too:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->pluck('id')->toArray();
And that I have not ever tried but I believe it works too:
$run = DB::table('users')->where('id', 3)->where('level', 2)->get()->pluck('id')->toArray()->first();
The question is "How does it works?"
What a hell and How should I white to return suitable results in any of there ways.
// It was easy to write my code to return total results if I write like that
$run = DB::from('users')->where('id', 3)->where('level', 2)->get()->count();
// Or to return first result if I write like that
$run = DB::from('users')->where('id', 3)->where('level', 2)->get()->first();
// But what sould I do to return all the results if write like that (As eloquent works).
$run = DB::from('users')->where('id', 3)->where('level', 2)->get();
I need something like "if - else case for methods" like:
function __construct() {
if(if aint`t no calling any methods except **get()** ){
// Lets return default method
return $this->results();
}
else{
// Do whatever...
}
}
There is my whole code:
https://github.com/amirandev/PHP-OOP-DB-CLASS/blob/main/db.php
CodePudding user response:
As I know, when you are trying something like that
$run = DB::from('users')->get()->count();
You get all users and php/laravel count users, meaning
$users = DB::from('users')->get(); // get all users
$usersConut = $users->count(); //count them away of database/mysql
The same thing with first()
when you are using this code DB::from('users')->count();
you are actually asking MySql for count not counting them in the backend.
I highly recommend using this package barryvdh/laravel-debugbar
to help you see the database queris.
CodePudding user response:
Each method returns $this
. So the next method will have the class with the modificatations done by the previous method. It's quite easy to achieve that.
class Example
{
private string $sentence = '';
public function make()
{
return $this->start()->end();
}
public function start()
{
$this->sentence .= 'This will be a ';
return $this;
}
public function end()
{
$this->sentence .= 'whole sentence.';
}
}
BTW, Eloquent query builder converts the method chain into an SQL query string. That's why it works really fast. If you just query one table, let's say users
, via the query builder, and then filter the results in your application (like it was based on the where
condition), the proccess will be quite slow because the hard work will be done by your app.
SQL is extremely fast, that's why we want to complete as many tasks as possible on the SQL side.