我想制作一个api(我使用laravel 4.2作为后端)来使用ionic应用程序。例如:
//我赚了以下$游客:
$tourists = User::where('block','0')
->where('guider', 1)
->where('location', $location)
->orderBy($orderBy, 'desc')
->orderBy('updated_at', 'desc')
->get();
return View::make('frontend.data.touristsData',array('tourists'=>$tourists));
//游客数据:
<?php
echo json_encode($tourists);
//在我的app.js(ionic)中使用过:
.controller('listController', ['$scope','$http','$state','$ionicModal', function($scope, $http, $state,$ionicModal){
$http.get("./touristsData").success(
function(data){
$scope.tourists = data;
......
//用于html
<div>{{tourists.username}}: {{tourists.intro}}</div>
//以上是一张表
但是如果我有两个表,例如,users表和posts表
//用户表(laravel迁移)
$table -> increments('id');//id auto_increment
$table -> string('username',30);
$table -> string('email',60) -> unique();
$table -> string('password',64);
$table -> boolean('admin')->default(0);
$table -> boolean('block')->default(0);
$table -> integer('tradeTime');
$table -> string('nationality', 50);
//岗位表
$table->increments('id');
$table -> integer('needHour');
$table -> string('travelDestination',40);
$table -> text('intro',300);
$table -> string('otherWords', 100);
$table->integer('user_id');
$table->softDeletes();
$table ->timestamps();
//user.php
<?php
use Illuminate'Auth'UserTrait;
use Illuminate'Auth'UserInterface;
use Illuminate'Auth'Reminders'RemindableTrait;
use Illuminate'Auth'Reminders'RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
protected $guard = array('email', 'password');
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function posts(){
return $this->hasMany('Posts');
}
//post.php
<?php
use Illuminate'Database'Eloquent'SoftDeletingTrait;
class post extends Eloquent{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $fillable = ['needHour', 'travelDestination','intro','otherWords'];
public function user()
{
return $this->belongsTo('User');
}
}
我们可以看到user_id将users表和posts表相互链接,所以我的问题是,我们如何输出两个表的内容,就像上面一样,只有一个表可以输出(这很容易)?
我想要实现的是如下代码:
<div>{{tourists.username}} : {{tourists.travelDestination}}</div>
有很多方法可以实现这一点。
您可以使用联接
$userWithDestinations = User::where('condition')->join('destination_table', function ($join)
{
$join->on('user.userID', '=', 'post_table.userID')
})->where('moreConditions')->get()
您可以使用Laravel Eagle Loading(我强烈建议这种方法用于可重复使用的目的)
你的用户模型可能看起来像这个
<?php
namespace App;
use Illuminate'Database'Eloquent'Model;
class User extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
public function posts()
{
return $this->hasMany('App'Posts');
}
}
为了做这样的事情:
$userWithDestinations = User::where('condition')->with(['posts'])->where('moreConditions')->get();
使用热切加载的最后一段代码将生成如下JSON数据:
[
{
userID:1,
name: Luis,
posts:
[
{
postID: 1,
travelDestination: 'Mexico'
},
{
postID: 11,
travelDestination: 'France'
},
]
},
{
userID:13,
name: John,
posts:
[
{
postID: 14,
travelDestination: 'Germany'
},
{
postID: 55,
travelDestination: 'Brazil'
},
]
}
]
由于你的USER-POSTS是一个1-N关系,在Angular中,如果你只想得到每个用户的第一个帖子,你可以做下面的代码。
{{tourist.username}} : {{tourist.posts[0].travelDestination}}
高级模型文档
https://laravel.com/docs/5.1/eloquent-relationships
注意
您的问题摘要实际上并不是关于"创建API",而是关于Laravel-Model的疑问。