Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

How can i display a table with two foreign keys in laravel

I have 3 tables

Users
-------------
user_id  (PK)
user_name

Items_distributed
-------------
user_id (FK) 
item_id(FK)

Item List
-----------------
item_id(PK)
item_name

Now i need to print Items_distributed table by taking both the user_id and item_id and dispaly their item_name and user_name

i dont know how to display both things at a time if i display either item_name or user_name its working but when i try to print both it is not working . Can any one solve my problem .

This is how i print the values

in controller i use like this $itemdata=item::orderBy('user_id','desc')->paginate(10); and in view i use

  @foreach($itemdata as $value)  
                    <tr>  
                        <td>{{$value->items->item_name}}</td>  
                        <td>{{$value->users->user_name}}</td>  
                        <td>{!!Html::link("editItem/",'Edit',array('class'=>'btn-sm  btn-warning margin-left-btn'))!!}
                            {!!Html::link("deleteItem/".$value->item_id,'Delete',array('class'=>'btn-sm btn-warning margin-left-btn'))!!}</td>
                    </tr>
  @endforeach
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use Laravel Eloquent Relationship. When you create model file for DB Table, You just put relation with other model.

If Relation has set, than Laravel it self fetch data from Related table.

In your case, Three models are created.

1) User Model.

2) Item Model.

3) ItemDestributed Model.

UserModel.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class User extends Model
{
    /**
     * Other code goes here
    **/
    public function itemDestribution()
    {
        return $this->hasMany('AppItemDistribution','foreign_key');
    }
}

ItemModel.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Items extends Model
{
    /**
     * Other code goes here
    **/
    public function itemDestribution()
    {
        return $this->hasMany('AppItemDistribution','foreign_key');
    }
}

ItemDestributionModel.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class ItemDestribution extends Model
{
    /**
     * Other code goes here
    **/
    public function user()
    {
        return $this->belongsTo('AppUser','foreign_key_of_user');
    }

    public function item()
    {
        return $this->belongsTo('AppItem','foreign_key_of_item');
    }
}

You can find more about Eloquent Relation from Here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...