 |
รบกวนท่านผู้รู้เรื่องการแปลง Sql ไปยัง Laravel query |
|
 |
|
|
 |
 |
|
ตัวอย่างกาใช้ Colection ในการ Join
$query = Product::join('categories', 'product.category_id', '=', 'categories.id')
->sum('product.amount');
ที่สำคัญคุณต้องสร้าง Model เพื่อทำการ Relation function ในลักษณะต่างๆก่อน เช่น
Model Product
public function category(){
// สินค้า belongsTo ภาษาอังกฤษคือเป็นของ "หมวดหมู่สินค้า"
return $this->belongsTo('App\Models\Category');
}
Model Category
public function products(){
// หมวดหมู่สินค้านี้ hasMany ภาษาอังกฤษคือมีสินค้ามากมาย
return $this->hasMany('App\Models\Product');
}
ซึ่งรายละเอียดมันเยอะ เช่น
- belongsTo
- belongsToMany
- hasOne
- hasMany
https://laravel.com/docs/9.x/eloquent-relationships
|
 |
 |
 |
 |
Date :
2022-06-29 16:45:27 |
By :
Genesis™ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณทุกคอมเมนต์เลยนะครับ ผมไปศึกษาตามที่บอกมาแล้ว
แต่สุดท้ายก็มาจบที่วิธีใช้ DB::select ครอบตัวคิวรี่เอาเลย ประมาณนี้ครับ
Code (SQL)
$result = DB::select("
select
dates,
sum(number) total
from
(
select
dates,
sums as number
from
Table1
union all
select
dates,
alls as number
from
Table2
union all
select
dates,
price as number
from
Table3
) t
group by
dates
");
|
ประวัติการแก้ไข 2022-06-30 20:01:30 2022-06-30 20:02:12
 |
 |
 |
 |
Date :
2022-06-30 20:00:26 |
By :
Blackone23 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$sql = <<<SQL
select dates,sum(number) total
from
(
select dates,sums as number
from Table1
union all
select dates,alls as number
from Table2
union all
select dates,price as number
from Table3
) t
group by dates
SQL;
$data = DB::($sql)
return veiw(......) ->with('data',$data);
น่าะจประมาณนี้ ใช้ Query Builder
|
 |
 |
 |
 |
Date :
2023-08-28 15:13:13 |
By :
Saksiri Sirikul |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|