eloquent

How to count the total price of order?

Deadly 提交于 2020-02-16 10:02:43
问题 I have these tables: Orders: id - status -user_id - address_id 1 await 1 1 products: id - name - price - quantity 1 test1 100$ 5 2 test2 50$ 5 order_product: order_id - product_id - quantity 1 1 2 1 2 2 cities: id - name - shipping_charges 1 NY 30$ How can I count the total price of the these columns: for each product: products(price) * order_product(quantity) for all products with shipping_charges - products(price) * order_product(quantity) + cities(shipping_charges) 回答1: You can retrieve

How to count the total price of order?

隐身守侯 提交于 2020-02-16 09:59:19
问题 I have these tables: Orders: id - status -user_id - address_id 1 await 1 1 products: id - name - price - quantity 1 test1 100$ 5 2 test2 50$ 5 order_product: order_id - product_id - quantity 1 1 2 1 2 2 cities: id - name - shipping_charges 1 NY 30$ How can I count the total price of the these columns: for each product: products(price) * order_product(quantity) for all products with shipping_charges - products(price) * order_product(quantity) + cities(shipping_charges) 回答1: You can retrieve

count relation of relation in laravel

血红的双手。 提交于 2020-02-15 10:02:43
问题 Suppose I have a Conversation model like this : class Conversation extends Model { public function questions (){ return $this->hasMany('App\Question','conversation_id','conversation_id'); } public function category () { return $this->belongsTo('App\Category', 'cat', 'cat_id'); } } And a Question model like this: class Question extends Model { public function conversation () { return $this->belongsTo('App\Conversation', 'conversation_id', 'conversation_id'); } } As you can see there is a

Return queried model in Eloquent attribute

六眼飞鱼酱① 提交于 2020-02-08 04:19:38
问题 I'm using Laravel's eloquent ORM, and I was hoping to be able to return a queried laravel model as an attribute, or more ideally as an eloquent relation. This is what I'm trying to do: class CalendarEvent extends Model { protected $appends = array('conflicts'); public function getConflictsAttribute () { $conflicts = CalendarEvent::where('calendar_event_type','=',$this->calendar_event_type) ->where('start','<',$this->end) ->where('end','>',$this->start) ->get(); return $conflicts; } } This is

Laravel 5.2 soft delete does not work

筅森魡賤 提交于 2020-02-06 04:15:43
问题 I am have simple app with a table post and model: <?php namespace App; use Illuminate\Database\Eloquent\Model; use SoftDeletes; class Post extends Model { protected $table = 'post'; protected $dates = ['deleted_at']; protected $softDelete = true; } I am trying to make example of soft delete and i am using route just for example route.php: <?php use App\Post; use Illuminate\Database\Eloquent\SoftDeletes; Route::get('/delete', function(){ $post = new Post(); Post::find(12)->delete(); }); I have

Laravel query many to Many

断了今生、忘了曾经 提交于 2020-02-05 08:21:30
问题 I need help.I have 2 tables products and categories : Get request sends category id. My question is: how to build a query using the product model??? (The query looks like this: Output the product where the category id is equal to $ request-> category). Table connections are configured, I only need the query, (I read the documentation, but do not not understand it) 回答1: You can use: $products = Product::whereHas('categories', function($q) use ($categoryId) { $q->where('id', $categoryId); })-

How count by the first letters in Laravel Query Builder?

荒凉一梦 提交于 2020-02-05 05:58:25
问题 I want to make a count by the first letters... I have this column I would like to count each OE rows and each GICS rows I'm working with this query $data4 = DB::table('incidencias') ->select(DB::raw('grupo_asig as grupo_asig'), DB::raw('count(*) as number')) ->whereNotIn('grupo_asig', ['']) ->groupBy('grupo_asig') ->orderBy('number', 'desc') ->get(); 回答1: Use CASE WHEN and count the field like OE and ASIG $data4 = DB::table('incidencias') ->select(DB::raw("(CASE WHEN grupo_asig LIKE 'OE%'

Laravel move data from one table to another and drop the column that the data came from?

♀尐吖头ヾ 提交于 2020-02-04 21:41:28
问题 I'm trying to do the following process in a Laravel migration: Create a new table called client_log_partitions . (DONE) $table->create(); $table->bigIncrements('id')->unique(); $table->bigInteger('client_log_id'); $table->mediumText('partition_data'); $table->timestamps(); I already have a table called client_logs with data stored in a bigText() column called log_data . Each row that already exists in the client_logs table needs to be split every 250 KB (or 250,000 characters since it should

Eloquent whereRaw is not working with bindings

旧城冷巷雨未停 提交于 2020-02-04 13:48:10
问题 I have a json column in my database with french characters. So when I use: App\Job::where('name->fr', 'like', '%Fune%')->count(); It is not finding results for jobs that has an accent in the name like Funéraire . I can accomplish what I want by adding a collate in the query using the whereRaw : App\Job::whereRaw('json_unquote(json_extract(`name`, \'$."fr"\')) LIKE \'%Fune%\' collate utf8mb4_general_ci')->count(); However, when I use bindings in my whereRaw method: App\Job::whereRaw('json

can't use paginate when use sortby in Laravel

丶灬走出姿态 提交于 2020-02-04 01:54:06
问题 I Want to get products and sort it by mutator attribute in my case it's called price i search for how to sort it and found that i can use sortBy from the collection like that private function search() { return Product::with(['firstImage', 'category']) ->sortBy('price'); } it works fine, but when I try to add paginate nothing happens and there is no paginate in the response private function search() { return Product::with(['firstImage', 'category']) ->paginate(9)->sortBy('price'); } So how can