laravel-query-builder

How to use alias column in whereIn with Laravel?

一笑奈何 提交于 2020-08-07 07:48:52
问题 here is my code. I am trying to get new list of A items in order to loop on. $allowed_a = \App\NewA::select('name')->get()->pluck('name'); $a = App\A::selectRaw("replace(unaccent(trim(name)), ' ', '') AS newname, name") ->whereIn('newname', $allowed_a)->get(); But I am getting Undefined column 'newname' . How can I fix it please? thanks 回答1: You should be able to achieve something equivalent using: $allowed_a = \App\NewA; \App\A::selectRaw('replace(unaccent(trim(name)) as newname') ->whereRaw

How to use alias column in whereIn with Laravel?

◇◆丶佛笑我妖孽 提交于 2020-08-07 07:47:51
问题 here is my code. I am trying to get new list of A items in order to loop on. $allowed_a = \App\NewA::select('name')->get()->pluck('name'); $a = App\A::selectRaw("replace(unaccent(trim(name)), ' ', '') AS newname, name") ->whereIn('newname', $allowed_a)->get(); But I am getting Undefined column 'newname' . How can I fix it please? thanks 回答1: You should be able to achieve something equivalent using: $allowed_a = \App\NewA; \App\A::selectRaw('replace(unaccent(trim(name)) as newname') ->whereRaw

Using limit parameter in paginate function

你说的曾经没有我的故事 提交于 2020-08-04 08:55:28
问题 Is it possible to use 'limit' parameter in paginate() function? I'm trying this: $users->where(...)->limit(50)->paginate($page) ...and now, if I have 100 users in the database then the response from paginate function will be all 100 users instead of 50 (or number of users defined with limit parameter). So, my question is: is it possible to implement limit parameter when I use paginate function? 回答1: No, it's not possible to limit the query when using pagination. Query pagination uses skip()

How to filt data by time using Laravel?

筅森魡賤 提交于 2020-05-17 07:00:06
问题 I have this query, I would like to obtain only the data created on the current month public function index(Request $request) { if($request->ajax()) { $data = DB::table('tbl_lista_contactabilidad as a') ->select('a.id','a.rif','a.razon_social','a.postventaatcs_id','fecha_contacto','a.contactado','a.persona_contacto','a.correo_contacto','a.celular_contacto','a.numero_contacto','a.comentarios','a.auditado') ->leftjoin('tbl_equipo_postventaatcs as h','h.id','=','a.postventaatc_id') ->leftjoin(

Is it possible to work in a model with query builder using Laravel?

心不动则不痛 提交于 2020-04-17 20:36:11
问题 I would like to know if is it possible to work in a model using query builder to make a join between two tables, I don't want to use eloquent this is my model <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use DB; class Tbl_Perimetro extends Model { puplic function perimetros(){ $carteras = DB::table('tbl_perimetros') ->join('tbl_equipo_postventaatcs', 'tbl_equipo_postventaatcs.id', '=', 'tbl_perimetros.postventaatc_id') ->join('tbl_lista_carteras', 'tbl_equipo

How count by the first letters in Laravel Query Builder?

荒凉一梦 提交于 2020-02-05 05:58:25
问题 I want to make a count by the first letters... I have this column I would like to count each OE rows and each GICS rows I'm working with this query $data4 = DB::table('incidencias') ->select(DB::raw('grupo_asig as grupo_asig'), DB::raw('count(*) as number')) ->whereNotIn('grupo_asig', ['']) ->groupBy('grupo_asig') ->orderBy('number', 'desc') ->get(); 回答1: Use CASE WHEN and count the field like OE and ASIG $data4 = DB::table('incidencias') ->select(DB::raw("(CASE WHEN grupo_asig LIKE 'OE%'

How can fetching huge records using Laravel and MySQL?

白昼怎懂夜的黑 提交于 2020-02-03 01:42:07
问题 I Need experts Suggestions and Solutions. We are developing job portal website here by handle around 1 million records. We are facing records fetching timeout errors. How can I handle those records using laravel and MySql? We are trying to follow steps: Increase the PHP execution time MySql Indexing Paginations 回答1: You should be chunking results when working with large data sets. This allows you to process smaller loads, reduces memory consumption and allows you to return data to the User

Laravel Query Builder, selectRaw or select and raw

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-22 14:58:06
问题 What's the difference between: DB::table('some_table') ->selectRaw('COUNT(*) AS result') ->get(); and: DB::select(DB::raw(" SELECT COUNT(*) AS result FROM some_table")); In the documentation https://laravel.com/docs/5.6/queries they advert about using raw() due SQL Injection, but it's the same with selectRaw ? 回答1: The end result of both is the same i.e but there are some difference: The first one: DB::table('some_table') ->selectRaw('COUNT(*) AS result') ->get(); Returns a collection of PHP

Laravel Query Builder, selectRaw or select and raw

不打扰是莪最后的温柔 提交于 2020-01-22 14:58:05
问题 What's the difference between: DB::table('some_table') ->selectRaw('COUNT(*) AS result') ->get(); and: DB::select(DB::raw(" SELECT COUNT(*) AS result FROM some_table")); In the documentation https://laravel.com/docs/5.6/queries they advert about using raw() due SQL Injection, but it's the same with selectRaw ? 回答1: The end result of both is the same i.e but there are some difference: The first one: DB::table('some_table') ->selectRaw('COUNT(*) AS result') ->get(); Returns a collection of PHP

Passing conditional param to Eager Loading in larave ends with error

試著忘記壹切 提交于 2020-01-22 02:34:08
问题 my code is as followed: return User::whereHas('roles', function ($role, $query) { return $role; $query->whereId($role); })->get(); what I am trying is to pass role id here to query builder. it ends up with following error: Symfony\Component\Debug\Exception\FatalThrowableError Too few arguments to function App\Http\Controllers\UserController::App\Http\Controllers\{closure}(), 1 passed in /Users/x/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 962 and exactly 2