AngularJS& Laravel 4.2从Multip表中检索数据并将其联接 [英] AngularJS & Laravel 4.2 retrieving data from multip tables and joining them

查看:91
本文介绍了AngularJS& Laravel 4.2从Multip表中检索数据并将其联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Laravel,最近才获得帮助,可以将数据从一个控制器插入到多个表中.现在,我正在尝试从数据库中检索数据,并在需要进行编辑或有人需要查看数据以确认其正确的情况下填充表单.我不明白如何在Laravel模型中加入表格?还是我必须创建一个联接SQL查询?

I've recently started to work with Laravel and I just recently got help to insert the data into multiple tables from one controller. Now I'm trying to retrieve data from the database and populate the form if an edit is needed or if someone needs to look at the data to be able to confirm it is correct. I don't understand how I join the tables is this in the model in Laravel? Or do I have to create a join SQL query?

Travelbill.php模型

<?php

  class Travelbill extends Eloquent {

    protected $table = 'travelbill';
    protected $primaryKey = 'TravelbillId';
    protected $fillable = array('ResourceId', 'Destination', 'StartDay', 'StartTime', 'EndDay', 'EndTime', 'Invoice', 'TravelCompensation');
    public $timestamps = true;

    public function travelbill() {
      return $this->belongsTo('User', 'ResourceId', 'ResourceId');
    }

    public function cost() {
      return $this->hasOne('Cost', 'TravelbillId', 'TravelbillId');
    }

    public function allowance() {
      return $this->hasOne('Allowance', 'TravelbillId', 'TravelbillId');
    }
  }

 ?>

Cost.php模型

<?php

  class Cost extends Eloquent {

    protected $table = 'cost';
    protected $primaryKey = 'CostId';
    // protected $fillable = array('TravelbillId', 'Description', 'DescriptionByHiq', 'SekInklMoms', 'SekExklMoms', 'SekInklMomsByHiq', 'SekExklMomsByHiq', 'Currency', 'ExchangeRate');
    protected $fillable = array('TravelbillId', 'Description', 'DescriptionByHiq', 'SekInklMoms', 'SekMoms', 'SekInklMomsByHiq', 'SekMomsByHiq', 'Currency', 'ExchangeRate');
    public $timestamps = false;

    public function cost() {
      return $this->belongsTo('Travelbill', 'TravelbillId', 'TravelbillId');
    }
  }


 ?>

Allowance.php模型

<?php

  class Allowance extends Eloquent {

    protected $table = 'allowance';
    protected $primaryKey = 'AllowanceId';
    protected $fillable = array('TravelbillId', 'DayAllowance', 'Breakfast', 'Lunch', 'Dinner');
    public $timestamps = false;

    public function allowance() {
      return $this->belongsTo('Travelbill', 'TravelbillId', 'TravelbillId');
    }
  }


 ?>

或者我在哪里加入,所以如果我得到旅行单,它还会随请求获得费用和津贴数据?

Or where am i doing this join so if i get the travelbill it also gets the Cost and Allowance data with the request?


我需要将数据输入到AngularJS表单中,因此,如果id = 1的人需要编辑其Travelbill,他应该可以单击编辑按钮,并查看ha填写的信息.


I need to get the data into my AngularJS form so if a person with id = 1 needs to edit his Travelbill he should be able to click a edit button and see the information ha has filled out.

推荐答案

您可以在此处执行的最简单的数据检索形式是执行以下操作:

The simplest form of data retrieval you can do here is to do the following:

$travelBills = Travelbill::with(['code','allowance'])->get();

这是急切加载",将执行三个查询:

This is Eager Loading and will perform three queries:

  • 加载所有旅行账单
  • 加载所有具有与所有旅行单ID匹配的外键的代码,并将其分配给每个旅行模型
  • 对津贴做同样的事情

最终,每个Travelbill模型将已经具有一个关联的Code and Allowance模型,使您可以像这样工作:

What you'll have in the end that every Travelbill model will already have an associated Code and Allowance model, allowing you to work like:

echo $travelBill->cost->SekInklMoms;

对于您加载的其中一份旅行单.请注意第一个查询中的几件事:

for one of the Travelbills you loaded. Note a couple of things in the first query:

  • 旅行单没有经过 过滤,我们现在正在全部加载它们.
  • 我们只是简单地这样做,不一定有效.我建议首先先熟悉一下加载的关系,然后再进行联接之类的事情(在任何情况下,这都会打破Eloquent ORM的精神)
  • The travelbills are not filtered, we are loading them all at this point.
  • We are doing it simply, not necessarily efficiently. I recommend first get comfortable with the relationships loading before get onto things like joins (which break the spirit of Eloquent ORM in any case)

这篇关于AngularJS&amp; Laravel 4.2从Multip表中检索数据并将其联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆