dqh1992 2018-09-06 09:35
浏览 34
已采纳

AngularJS和Laravel 4.2从多个表中检索数据并加入它们

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 Model

<?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 Model

<?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 Model

<?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?

EDIT:
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.

  • 写回答

1条回答 默认 最新

  • donglangtun1850 2018-09-06 11:52
    关注

    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:

    • Load all the travel bills
    • Load all codes that have foreign keys matching all the travelbill ids and assign them to each travel model
    • Do the same with allowances

    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:

    • 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)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决