eloquent

get row count according to the month in the date in Laravel

久未见 提交于 2019-12-25 03:27:26
问题 I want a query in laravel to group by data with dateofSale's month with the count of rows with the same month. here is my table and I want to output something like this numberOfSales | Month 2 Nov 1 Oct how can I do it? 回答1: You should be able to achieve this what you're after with: $data = \App\SaleData::selectRaw('COUNT(*) as count, YEAR(dateOfSale) year, MONTH(dateofSale) month') ->groupBy('year', 'month') ->get(); If you don't include the year as well then you'll have sales from different

Return collection based on model method result

╄→尐↘猪︶ㄣ 提交于 2019-12-25 03:21:14
问题 I have a User model with a Credits relation. public function credits() { return $this->hasMany('App\Credit'); } I'd like to return all users where their credit balance is greater than 0. Right now, I have two methods; one to retrieve the total credits the user has amassed and another to retrieve the credits the user has spent. public function creditsIncome() { return $this->credits->where('type', 0)->sum('amount'); } public function creditsExpense() { return $this->credits->where('type', 1)-

How to seed multiple relationships in Laravel with Faker

家住魔仙堡 提交于 2019-12-25 03:13:55
问题 I have a database with two columns, brands and shops. Each brand can owe several shops, and I want to seed my database via Fakers using Laravel. So after setting up the migrations and the relationships in the models <?php namespace App; use Illuminate\Database\Eloquent\Model; class Brand extends Model { /** * Get the shops for the brand. */ public function shops() { return $this->hasMany('App\Shop','sh_brand_id'); } } And: <?php namespace App; use Illuminate\Database\Eloquent\Model; class

Is it possible to perform “future” soft delete in laravel?

走远了吗. 提交于 2019-12-25 01:47:00
问题 I found that the soft-delete in laravel Eloquent ORM is just replacing the null in deleted_at column by a timestamp. When querying a table with soft delete, is it just checking if the deleted_at is null, or it is really comparing the value with current time? I am asking to see if I am able to do schedule delete by setting a future time on the deleted_at column. 回答1: Laravel only checks if deleted_at is not NULL . SoftDeletingScope : public function apply(Builder $builder) { $model = $builder-

Laravel 4/5, order by a foreign column

痞子三分冷 提交于 2019-12-25 01:43:32
问题 In Laravel 4/5 how can order a table results based in a field that are connected to this table by a relationship? My case: I have the users that only store the e-mail and password fields. But I have an another table called details that store the name , birthday , etc... How can I get the users table results ordering by the details.name ? P.S.: Since users is a central table that have many others relations and have many items, I can't just make a inverse search like Details::... 回答1: I would

Laravel - Eloquent overwriting a custom timestamp… WHY?

安稳与你 提交于 2019-12-25 01:17:51
问题 I am making an inventory management system. When a product is out of stock, I make an entry in a table and note the "oos_at" field with the date/time. later, when it is back in stock, i find that entry and update the "restocked_at" timestamp field. However, when I do the second action, my "oos_at" field is overwritten with the timestamp of the query ('updated_at') field... I had to keep track of this "oos_at" field on another entity so it wouldn't get overwritten and then use that field to

Laravel many-to-many relationship query

血红的双手。 提交于 2019-12-25 00:52:05
问题 I am building a Laravel application with a users table and an activities table, and a many-to-many relationship between activities and users implemented with a pivot table called assignments: users: id, ... activities: id, ... assignments: id, activity_id, user_id, ... I need to keep a record of each time a user completes an activity. A user can complete the same activity many times. I am trying to decide on the best way to model this in the database. The options as far as I can see are: a

Laravel - Ignoring fields that don't exist when using model::create()

早过忘川 提交于 2019-12-24 23:36:10
问题 The Problem I can't make Laravel ignore fields that don't exist in the database when present in Eloquent's create method or when seeding the database. In the example below, the "day" field does not exist in the Event table. However, I need it present in the array so I can use it in the EventObserver.php in order to create an $event->day at the same time as creating an $event. I realise I could create the Day in it's own factory, I just want to know if this is possible or even advisable. The

How do I create relationship from json column data on same table?

戏子无情 提交于 2019-12-24 23:33:06
问题 I have something like the following table (tablename = applications ): | id | applicants | | ------------- | ------------- | | uuid-123-abc | [{'first_name':'Dave'},{'first_name':'Steve'}] | I need to be able to create a relationship for it so I can just go: $application->applicants and (in this example) get a collection with two Applicant models. I'm assuming I'll need an Applicant model class somewhere. But I'm not even sure if this can be done because there is no applicant table. I was

Laravel filter based on has one relationship

蹲街弑〆低调 提交于 2019-12-24 23:06:13
问题 I am pulling my hair out over this one, and I feel like I have tried every method! Here is my problem. I have 2 tables USERS ID | FIRSTNAME | EMAIL_ADDRESS 1 | Joe Bloggs | joe@bloggs.com STATUS ID | USER_ID | STATUS | DATE 1 | 1 | 'In' | 2018-06-04 09:01:00 2 | 1 | 'Out' | 2018-06-04 09:00:00 As you can see by the tables above, each user can have many status', but each user has to have 1 most recent status, which I am doing like this (please tell me if I am doing it wrong) public function