我想把 Product 和 ProductPrice 表放在一起,但我的导师说不要使用 eloquent 关系。所以这是他说要使用的代码:
这是 App\Http\Controllers\Api\ProductController.php 中的一个函数,它使用了他的自定义左连接:
public function table($dproduct, $start, $search, $sortColumn, $sort, $index, $limit)
{
$result['dproduct'] = $dproduct;
$result['data'] = array();
$result['recordsFiltered'] = 0;
$result['recordsTotal'] = 0;
$product = new Product();
$productResult = $product->getResult($search, $sortColumn, $sort, $index, $limit);
if ($productResult['Total'] > 0) {
$result["recordsFiltered"] = $productResult['Total'];
$counter = $start;
$data = array();
foreach ($productResult['Data'] as $productData) {
$counter++;
$data[] = $this->set($counter, $productData);
}
$result["recordsTotal"] = $counter;
$result["data"] = $data;
} else {
$result["error"] = $productResult['Message'];
}
return $result;
}
这是实际的 ProductController(非 API):
class ProductController extends Controller
{
const PRODUCT = 'product';
public function __construct()
{
return parent::authenticate();
}
public function index()
{
$admin = $this->getAdmin();
$productView = $this->isView(ProductController::PRODUCT);
$productCreate = $this->isCreate(ProductController::PRODUCT);
$productUpdate = $this->isUpdate(ProductController::PRODUCT);
$productDelete = $this->isDelete(ProductController::PRODUCT);
if ($productView) {
return parent::setCompactView(ProductController::PRODUCT, compact(
'admin',
'productView',
'productCreate',
'productUpdate',
'productDelete'
));
} else {
return parent::setCompactView('error', compact('admin'));
}
}
}
这是我的产品型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\GlobalController;
class Product extends Model
{
protected $table = 'Product';
protected $primaryKey = 'Id';
public $incrementing = false;
const CREATED_AT = 'CreatedDate';
const UPDATED_AT = 'UpdatedDate';
public function getAll()
{
return self::where('Active', true)->get();
}
public function getById($Id)
{
return self::where('Id', $Id)->first();
}
public function getResult($search = '', $sortColumn = 0, $sort = '', $page = 0, $limit = 0)
{
if ($sortColumn == 1) {
$sortColumn = 'Description';
} else {
$sortColumn = 'Name';
}
if ($sort == '') {
$sort = 'asc';
}
$data = self::where(function ($query) use ($search) {
return $query->where('Name', 'like', '%' . $search . '%')
->orWhere('Description', 'like', '%' . $search . '%');
})
->skip(($page - 1) * 10)
->take($limit)
->orderBy($sortColumn, $sort)
->get();
$count = self::where(function ($query) use ($search) {
return $query->where('Name', 'like', '%' . $search . '%')
->orWhere('Description', 'like', '%' . $search . '%');
})->get()->count();
$result['Data'] = $data;
$result['Total'] = $count;
if ($count <= 0) {
$result['Message'] = GlobalController::TEXT_NO_DATA;
}
return $result;
}
public function getTotal()
{
return self::get()->count();
}
public function setDelete($id)
{
return self::where('Id', $id)->delete();
}
}
这是我的 ProductPrice 模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Http\Controllers\GlobalController;
class ProductPrice extends Model
{
protected $table = 'ProductPrice';
protected $primaryKey = 'Id';
}
ProductPrice 还没有控制器,因为我真的不明白要输入什么。
Product 表有这些表:
Schema::create('Product', function (Blueprint $table) {
$table->uuid('Id')->primary();
$table->uuid('CategoryId')->nullable();
$table->string('Name', 60)->charset('utf8');
$table->mediumText('Description')->charset('utf8')->nullable();
$table->uuid('CreatedId')->nullable();
$table->timestamp('CreatedDate')->useCurrent();
$table->uuid('UpdatedId')->nullable();
$table->timestamp('UpdatedDate')->useCurrent();
});
Schema::table('Product', function ($table) {
$table->foreign('CategoryId')->references('Id')->on('Category')->onDelete('set null');
$table->foreign('CreatedId')->references('Uid')->on('Admin')->onDelete('set null');
$table->foreign('UpdatedId')->references('Uid')->on('Admin')->onDelete('set null');
});
ProductPrice 是这样的:
Schema::create('ProductPrice', function (Blueprint $table) {
$table->uuid('Id')->primary();
$table->uuid('ProductId');
$table->integer('Price')->default(0);
$table->integer('CutPrice')->default(0);
$table->integer('SpecialPrice')->default(0);
$table->smallInteger('Discount')->default(0);
$table->uuid('CreatedId')->nullable();
$table->timestamp('CreatedDate')->useCurrent();
$table->uuid('UpdatedId')->nullable();
$table->timestamp('UpdatedDate')->useCurrent();
});
Schema::table('ProductPrice', function ($table) {
$table->foreign('ProductId')->references('Id')->on('Product')->onDelete('cascade');
$table->foreign('CreatedId')->references('Uid')->on('Admin')->onDelete('set null');
$table->foreign('UpdatedId')->references('Uid')->on('Admin')->onDelete('set null');
});
我的问题是,我需要做什么才能在 ProductId 上将它们作为外键连接在一起,然后在同一个表视图的视图中显示它们?