duanmen8491 2016-03-22 06:35
浏览 26
已采纳

空数据从多对多关系回归5

I've two model called application and user.

Application

id | name

user

id | first name

application_user

id | application_id | user_id | comment| rating | like

Here is the model classes

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\ModelValidator;

class Application extends ModelValidator
{
    //
    protected $table = "application";

    public function user()
    {
        return $this->belongsToMany('App\User','application_user','application_id','user_id')->withPivot(['rating','like','comment'])->withTimestamps();
    }

    public function getComment()
    {
        $comments = array();

        foreach ($this->user as $user) {
            $comments[] = $user->comment;
        }

        return compact($comments);
    }
}

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;

class User extends ModelValidator implements AuthenticatableContract,CanResetPasswordContract
{
    use Authenticatable,CanResetPassword;

    protected $table = 'user';

    public function application()
    {
        return $this->belongsToMany('App\Application')->withPivot(['rating','like','comment'])->withTimestamps();
    }
}

But when i run the below code it prints empty array.

$apps = \App\Application::all();

        foreach ($apps as $app) {
            print_r($app->getComment());
        }

Why its returning empty array ?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • doutuan6158 2016-03-22 06:50
    关注

    just change your relationship like

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use App\ModelValidator;
    
    class Application extends ModelValidator
    {
        //
        protected $table = "application";
    
        public function user()
        {
            return $this->hasMany('App\ApplicationUser','application_id','id')
                   ->withPivot(['rating','like','comment'])->withTimestamps();
        }
    
    }
    
    <?php
    
    namespace App;
    
    use Illuminate\Auth\Authenticatable;
    use Illuminate\Auth\Passwords\CanResetPassword;
    use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
    use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
    use Illuminate\Database\Eloquent\Model;
    
    class User extends ModelValidator implements AuthenticatableContract,CanResetPasswordContract
    {
        use Authenticatable,CanResetPassword;
    
        protected $table = 'user';
    
        public function application()
        {
            return $this->belongsToMany('App\Application')->withPivot(['rating','like','comment'])->withTimestamps();
        }
    }
    

    change your code like this.

    foreach ($apps as $app) {
        print_r($app->user);
    }
    

    Now you get data from application_user table, you got empty result because of you trying to find a data into user table so for that you got empty result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题