Php Larvel 4:如何检索一对多关系中的数据?

Php Larvel 4:如何检索一对多关系中的数据?,php,laravel,laravel-4,eloquent,one-to-many,Php,Laravel,Laravel 4,Eloquent,One To Many,(见下面的代码) 我有两个模型和用户信息。我想根据每个输入的对话id,获取10条带有相关发件人姓名的消息,并反转数据。我已经成功地获取了这些消息,但无法将其缩小到每个会话/包含相关发件人名称的10条消息 我已尝试使用用户数据返回消息数据,但我不知道如何访问发件人的名称,或者它不返回关联的用户: $allMessages = Messages::with('User')->whereIn('conv_id',$conv_id); 我也尝试过返回10条消息,但它会根据收到的所有消息而不是每

(见下面的代码)

我有两个模型和用户信息。我想根据每个输入的对话id,获取10条带有相关发件人姓名的消息,并反转数据。我已经成功地获取了这些消息,但无法将其缩小到每个会话/包含相关发件人名称的10条消息

  • 我已尝试使用用户数据返回消息数据,但我不知道如何访问发件人的名称,或者它不返回关联的用户:

    $allMessages = Messages::with('User')->whereIn('conv_id',$conv_id);
    
  • 我也尝试过返回10条消息,但它会根据收到的所有消息而不是每次对话返回10条消息

    $allMessages = Messages::whereIn('conv_id',$conv_id)->take(10);
    
  • 如何根据每个对话的id以及邮件的关联发件人名称获取10封邮件?代码改进的额外积分。提前谢谢你


    用户模型

    public function messages(){
        return $this->hasMany('Messages');
    }
    
    public function user(){
        return $this->belongsTo('User');
    }
    
    消息模型

    public function messages(){
        return $this->hasMany('Messages');
    }
    
    public function user(){
        return $this->belongsTo('User');
    }
    
    控制器

    public function index()
        {
            $timestamps = Input::get('from'); //timestamp of latest message
            $conv_id = Input::get('conv_id'); //array of conversation ids
            $allMessages = Messages::whereIn('conv_id',$conv_id);
            if(is_null($timestamps)){
               $messages = $allMessages->orderBy('created_at','desc');
            }else{
               asort($timestamps);
               $messages = $allMessages->where('created_at','>',end($timestamps));
            }
            return $messages->get()->reverse();
        }
    

    迁移

    消息表

    Schema::create('messages', function(Blueprint $table)
            {
                $table->increments('id');
                $table->integer('user_id');
                $table->integer('conv_id');
                $table->text('body');
                $table->timestamps();
            });
    
    Schema::create('users',function($table)
            {
                $table->increments('id');
                $table->string('email')->unique();
                $table->string('password',100);
                $table->string('name',150);
                $table->string('usertype',50);
                $table->boolean('block');
                $table->string('remember_token',100);
                $table->timestamp('lastlogin_at');
                $table->timestamps();
                $table->softDeletes();
            });
    
    0: {id:37, user_id:1, conv_id:2, body:damn mate, created_at:2014-06-20 00:55:32,…}
    1: {id:38, user_id:1, conv_id:2, body:hello, created_at:2014-06-20 02:18:21,…}
    2: {id:39, user_id:1, conv_id:2, body:dude, created_at:2014-06-20 02:20:10,…}
    3: {id:40, user_id:1, conv_id:2, body:test, created_at:2014-06-20 06:52:37,…}
    4: {id:67, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 01:25:56,…}
    5: {id:68, user_id:1, conv_id:2, body:hey, created_at:2014-06-21 01:26:14, updated_at:2014-06-21 01:26:14,…}
    6: {id:69, user_id:1, conv_id:1, body:testa, created_at:2014-06-21 01:27:02,…}
    7: {id:70, user_id:1, conv_id:1, body:testser, created_at:2014-06-21 01:27:32,…}
    8: {id:115, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 02:45:06,…}
    9: {id:116, user_id:1, conv_id:2, body:test, created_at:2014-06-21 02:57:03,…}
    
    用户表

    Schema::create('messages', function(Blueprint $table)
            {
                $table->increments('id');
                $table->integer('user_id');
                $table->integer('conv_id');
                $table->text('body');
                $table->timestamps();
            });
    
    Schema::create('users',function($table)
            {
                $table->increments('id');
                $table->string('email')->unique();
                $table->string('password',100);
                $table->string('name',150);
                $table->string('usertype',50);
                $table->boolean('block');
                $table->string('remember_token',100);
                $table->timestamp('lastlogin_at');
                $table->timestamps();
                $table->softDeletes();
            });
    
    0: {id:37, user_id:1, conv_id:2, body:damn mate, created_at:2014-06-20 00:55:32,…}
    1: {id:38, user_id:1, conv_id:2, body:hello, created_at:2014-06-20 02:18:21,…}
    2: {id:39, user_id:1, conv_id:2, body:dude, created_at:2014-06-20 02:20:10,…}
    3: {id:40, user_id:1, conv_id:2, body:test, created_at:2014-06-20 06:52:37,…}
    4: {id:67, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 01:25:56,…}
    5: {id:68, user_id:1, conv_id:2, body:hey, created_at:2014-06-21 01:26:14, updated_at:2014-06-21 01:26:14,…}
    6: {id:69, user_id:1, conv_id:1, body:testa, created_at:2014-06-21 01:27:02,…}
    7: {id:70, user_id:1, conv_id:1, body:testser, created_at:2014-06-21 01:27:32,…}
    8: {id:115, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 02:45:06,…}
    9: {id:116, user_id:1, conv_id:2, body:test, created_at:2014-06-21 02:57:03,…}
    

    编辑:在chrome浏览器中查看狼人答案的预览结果

    Schema::create('messages', function(Blueprint $table)
            {
                $table->increments('id');
                $table->integer('user_id');
                $table->integer('conv_id');
                $table->text('body');
                $table->timestamps();
            });
    
    Schema::create('users',function($table)
            {
                $table->increments('id');
                $table->string('email')->unique();
                $table->string('password',100);
                $table->string('name',150);
                $table->string('usertype',50);
                $table->boolean('block');
                $table->string('remember_token',100);
                $table->timestamp('lastlogin_at');
                $table->timestamps();
                $table->softDeletes();
            });
    
    0: {id:37, user_id:1, conv_id:2, body:damn mate, created_at:2014-06-20 00:55:32,…}
    1: {id:38, user_id:1, conv_id:2, body:hello, created_at:2014-06-20 02:18:21,…}
    2: {id:39, user_id:1, conv_id:2, body:dude, created_at:2014-06-20 02:20:10,…}
    3: {id:40, user_id:1, conv_id:2, body:test, created_at:2014-06-20 06:52:37,…}
    4: {id:67, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 01:25:56,…}
    5: {id:68, user_id:1, conv_id:2, body:hey, created_at:2014-06-21 01:26:14, updated_at:2014-06-21 01:26:14,…}
    6: {id:69, user_id:1, conv_id:1, body:testa, created_at:2014-06-21 01:27:02,…}
    7: {id:70, user_id:1, conv_id:1, body:testser, created_at:2014-06-21 01:27:32,…}
    8: {id:115, user_id:1, conv_id:2, body:haha, created_at:2014-06-21 02:45:06,…}
    9: {id:116, user_id:1, conv_id:2, body:test, created_at:2014-06-21 02:57:03,…}
    
    你可以试试这个:

    $allMessages = Messages::with('user')
                           ->where('conv_id', $conv_id)
                           ->take(10)
                           ->get();
    
    这将返回
    消息的集合
    模型,因此您需要像这样循环:

    @foreach($allMessages as $message)
        {{ $message->user->name }}
        {{ $message->body }}
    @endforeach
    
    此外,您还可以使用:

    // Get the first message and then user->name
    $allMessages->first()->user->name;
    
    // Get the second message and then user->name
    $allMessages->get(1)->user->name;
    
    //dd(json_编码($heartsPurReport))

    //在视图中,使用foreach循环并在foreach循环外部使用以下分页

                           {!! $consumption->render() !!}
    

    我如何访问用户名?我似乎在var_dump对象中找不到它。
    $allMessages->first()->user->name
    谢谢!顺便说一句,我注意到你使用where()而不是where()。这是打字错误还是我真的应该用where?顺便说一句,我的conv_id是一个会话id数组。您应该使用
    where
    。如果
    $conv_id
    是多个
    conv_id
    的数组,那么您应该使用
    where
    ,否则使用
    where
    ,欢迎:-)