在Laravel雄辩方法中联接表

时间:2020-10-02 17:11:18

标签: laravel laravel-5 join eloquent controller

如何用雄辩的方法编写此代码?

$product = DB::table('products')
             ->join('purchase', 'products.id', '=', 'purchase.id')
             ->join('sales', 'purchase.id', '=', 'sales.id')
             ->select('sales.*', 'purchase.*','products.*')
             ->get(); 

1 个答案:

答案 0 :(得分:1)

创建产品模型,并在“产品”模型中与“购买”添加一对多关系。

library(caret)
library(randomForest)

data(iris)
N <- iris
N$Species = ifelse(N$Species == "setosa", "a", "b") 

N$Species = as.factor(N$Species) 

res <- matrix(0, nrow = 10, ncol = 5)
colnames(res) <- c("Threshhold","Accuracy", "PositivePred", "NegativePred", "F-value")
out <- matrix(0, nrow = 9, ncol = 5)
colnames(out) <- c("Threshhold","Avg.Accuracy", "Avg.PosPred", "Avg.NegPred", "Avg.F_Value")


### creating 10 folds 

folds <- cut(seq(1,nrow(N)),breaks=10,labels=FALSE)

for(J in 1:9) {
 thresh = J/10
 dataset<-N[sample(nrow(N)),]              ####  mix up the dataset N
 for(I in 1:10){
    #Segement your data by fold using the which() function 
    testIndexes <- which(folds==I,arr.ind=TRUE)
    N_test <- dataset[testIndexes, ]              ### select each fold for test
    N_train <- dataset[-testIndexes, ]            ### select rest for training 
    rf = randomForest(Species~., data = N_train, mtry=3, ntree=10)
    pred = predict(rf, N_test, type="prob")
    label = as.factor(ifelse(pred[,1]>=thresh,"a","b"))
    confusion = confusionMatrix(N_test$Species, label)
    res[I,1]=thresh   
    res[I,2]=confusion$overall[1]
    res[I,3]=confusion$byClass[3]
    res[I,4]=confusion$byClass[4]
    res[I,5]=confusion$byClass[7]
 } 
print(res)
out[J,1] = thresh
out[J,2] = mean(res[,2])
out[J,3] = mean(res[,3])
out[J,4] = mean(res[,4])
out[J,5] = mean(res[,5])

}
print(out)

创建“购买”模型,并在“购买”模型中与“销售”添加一对多关系。

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Sendgrid.Webhooks.Service;

namespace WebHookSendgrid.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class WebHookController : ControllerBase
    {

        // POST api/values
        [HttpPost("Sendgridwh")]
        public async Task<ActionResult> Sendgridwh([FromBody] object[] eventList)
        {
            var json = JsonConvert.SerializeObject(eventList);

            var parser = new WebhookParser();
            var events = parser.ParseEvents(json);
       
            // this is custom method, you can make yours for your needs  
            await sendGridService.EmailActivity(events);
            
            return new OkResult();
        }
    }
}

创建销售模型。

您可以使用以下语句检索数据。

const result = await this.service.setCustomer(cust).toPromise()

注意:我假设这种关系是一对多的,您也可以根据数据声明,也可以定义一对多的逆向关系,请参阅laravel文档https://laravel.com/docs/8.x/eloquent-relationships#one-to-many

您将以不同的键获取购买和销售数据,因此您可以使用以下语法对其进行循环。

public function purchases()
{
    return $this->hasMany('App\Models\Purchase');
}