Category : eloquent–relationship

The User model has a one to one relationship with the PhoneNumber class: public function phoneNumber() { return $this->hasOne(PhoneNumber::class); } Yet, if the PhoneNumber table has multiple rows with the same user_id (for example 1), multiple models are returned from the Eloquent statement. $multiplePhoneNumbers = User::find(1)->phoneNumber()->get(); This begs the question, what is the purpose of ..

Read more

I want to retrieve some data but I don’t know how to. I have a model called student with this many-to-many relationship defined. public function statusuri() { return $this->belongsToMany(Status::class, ‘status_student’) ->withPivot(‘id’, ‘data_inceput’, ‘data_sfarsit’, ‘document’, ‘status_id’, ‘stare_stagiu_id’) ->withTimestamps(); } The structure of the pivot table status_student is this: id data_inceput data_sfarsit document student_id status_id stare_stagiu_id In ..

Read more

I have a DB query like this: $bal = DB::table(‘user_wallet’)->where(‘wallet_id’,$wallet_id)->where(‘user_id’,$user_id)->first(); So it basically gets the row from user_wallet table that has the the wallet_id of $wallet_id variable and user_id of $user_id variable, which returns this as result: Now I wish to do this query with Eloquent but I don’t know what is the proper way. ..

Read more

I have 3 Table Like This, One of User Table users id, name second one documents id, type third One clinician_credentials id, user_id, document_id => in this user_id from users table and document_id from documents table I am tried hasManyThrough but did not give perfect data, please suggest to me which one relationship is suitable ..

Read more

I have 3 tables like this, One of User table users id, name second one documents id, type third one clinician_credentials id, user_id, document_id => in this user_id from users table and document_id from documents table I am tried hasManyThrough but did not give perfect data. which relationship is suitable in this case? public function ..

Read more

I am trying to make a connection between teams and users. Pretty much Latrust looks like an awesome package BUT some things I believe could be explained better. Let’s pick a team (say: team_id = 1): $team = Team::where(‘id’, $request->team_id)->first(); And let’s pick our users from the team: $roles = Role::get(); $users = User::whereRoleIs($roles->pluck(‘name’)->toArray(), $team)->get() ..

Read more

I have a Laravel Eloquent Model called EmailList and a model called Subscriber. I defined the following hasMany relationship on the EmailList. public function subscribers() { return $this->hasMany(Subscriber::class); } This relationship works fine. I now want to apply a where() condition on the parent and a where() condition on the child. For example, I have ..

Read more

I want to find days between 2 dates in Eloquent Query In Laravel. I have Query Like this, $credentials = Documents::whereIn(‘agency_id’, $agency_ids) ->with(‘dp_credential’) ->get()->toArray(); if (count($credentials)) { foreach ($credentials as $val) { if ($val[‘dp_credential’]) { $current_date = new DateTime(date(‘Y-m-d’)); $expiry_date = new DateTime($val[‘dp_credential’][‘expiry_date’]); $interval = $current_date->diff($expiry_date); $days = $interval->format(‘%r%a’); // if you getting days in ..

Read more

I really don’t know how to put it. I have two tables Order.php and Product.php, a return $this->hasMany(Product::class, ‘id’); in Order.php and return $this->belongsTo(Order::class, ‘product_id’); in Product.php. In my OrderController public function index() { $orders = Order::latest()->paginate(10); return view(‘admin.order.order’)->with([ ‘orders’ => $orders, ]); } in my view @foreach($orders as $order) <tr data-href="{{ route(‘orders.view’, $order->id) }}"> ..

Read more

Property Model: class Property extends Model { use HasFactory; protected $table = ‘properties’; protected $fillable = [‘name’, ‘address’, ‘city’, ‘poskod’, ‘state’, ‘facilities’]; /* Get all the units in this property */ public function units() { return $this->hasMany(Unit::class); } } Unit Model class Unit extends Model { use HasFactory; protected $fillable = [ ‘user_id’, ‘property_id’, ‘type’, ..

Read more

Let’s assume I’ve got a User model ("users" table => id, email, userable_id, userable_type) and a Customer model ("customers" table => id, name etc., not important fields). User model has the following: public function userable() { return $this->morphTo(); } Customer model has the following: public function user() { return $this->morphOne(‘AppModelsUser’, ‘userable’); } Everything works fine ..

Read more

Working on a query in Laravel 8 project for retrieving company details. below is the structure and mysql query. # Getting company param from request. SELECT c.company_name, p.position_name, s.salary FROM tbl_company as c JOIN tbl_company_type as t ON t.id = c.company_type_id JOIN tbl_company_position as p ON p.company_id = c.id JOIN tbl_comapny_salary as s ON s.position_id ..

Read more

I have simple hasOne relationship and fixed list of displayed columns. If to put columns on paginate method, outputed relation data will be null. $model->with([‘item:id,title’])->paginate(5); // item filled correctly $model->with([‘item:id,title’])->paginate(5, [‘id’,’name’]); // item will be null How to make it work together? Sourc..

Read more

So, in my application I have models: user, category, service(i.e subcategory) and userService. There exists many to many relationship between user and category, user and service. And one to many between category and service. // Tables Users : id, name, email, password Categories : id, name Services : id, name, category_id user_services: id, user_id, category_id, ..

Read more

Following query is ignoring the where(‘contact_id’,$id) causing to return results that does not belong to this contact. Why? Transaction::where(‘contact_id’,$id) ->with(‘allocations’) ->whereHas("allocations",function ($query){ $query->select(‘transaction_id’) ->havingRaw(‘transactions.amount > sum(amount)’) ->groupBy(‘transaction_id’); }) ->orDoesntHave("allocations") ->get(); if I remove the whereHas part, it is bringing back the correct result. But I need the whereHas part. ->whereHas("allocations",function ($query){ $query->select(‘transaction_id’) ->havingRaw(‘transactions.amount > sum(amount)’) ..

Read more

There is a query that works when strict => false in database.php of Laravel. I don’t want to make strict => false, I want it to remain true. Is there a way to change this query and achieve the same results? Transaction::where(‘contact_id’, 9) ->with(‘allocations’) ->whereHas("allocations", function ($query) { $query->havingRaw(‘transactions.credit > sum(amount)’); $query->groupBy(‘transaction_id’); }) ->orDoesntHave("allocations") ->get(); ..

Read more

I have two tables transactions and transaction_allocations. One transaction can have many allocations. Transaction Model Schema::create(‘transactions’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->timestamps(); $table->foreignId(‘contact_id’)->constrained(); $table->decimal(‘amount’,19,4)->nullable(); }); Allocation Model Schema::create(‘transaction_allocations’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->timestamp(‘created_at’); $table->foreignId(‘transaction_id’)->nullable()->constrained(); $table->foreignId(‘bill_id’)->nullable()->references(‘id’)->on(‘bills’); $table->decimal(‘amount’,19,4)->nullable(); }); Relationship in Transaction Model public function allocations(){ return $this->hasMany(TransactionAllocation::class); } I need to query all transactions where ..

Read more