如何从数据库中的两个表(用户和帖子)为 ionic 创建 api
how to make an api for ionic from two tables(users and posts) in database
我想制作一个 api(我使用 laravel 4.2 作为后端)以使用 ionic 应用程序。
例如:
//我制作了以下$tourists:
$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>
//以上为一个table
但是如果我有两个 table 怎么办,例如,用户 table 和帖子 table
//用户table(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
$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');
}
}
我们可以看到用户 table 和帖子 table 通过 user_id 相互链接,
所以我的问题是如何将两个table的内容合并输出,就像上面一样,只有一个table输出(很容易)?
我想要实现的就像下面的代码:
<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预加载(我强烈推荐这种方法用于可重用的目的)
您的用户模型可能如下所示
<?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 中,如果您只想获得每个用户的第一个 post,您可以执行类似以下代码的操作。
{{tourist.username}} : {{tourist.posts[0].travelDestination}}
Larevel 模型文档
https://laravel.com/docs/5.1/eloquent-relationships
注意
您的问题摘要实际上并不是关于 "makie an API",它更像是一个 Laravel-模型问题。
我想制作一个 api(我使用 laravel 4.2 作为后端)以使用 ionic 应用程序。 例如:
//我制作了以下$tourists:
$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>
//以上为一个table
但是如果我有两个 table 怎么办,例如,用户 table 和帖子 table
//用户table(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
$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');
}
}
我们可以看到用户 table 和帖子 table 通过 user_id 相互链接, 所以我的问题是如何将两个table的内容合并输出,就像上面一样,只有一个table输出(很容易)?
我想要实现的就像下面的代码:
<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预加载(我强烈推荐这种方法用于可重用的目的)
您的用户模型可能如下所示
<?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 中,如果您只想获得每个用户的第一个 post,您可以执行类似以下代码的操作。
{{tourist.username}} : {{tourist.posts[0].travelDestination}}
Larevel 模型文档
https://laravel.com/docs/5.1/eloquent-relationships
注意
您的问题摘要实际上并不是关于 "makie an API",它更像是一个 Laravel-模型问题。