Category : laravel-query-builder

After I executed the query below Laravel display an error saying the enterpriseDB.employee_detail does not exist. Both enadopodDB and enterpriseDB are two different databases. $query = DB::connection(‘enadopodDB’,’enterpriseDB’) ->table(‘affected_employee’) ->join(‘enterpriseDB.employee_detail’, ‘enterpriseDB.employee_detail.employee_detail_id’, ‘=’, ‘affected_employee.employee_id’) ->select(‘affected_employee.*’, ‘enterpriseDB.employee_detail.full_name’) ->get(); var_dump($query); If the query builder is wrong, please suggest a solution for it. Sourc..

Read more

Im trying to create a laravel project using laravel new proj_name but it shows always showing that composer is not recognized as an internal or external command operable program or batch file i have set my environmental variable as C:UsersadminAppDataRoamingComposervendorbin; and my pathext as .PHAR; dont where i made the mistake, it works perfectly with ..

Read more

I have two models with relations as defined below Order public function owner() { return $this->belongsTo(User::class, ‘owner_id’); } User public function company(){ return $this->belongsTo(Company::class, ‘company_id’); } company table have ‘title’ field. what I want is to get all the orders sorted/order by company title. I’ve tried different solution but nothing seems to work. Any help ..

Read more

SELECT DISTINCT tasks.id, tasks.category_id, (CASE WHEN (SELECT task_id FROM completed_user_tasks WHERE user_id = 1 AND task_id = tasks.id) THEN true ELSE false END) AS completed FROM tasks WHERE tasks.id NOT IN ( SELECT task_id FROM skipped_user_tasks WHERE user_id = 1 UNION SELECT task_id FROM shown_user_tasks WHERE user_id = 1 UNION SELECT task_id FROM current_user_tasks WHERE ..

Read more

I have three tables: super_consumers , balance_ins, recharges super_consumers table: id name ———- —- 1 A 2 B balance_ins table: id super_consumer_id amount type(1,2) — —————– —— —- 1 1 10 1 2 1 20 1 3 2 10 2 recharges table: id super_consumer_id amount status(0,1) — —————– —— —— 1 1 5 1 2 ..

Read more

I have a model Survey which is related to hasOne another model Installation which is related to hasMany another model Assignment. So i defined a hasManyThrough relationship like this public function assignments() { return $this->hasManyThrough(Assignment::class,Installation::class); } I want to write a query to fetch any Survey where the Assignments associated with the survey does not ..

Read more

Good Day, I do have 2 tables loan_account_installment and loan_account_installment_repayment related to each other. If repayment was made row will be created at loan_account_installment_repayment. What I want to happen is to deduct the payment made on loan_account_installment_repayment on loan_account_installment table. Unfortunately Im having an error. Call to undefined method stdClass::update(). By the way this is ..

Read more

I need to get Records so that an array of ids come at top of collection. $ids = [15, 20]; I tried : $list = $list->orderByRaw("field(id,".implode(‘,’,$id).")"); But this is working only with whereIn : $list = $list->whereIn(‘id’,$ids)->orderByRaw("field(id,".implode(‘,’,$id).")"); But i need to fetch all records but Ids 15 and 20 at the top. How to achieve ..

Read more

How can I write this query with Laravel Eloquent "SELECT * FROM links WHERE (user_id = 1 OR user_ip = ‘127.0.0.1’) AND id = 53 LIMIT 1" I have written this one: ` $link = Link::query() ->where(‘user_id’, Auth::id()) ->orWhere(‘user_ip’, request()->ip()) ->with(‘clicks’) ->findOrFail($id); And the SQL version of this one is :"SELECT * FROM links WHERE ..

Read more

I am trying to fetch data using slug between 2 table, When I am using Eloquent then everything is fine but How to convert that query in Query Builder. Eloquent Query : $results = Product::orderBy(‘id’,’desc’)->with(‘categories’)->whereHas(‘categories’, function ($query){ $query->where(‘slug’, request()->sub_category); })->paginate(24); Here I am using two tables product and categories. Query Builder : $results = DB::table(‘products’) ..

Read more

I’m trying to do the following: get the average of subscriptions.quantity where the subscriptions.ends_at is NULL and the subscriptions.account_id is connected to accounts who have accounts.partner_id is NULL. I Have the 2 following Laravel queries, does anyone knows what is the better use between them? assuming I have thousands of rows in my DB. $optionOne ..

Read more