Php 如何从数据库中的两个表(用户和帖子)为ionic创建api

Php 如何从数据库中的两个表(用户和帖子)为ionic创建api,php,laravel-4,ionic-framework,single-page-application,hybrid-mobile-app,Php,Laravel 4,Ionic Framework,Single Page Application,Hybrid Mobile App,我想制作一个api(我使用Laravel4.2作为后端)来使用ionic应用程序。 例如: //我赚了以下5美元: $tourists = User::where('block','0') ->where('guider', 1) ->where('location', $location)

我想制作一个api(我使用Laravel4.2作为后端)来使用ionic应用程序。 例如:

//我赚了以下5美元:

                    $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);
//在html中使用

<div>{{tourists.username}}: {{tourists.intro}}</div>
//员额表

            $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');
    }

有很多方法可以实现这一点

您可以使用连接

$userWithDestinations = User::where('condition')->join('destination_table', function ($join)
{
   $join->on('user.userID', '=', 'post_table.userID')
})->where('moreConditions')->get()
您可以使用Laravel-Eager-load(我强烈建议这种方法用于可重用目的)

您的用户模型可能如下所示

<?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');
    }       
}
最后一段代码使用渴望加载将生成如下JSON数据:

$userWithDestinations = User::where('condition')->with(['posts'])->where('moreConditions')->get();
[
    {
        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'
            },              
        ]
    }       
]
由于您的用户-帖子是1-N关系,因此,如果您只想获得每个用户的第一篇帖子,您可以执行类似于以下代码的操作

{{tourist.username}} : {{tourist.posts[0].travelDestination}}
大级别模型文档

$userWithDestinations = User::where('condition')->join('destination_table', function ($join)
{
   $join->on('user.userID', '=', 'post_table.userID')
})->where('moreConditions')->get()

注意


您的问题摘要实际上并不是关于“makie an API”,更多的是对Laravel模型的怀疑。

顺便说一句,我使用的是Laravel 4.2,其中我使用了您的答案(急切加载方式),但似乎不起作用。请确保您与后期模型建立了关系。顺便说一下,我想你确实有一个“posts”表的模型,对吗?我有,我会在我的问题中展示它。等等,汉克斯。我是用你的答案做的。非常感谢您应该坚持模型和表的名称约定。您的表名为posts,其模型为'Need'。我看不到模型的名称空间。都在同一命名空间上吗?抱歉,刚刚更改了它
$userWithDestinations = User::where('condition')->with(['posts'])->where('moreConditions')->get();
[
    {
        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'
            },              
        ]
    }       
]
{{tourist.username}} : {{tourist.posts[0].travelDestination}}