PHP code example of sirmathays / convenient-laravel-macros
1. Go to this page and download the library: Download sirmathays/convenient-laravel-macros 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/ */
sirmathays / convenient-laravel-macros example snippets
$query = User::query()->selectKey();
$query->toSql() // "select `id` from `users`"
$query = User::query()
->whereLike('name', 'Matti Suo', 'right')
->orWhereLike('name', 'ranie');
->orWhereLike('name', 'mi', 'left');
$query->toSql(); // "select * from `users` where (`name` LIKE ?) or (`name` LIKE ?) or (`name` LIKE ?)"
// First ? being "Matti Suo%", second "%ranie%" and third "%mi"
$query = User::query()->whereLike('games.name', 'Apex Leg', 'right');
$query->toSql();
// select * from `users` where (exists (select * from `games` where `users`.`id` = `games`.`user_id` and `name` LIKE ?))
// ? being "Apex Leg%"
$query = User::query()->selectRawArr([
'concat(`id`, "-", `name`) as id_name'
'concat(`email`, "-", `name`) as email_name'
]);
$query->first()->toArray() // ["id_name" => "1-Matti", "email_name" => "[email protected]"]
// VS
$query = User::query()->selectRaw('concat(`id`, "-", `name`) as id_name, concat(`email`, "-", `name`) as email_name');