PHP code example of axn / laravel-database-extension
1. Go to this page and download the library: Download axn/laravel-database-extension 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/ */
axn / laravel-database-extension example snippets
DB::table('appartements')->orderByNatural('numero')->get();
// Descendant
DB::table('appartements')->orderByNatural('numero', 'desc')->get();
// or
DB::table('appartements')->orderByNaturalDesc('numero')->get();
use Axn\Illuminate\Database\Eloquent\DefaultOrderScope;
class MyModel extends Model
{
protected static function booted()
{
static::addGlobalScope(new DefaultOrderScope([
'column' => 'option',
]));
}
}
use Axn\Illuminate\Database\Eloquent\DefaultOrderScope;
class User extends Model
{
protected static function booted()
{
static::addGlobalScope(new DefaultOrderScope([
'lastname' => 'asc',
'firstname' => 'desc',
]));
}
}
// instead of doing joinRel('roles') (User belongs-to-many Role)
User::joinRel('userHasRoles') // User has-many UserHasRole
->joinRel('userHasRoles.role') // UserHasRole belongs-to Role
->get();
// with aliases:
User::alias('u')
->joinRel('userHasRoles', 'uhr')
->joinRel('uhr.role', 'r')
->get();
User::joinRel('userHasRoles', function ($join) {
$join->where('is_main', 1);
})
->joinRel('userHasRoles.role')
->get();
class User extends Model
{
// joinRel('mainAddress', 'a') will do:
// join `addresses` as `a` on `a`.`user_id` = `users`.`id` and `a`.`is_main` = 1
public function mainAddress()
{
return $this->hasOne('addresses')->where('is_main', 1);
}
}
$originalBuilder = User::joinRel('userHasRoles');
$clonedBuilder = clone $originalBuilder;
// Produces error: No model with alias "userHasRoles"
$clonedBuilder->joinRel('userHasRoles.role');
// where exists (select * from `comments` where `comments`.`post_id` = `posts`.`id`)
Post::whereHas('comments')->get();
// where `posts`.`id` in (select `comments`.`post_id` from `comments`)
Post::whereHasIn('comments')->get();
// where `posts`.`id` in (
// select `comments`.`post_id` from `comments`
// where `comments`.`content` like "A%"
// )
Post::whereHasIn('comments', function ($query) {
$query->where('content', 'like', "A%");
})->get();
// where `posts`.`id` in (
// select `comments`.`post_id` from `comments`
// inner join `users` as `author` on `author`.`id` = `comments`.`author_id`
// where `author`.`lastname` like "A%"
// )
Post::whereHasIn('comments', function ($query) {
$query
->joinRel('author')
->where('author.lastname', 'like', "A%");
})->get();
$postTypes = PostType::withoutTrashedExcept($post->post_type_id)->get();
// you also can provide multiple ids:
$postTypes = PostType::withoutTrashedExcept([1, 2, 3])->get();
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Axn\Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
// ...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.