Need to move core files to parent folder
Category Archives: Laravel
Where with Subquery
Laravel Example
1 2 3 4 5 6 7 8 9 10 11 12 |
$relations = DB::table('tenten_pms_product as a') ->join('tenten_pms_product_category as b', 'b.product_id', '=', 'a.id') ->select('b.product_id') ->where('b.category_id', $cat_id) ->whereRaw('(select count(*) from tenten_pms_product_model as suba where suba.product_id = a.id and suba.enabled = 1) > 0') ->orderBy('a.state_new', 'desc') ->orderBy('b.sort_order', 'asc') ->orderBy('a.created_at', 'desc') ->get(); //logg($relations); |
SQL
1 2 3 4 5 6 7 8 9 10 11 12 |
SELECT a.*, (select count(*) from tenten_pms_product_model as suba where suba.product_id = a.id and suba.enabled = 1) as model_count from tenten_pms_product as a join `tenten_pms_product_category` as b on b.product_id = a.id WHERE b.`category_id` = '41' and (select count(*) from tenten_pms_product_model as suba where suba.product_id = a.id and suba.enabled = 1) > 0 ORDER BY a.state_new desc, b.`sort_order` asc LIMIT 50 |
Reference:
Eloquent find() not work – always return null
Could be soft deleted.
https://stackoverflow.com/questions/37719932/laravel-eloquent-modelfind-doesnt-work
Order by Array
Sample
1 2 3 4 |
$prods = $prods->orderBy(DB::raw('field(a.id, ' . implode(', ', $final_arr) . ')')) ->skip($skip) ->take($take) ->get(); |
Reference
Get Daily Data using Laravel Query Builder
Query Builder
1 2 3 4 |
$result = DB::table('orders') ->select(DB::raw("sum(total) as total, DATE_FORMAT(created_at,'%Y-%m-%d') as date_label")) ->where('status', 1) ->get(); |
Hash Handlers in Laravel
Make password
1 2 3 |
use Illuminate\Support\Facades\Hash; Hash::make('12345'); |
Check password
1 |
$result = Hash::check($password, $user->password); |
Reference
Laravel API
Add column “api_token” in table users
1 2 |
ALTER TABLE `users` CHANGE `api_token` `api_token` varchar(80) COLLATE 'utf8mb4_unicode_ci' NULL AFTER `remember_token`; |
Postman issues in local
Postman doesn’t apply settings in host file, so don’t use “example.test”, change to “localhost:8000”.
Token
1 2 3 4 5 6 |
//Origin Token, should be used to login $token = \Str::random(60); $admin->forceFill([ 'api_token' => hash('sha256', $token), ])->save(); |
Reference:
Reflection Exception: Class Log does not exist error
Could be invalid format in config files.
- .env
- constants.php (old laravel)
e.g. define(‘BASE_URL’, url(‘/’)); -> error
define(‘BASE_URL’, “http://localhost”); -> OK
Create Path with Laravel File Facade
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
use Illuminate\Support\Facades\File; use Intervention\Image\Facades\Image; private function make_thumb($file){ $file_path = storage_path('/app/media' . $file); //logg(file_exists($file_path)); //logg($file_path); $img = Image::make($file_path); $img->resize(600, null, function ($constraint) { $constraint->aspectRatio(); }); $thumb_dir = storage_path('/app/pcat_thumb' . dirname($file)); if(file_exists($thumb_dir) == false){ File::makeDirectory($thumb_dir, 0777, true, true); } $thumb = storage_path('/app/pcat_thumb' . $file); $img->save($thumb); } |
Sort by array values
1 2 3 4 5 6 |
static function items_by_ids($ids){ $result = self::whereIn('id', $ids) ->orderBy(DB::raw('field(id, ' . implode(',', $ids) . ')')) ->get(); return $result; } |
Reference: