eloquent

Laravel using Sum and Groupby

♀尐吖头ヾ 提交于 2019-12-31 03:01:42
问题 I would like to fetch sum of quantity in each month, so that I can display on bar chart quantities against month This is what I thought but didn't workout $data1 = Borrow::groupBy(function($d) { return Carbon::parse($d->created_at)->format('m')->sum('quantity'); })->get(); My table structure Schema::create('borrows', function (Blueprint $table) { $table->increments('id'); $table->integer('member_id'); $table->integer('book_id'); $table->integer('quantity'); $table->integer('status')->default

Laravel using Sum and Groupby

本小妞迷上赌 提交于 2019-12-31 03:01:26
问题 I would like to fetch sum of quantity in each month, so that I can display on bar chart quantities against month This is what I thought but didn't workout $data1 = Borrow::groupBy(function($d) { return Carbon::parse($d->created_at)->format('m')->sum('quantity'); })->get(); My table structure Schema::create('borrows', function (Blueprint $table) { $table->increments('id'); $table->integer('member_id'); $table->integer('book_id'); $table->integer('quantity'); $table->integer('status')->default

Convert raw query to Laravel eloquent

和自甴很熟 提交于 2019-12-31 02:55:25
问题 How to convert this raw query to Laravel eloquent way: select c.name as country from country c, address ad, city ci where ad.id = 1 and city.id = ad.city_id and c.code = ci.country_code 回答1: First link Second link Query Builder DB::table("country") ->join('city', 'city.country_code', '=', 'country.user_id') ->join('address', 'address.city_id', '=', 'city.id') ->select('country.name as country') ->where('address.id', 1) ->get(); Eloquent Country::with(['city','address' => function($query){

How to get average with orderBy Desc in Laravel 5

两盒软妹~` 提交于 2019-12-31 02:43:07
问题 I have 2 tables in my database. books and ratings in books id , name in ratings id , book_id , rating i have set has many relationship for these models. So in Book model - public function ratings() { return $this->hasMany('App\Rating'); } in Rating Model - public function book() { return $this->belongsTo('App\Book'); } Now i want to fetch all books with there average rating but order by high rating. so i high rated books first and then low ratings. So How i can join 2 tables to achieve this

How to get average with orderBy Desc in Laravel 5

旧巷老猫 提交于 2019-12-31 02:43:05
问题 I have 2 tables in my database. books and ratings in books id , name in ratings id , book_id , rating i have set has many relationship for these models. So in Book model - public function ratings() { return $this->hasMany('App\Rating'); } in Rating Model - public function book() { return $this->belongsTo('App\Book'); } Now i want to fetch all books with there average rating but order by high rating. so i high rated books first and then low ratings. So How i can join 2 tables to achieve this

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias on relationship

百般思念 提交于 2019-12-30 17:29:09
问题 So, I'm receiving the following error from Laravel framework; but I couldn't find why this framework is producing this error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'participants' (SQL: select `participants`.*, `participants`.`message_id` as `pivot_message_id`, `participants`.`user_id` as `pivot_user_id`, `participants`.`created_at` as `pivot_created_at`, `participants`.`updated_at` as `pivot_updated_at` from `participants` inner join `participants` on

Eloquent casts decimal as string

元气小坏坏 提交于 2019-12-30 09:30:58
问题 I have a decimal field to represent money in my Eloquent Model and I'm noticing erroneous results when trying to add some number to this field. Upon further inspection, I found that the decimal field is being cast as a string , like "1245114.00" in the tinker console. I checked the table structure and I can verify that the field is indeed decimal(11,3) . This question was asked before but has no answers. Why is this happening? 回答1: You need to define in your model which fields need to be cast

laravel 4: validation unique (database) multiple where clauses

六月ゝ 毕业季﹏ 提交于 2019-12-30 06:48:25
问题 The laravel 4 documentation mentions unique field validation. They explain here how to include where clauses to the unique validation. A single WHERE clause for the unique table for example: $validator = Validator::make( array( 'name' => 'John Doe' ), array( 'name' => 'unique:table,field,NULL,id,field1,value1' ) ); http://laravel.com/docs/validation#rule-unique Now i assume this does something like: "SELECT id FROM table WHERE field = 'John Doe' AND field1 = value1 LIMIT 1" Then check's if

How to order grouped results using Eloquent?

喜欢而已 提交于 2019-12-30 06:17:09
问题 I've been trying to figure this out for some time now and just can't seem to make it work. I have a table that looks similar to this. Table: Issues id yearly_issue year stock created_at updated_at magazine_id 1 10 2000 1 [timestamp] [timestamp] 3 2 12 1994 6 [timestamp] [timestamp] 10 3 36 2007 10 [timestamp] [timestamp] 102 4 6 2002 7 [timestamp] [timestamp] 7 5 6 2002 2 [timestamp] [timestamp] 5 6 12 2003 9 [timestamp] [timestamp] 10 7 11 2003 12 [timestamp] [timestamp] 10 My problem is

two foreign keys, how to map with laravel eloquent

二次信任 提交于 2019-12-30 06:10:08
问题 I have two tables in MySQL, where the first one is called users and the second one is called games. The table structure is as follows. users id (primary) email password real_name games id (Primary) user_one_id (foreign) user_one_score user_two_id (foreign) user_two_score My games table is holding two foreign relations to two users. My question is how do I make the model relations for this table structure?? - According to the laravel documentation, I should make a function inside the model and