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

报告相同问题?