Category : eloquent

my first database host in aws and secound database host in diedem server. and I want to reference one column from laravel_1.uesrs table to multiple other database server in employees table . Host: 192.168.0.100 Database:[laravel_1.uesrs]: https://i.stack.imgur.com/Tq5Mm.png CreateUsersTable Schema::create(‘users’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->string(‘name’); $table->string(’email’); } Host: 10.0.0.12 (different server) Database: [laravel_2.employees]: https://i.stack.imgur.com/QcLY5.png CreateEmployeesTable Schema::create(’employees’, ..

Read more

I have a model called CustomerInfo and i am trying to update it. update returns true but the changes are not reflecting on my db. $customerInfo = CustomerInfo::where(‘machine_name’,$username)->firstOrFail(); $result = $customerInfo->update($data); $data varaible is a array having key value pair. Also tried the following $customerInfo = CustomerInfo::where(‘machine_name’,$username)->update($data); Sourc..

Read more

My Laravel Project have a resource controller named Blogpost, where in destroy method, the $blogpost->delete() approach doesn’t work. But if I use DB Query, it works just fine. See code below. This doesn’t work: public function destroy(Blogpost $blogpost) { $blogpost->delete(); return redirect()->route(‘blog.index’); } This works perfectly: public function destroy($id) { $post = Blogpost::find($id); $post->delete(); return ..

Read more

I want to create query for promotion. my question is how this query to change from SQL to eloquent? SELECT * from Insurance_promotion where Active = 1 AND VehicleNo NOT IN ( SELECT RegistrationNo from insurance_motor where Insurance_promotion.VehicleNo = insurance_motor.RegistrationNo AND InsuranceID In ( SELECT InsuranceID from insurance_holder where insurance_motor.InsuranceID = insurance_holder.InsuranceID and NRIC = ..

Read more

Has MySQL view ‘histstat’. In MySQL select * from histstat works fine. Laravel model is very simple: class HistStat extends Model { use HasFactory; protected $table = ‘histstat’; protected $fillable = [‘day’, ‘total’, ‘paid’]; } Then I want to get first 14 records of hisstat: $dynamic = HistStat::all()->slice(14); … and execution ends with error SQLSTATE[42000]: ..

Read more

I am trying to create an email blaster for more than 100k emails at once using the Laravel framework. My command: public function handle() { $blastId = $this->argument(‘blast’); $blast = Blast::find($blastId); activity()->performedOn($blast)->log(‘Blast is sending’); foreach ($blast->unsentLogs as $log) { try { Mail::queue((new BlastEmail($log))->onQueue(’emails’)); } catch (Exception $e) { activity(‘send_failed’)->performedOn($log->contact)->causedBy($log)->log(‘Email send failed: ‘.$e->getMessage()); } } $blast->status ..

Read more

I need the column date and digits to concatenate. The date should be YY-mm format and the digits should have a leading zero. Example output 21-09-0001 How to do it in WHERE FUNCTION laravel eloquent? like this. I found this on the internet example: public function scopeFindDVNumber($query,$name) { $query->where(DB::raw( "REPLACE( COALESCE(name_first,”),’ ‘, COALESCE(name_middle,”),’ ‘, COALESCE(name_last,”) ..

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

i have 5 filter , kampus,fakulti,program ,semester and status so each filter is always been filter with status it means the status filter will be the selection of switch case and the other filter will be condition in the switch case example : if($student){ $today = date("Y-m-d"); switch($request->stat){ case ‘Y’: if($request->kampus != "-" && $request->fakulti ..

Read more

// how should i join these 4 tables users, pages, category, products using relationships intead in controller. //I also prefer calling these functions from corresponding models using respective function names but not database column names in view. Thanks in advance. These are my models //models class User extends Authenticatable{ use HasFactory, Notifiable; protected $fillable = ..

Read more

How to write the Laravel eloquent query to select the max value without using DB::raw and writing in pure eloquent so that it doesn’t have to put MySQL strict mode to false. Means any alternative to write, DB::raw(‘max(price) as price’) without making MySQL strict mode false. Item::join(‘product’, ‘item.id’, ‘=’, ‘product.item_id’) ->select([‘item_id’, ‘item.title’,DB::raw(‘max(price) as price’)]) ->where(‘product.user_id’, ..

Read more

I’ve this query to list all the users with their corresponding relationships. user has many tasks each tasks has many worktimes and these worktimes can have different users ie, each task can be shared by different users, hence they have individual worktime for each task. i’ve tried the below code $users = User::select(‘users.id’, ‘users.first_name’, ‘users.last_name’, ..

Read more