Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从多对多雄辩的关系中检索数据_Php_Laravel_Laravel 5.2 - Fatal编程技术网

Php 如何从多对多雄辩的关系中检索数据

Php 如何从多对多雄辩的关系中检索数据,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,我有两张桌子 1.课程 2.用户 用户可以从课程列表中选择课程,并使用其实体保存用户信息。现在管理员可以检查用户详细信息。因此,有一个用户详细信息页面在那里我想显示用户详细信息。但事实并非如此 以下是用户模型: <?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { protected $table='us

我有两张桌子 1.课程 2.用户

用户可以从课程列表中选择课程,并使用其实体保存用户信息。现在管理员可以检查用户详细信息。因此,有一个用户详细信息页面在那里我想显示用户详细信息。但事实并非如此

以下是用户模型:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $table='users';
     protected $fillable=['name','email','phone','course_id'];
    public function roles()
    {
        return $this->belongsToMany('App\Role', 'user_role', 'user_id', 'role_id');
    }

    public function hasAnyRole($roles)
    {
        if (is_array($roles)) {
            foreach ($roles as $role) {
                if ($this->hasRole($role)) {
                    return true;
                }
            }
        } else {
            if ($this->hasRole($roles)) {
                return true;
            }
        }
        return false;
    }

    public function hasRole($role)
    {
        if ($this->roles()->where('name', $role)->first()) {
            return true;
        }
        return false;
    }

    public function course()
    {
        return $this->hasMany('App\Course');
    }
}

在控制器中获取用户数据:

<?php

namespace App\Http\Controllers;
use App\User;
use App\Role;
use App\Fee;
use App\Course;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

class AppController extends Controller
{

    public function getStudentDetails(){
        // $user = User::all();
        $user = \DB::table('users')->where('course_id','!=',0) ->get(); 
         return view('studentDetails', compact('user'));
    }
}
$user=User::with('courses')->first();
在视图中,检查用户在tr之前是否有课程,在tbody中使用此条件

<tbody>
@foreach($user as $row)
@if($row->courses)<tr><td></td></tr>
@endif
@endforeach
</tbody>

获取控制器中的用户数据:

<?php

namespace App\Http\Controllers;
use App\User;
use App\Role;
use App\Fee;
use App\Course;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

class AppController extends Controller
{

    public function getStudentDetails(){
        // $user = User::all();
        $user = \DB::table('users')->where('course_id','!=',0) ->get(); 
         return view('studentDetails', compact('user'));
    }
}
$user=User::with('courses')->first();
在视图中,检查用户在tr之前是否有课程,在tbody中使用此条件

<tbody>
@foreach($user as $row)
@if($row->courses)<tr><td></td></tr>
@endif
@endforeach
</tbody>

我没有得到你想要显示的用户。“谁从用户详细信息页面的列表中选择了课程”您是什么意思?在->get()/之前您有一点空间,但我不知道您要做什么,而是尝试$user=user::where('course_id',1)->get();用户已经从课程表中选择了课程列表,并完成了注册。现在,我想向该用户显示其他带有课程名称的页面的详细信息,并显示我还需要检查课程表。因为在用户表中,我只保存了课程的id而不是名称@Erick@AchrafKhouadja它会只检查课程_id=1..对吗?现在很清楚了,答案是shuvrow!下次尝试更好地解释,您将更快、更轻松地获得帮助:)我没有得到您想要显示的用户。“谁从用户详细信息页面的列表中选择了课程”您是什么意思?在->get()/之前您有一点空间,但我不知道您要做什么,而是尝试$user=user::where('course_id',1)->get();用户已经从课程表中选择了课程列表,并完成了注册。现在,我想向该用户显示其他带有课程名称的页面的详细信息,并显示我还需要检查课程表。因为在用户表中,我只保存了课程的id而不是名称@Erick@AchrafKhouadja它会只检查课程_id=1..对吗?现在很清楚了,答案是shuvrow!下次试着更好地解释,你会更快更容易地得到帮助:)