eloquent

Eloquent ORM aggregation vs MySql aggregation

让人想犯罪 __ 提交于 2020-01-05 05:11:12
问题 I'm working on a Laravel 5.2 project and I'm using Eloquent models, but I'm not sure how to approach to aggregation features. Let's say I have Product and Price models and a hasMany relationship, so a product can have multiple prices. Now, in my Product model I'd like a function that will give me an average price of the product. I found 2 ways to get the same result: 1.) Eloquent aggregation public function avg_price() { return $this->prices()->avg('price'); } 2.) MySql aggregation public

How to notify the admin if the items(Multiple items) are going to expire in days later-Laravel

为君一笑 提交于 2020-01-05 04:07:11
问题 There are many items in the Items table and there is an expired date column in the items table. I just want to get a push notification every day before each items going expire one date before (Expire dates are different to each other and can have one expire date to multuple items). im new to laravel. Please help me. 回答1: This can be simply done with Laravel task scheduling You need to create a custom artisan command like php artisan make:command SendItemExpiryEmailsToAdmin Under App\Console

Laravel eloquent query right data

时间秒杀一切 提交于 2020-01-05 04:04:09
问题 I am fairly new to laravel and eloquent. I have 2 tables threads and messages you can see the structure in the links below: threads messages Now my goals is to query only the threads from table threads that have the same user_id in the messages table as the logged in user . Is there a way to do this via eloquent or do I have to write a query for this? I currently get all the threads like this: $thread = Thread::findOrFail($id); But this gives security issues since you can go to any thread if

Laravel - Dynamic relationship using hasManyThough() and unique merge

佐手、 提交于 2020-01-05 03:05:14
问题 I can think of several ad-hoc ways to do this, but I'm really looking for a 'best practices' type of solution. I have 3 tables involved - users (user_id) - usages ('user_id', 'provider_id', 'nurse_id', 'patient_id') - usage_alerts ('usage_id') Im trying to eager load alerts using hasManyThrough() based on a user's role. The user_id field is agnostic, and can apply to any role, so merging and filtering needs to take place. Using $this->hasManyThrough('UsageAlert', 'Usage')->get() will return a

Laravel 4 - Select related models

匆匆过客 提交于 2020-01-04 14:31:19
问题 I have following table Schema::create('jokes_categories', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('is_active'); $table->timestamps(); }); Schema::create('jokes', function(Blueprint $table) { $table->increments('id'); $table->string('content', 200)->unique();; $table->enum('is_active', array('Y', 'N')); $table->integer('category_id')->unsigned(); $table->foreign('category_id')->references('id')->on('jokes_categories'); $table->timestamps();

Laravel 4 - Select related models

霸气de小男生 提交于 2020-01-04 14:30:48
问题 I have following table Schema::create('jokes_categories', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('is_active'); $table->timestamps(); }); Schema::create('jokes', function(Blueprint $table) { $table->increments('id'); $table->string('content', 200)->unique();; $table->enum('is_active', array('Y', 'N')); $table->integer('category_id')->unsigned(); $table->foreign('category_id')->references('id')->on('jokes_categories'); $table->timestamps();

Laravel: Error [PDOException]: Could not Find Driver in MySQL

偶尔善良 提交于 2020-01-04 14:15:03
问题 I'm trying save a register in my MySQL database using Eloquent of Laravel 5 I edited database information in .env file, localized in my system root path, and in config/database.php maintain 'default' => 'mysql' and other mysql connection information (it using env('DB_DATABASE', 'forge') for get informations) I can use Migration , but when I trying save register using function save() of my Model : $newClient = new Client; $newClient->name = "John"; $newClient->save(); It will occur the

Assign User to any Shop in Laravel Relationship?

梦想的初衷 提交于 2020-01-04 06:38:29
问题 In laravel, I have 3 table User // for Authentication and Create Another User Once logged in Expense Shop My Purpose- I want user can register and Also, can create another user when they logged in And Can assign user to another Shop as they want.. And Only User in the Same Shop Can see their Expense .. // My User Table <pre> Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('shop_id')->nullable(); $table->unsignedBigInteger('user_id'

Eloquent - using Union to combine two symmetric relationships throws error

不打扰是莪最后的温柔 提交于 2020-01-04 05:42:32
问题 I built a many-to-many relationship between users as friends through a pivot table. They have to have different foreign keys in the pivot ( friends ) table, user_from_id , user_to_id . In the User model, I define two relationship: public function friends_from() { return $this->belongsToMany('App\User,'friends', 'user_from_id`, `user_to_id`); } ... and friends_to symmetrically, which works fine. But it still makes it hard to consolidate just all "my friends. I thought I found a really clever

Laravel only delete a model if no related model exist

大城市里の小女人 提交于 2020-01-04 05:42:16
问题 Lets say i have a model called 'manufacturer' and this model has one to many relation with another model 'vehicle'. Now i dont want to let users delete a manufacturer if there are any vehicles associated with this model. //In Manufacturer model public function vehicles() { return $this->hasMany('Vehicle'); } And in the repository/controller i have another method to check for it. public function checkAssociatedVehicles($id) { return Manufacturer::with('vehicles')->find($id)->toJson(); } This