从数据透视表中检索数据到多对多关系船laravel 5

时间:2018-07-18 07:33:44

标签: php laravel

我在laravel中建立了很多对很多关系,但是效果很好,但是当我想检索并显示数据时,会发生一些奇怪的事情 现在我想显示分配给登录用户的客户端,所以这是我的代码 客户模型:

public function sellmanlist(){
    return $this->belongsToMany('App\User' , 'client_user','client_id');
}

控制器和后退方法

public function myclient(Client $client){
    $user_id =Auth::user()->id;
    $client = Client::with('sellmanlist')->firstOrFail();
    return view('admin.client.myclient',compact('user_id','client'));
}

这是我已经在测试的视图

@foreach($client->sellmanlist as $sellman )
            @php(
            $sell_id = $sellman->pivot->user_id
            )
            @if($user_id === $sell_id)
                {{$client->id}}
                {{$client->title}}
            <br>
                @endif
            @endforeach

但这仅显示1次2的客户端ID,这是我的数据透视表

 public function up()
{
    Schema::create('client_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('client_id');
        $table->integer('user_id');
        $table->timestamps();
    });
}

1 个答案:

答案 0 :(得分:1)

只需在用户模型中为struct Busy_list{ int client_socket; char JOB[1024]; int characters; int No_jobs; int IsReplace; int Priority; }; char** PriorityEvaluator(int client_socket){ struct Busy_list Array; FILE *Ufptr; Ufptr = fopen("Unsorted_busy_list.txt","r+"); for(int i;!EOF;i++){ fscanf(Ufptr, "%d\t%s\t%d\t%d\t%d\n",&Array.client_socket,&Array.JOB,&Array.characters,&Array.No_jobs,&Array.IsReplace); } fclose(Ufptr); } 添加反向/* 4 double fish 4 1 0 5 double praise 6 2 0 5 replace peter 2-o,4-o 5 2 1 */

用户模型

belongsToMany

现在为在控制器中登录的用户获取客户端

Client

在视图中显示

public function clients(){
    return $this->belongsToMany('App\Client','client_user');
}
相关问题