PHP code example of angel-source-labs / laravel-expressions
1. Go to this page and download the library: Download angel-source-labs/laravel-expressions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
angel-source-labs / laravel-expressions example snippets
public function testSelectRawUsingExpression()
{
$expression = new Expression("price as price_before_tax");
$sql = DB::table('orders')->selectRaw($expression)->toSql();
$this->assertEquals('select price as price_before_tax from `orders`', $sql);
}
$expression = new Expression("inet_aton(?)", ["192.168.0.1"]);
DB::table('audits')->where('ip', $expression)->get();
public class InetAtoN extends Expression
{
public function __construct($address)
{
parent::__construct("inet_aton(?)", $address);
}
}
DB::table('audits')->where('ip', new InetAtoN("192.168.0.1"))->get();
public class Point extends Expression
{
public function __construct($lat, $lng)
{
parent::__construct("ST_GeomFromText(?, ?)", [$lng, $lat]);
}
}
$model->point = new Point(44.9561062,-93.1041534);
$model->save();
class ClassIsExpression implements IsExpression
{
use ProvidesExpression;
}
function testSelectRawUsingExpression()
{
$expression = new ClassIsExpression("price as price_before_tax");
$sql = DB::table('orders')->selectRaw($expression)->toSql();
$this->assertEquals('select price as price_before_tax from `orders`', $sql);
}
use Illuminate\Database\Query\Expression as BaseExpression;
class Expression extends BaseExpression implements IsExpression
{
use ProvidesExpression;
}
public class Point extends Expression
{
public function __construct($lat, $lng)
{
parent::__construct(ExpressionGrammar::make()
->mySql("ST_GeomFromText(?, ?)")
->mySql("ST_GeomFromText(?, ?, 'axis-order=long-lat')", "8.0")
->postgres("ST_GeomFromText(?, ?)"),
[$lng, $lat]);
}
}
$model->point = new Point(44.9561062,-93.1041534);
$model->save();
$expression = new Expression("price * ? as price_with_tax", [1.0825]);
DB::table('orders')->select($expression)->get();
$expression = new Expression("price * ? as price_with_tax", [1.0825]);
DB::table('orders')->selectRaw($expression)->get();
$expression = new Expression("price * ? as price_with_tax, price * ? as profit", [1.0825]);
DB::table('orders')->selectRaw($expression, [.20])->get();
use App\Models\Income;
$incomes = Income::where('amount', '<', function ($query) {
$query->selectRaw('avg(i.amount)')->from('incomes as i');
})->get();
sql
select `department`, SUM(price) as total_sales from `orders` group by `department` having SUM(price) > ?; # bindings = [1 => 2500]
sql
select `department`, SUM(price) as total_sales from `orders` group by `department` having SUM(price) > ? and AVG(price) > ?; # bindings = [1 => 2500, 2 => 100]
sql
select * from `orders` where `id` in (?, ?, ?, ?) order by field(id, ?, ?, ?, ?); # bindings = [1 => 12, 2 => 23, 3 => 34, 4 => 45, 5 => 12, 6 => 23, 7 => 34, 8 => 45]
sql
select * from `orders` where `id` in (?, ?, ?, ?) order by field(id, ?, ?, ?, ?); # bindings = [1 => 12, 2 => 23, 3 => 34, 4 => 45, 5 => 12, 6 => 23, 7 => 34, 8 => 45]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.