How to see table fields from foreign keys in Larav

2020-05-06 10:24发布

I'm new at Laravel and not good with syntax. I want to see the values of another table through the foreign key(id of that table).

https://ibb.co/pXRFRHn You can see in this picture I get ids under user & class. I want the titles associated with these ids.

I have tables sections,users and a class. I use class_id & user_id as the foreign key in the section table. When I try to show data, I see the id, but I want the name & other fields extracted from that id.

Controller

public function index()
{
    $sections = Section::all();
    $classs = Classs::all();
    $users = User::all();

    return view('sections.index')->with('sections', $sections)->with('classs', $classs)->with('users', $users);
}

Blade/View

<div class="box">
    <div class="box-header">
        <h3 class="box-title">All Sections</h3>
    </div>
    <div class="box-body">
        <table class="table table-responsive">
            <thead>
            <tr>
                <th>Name</th>
                <th>Class</th>
                <th>User</th>
                <th>Modify</th>
            </tr>
            </thead>
            <tbody>
            @foreach($sections as $cat)
                <tr>
                    <td>{{$cat->title}}</td>
                    <td>{{$cat->class_id}}</td>
                    <td>{{$cat->user_id}}</td>
                    <td>
                        <button class="btn btn-info" data-mytitle="{{$cat->title}}"
                                data-myclassid="{{$cat->class_id}}" data-myuserid="{{$cat->user_id}}"
                                data-catid={{$cat->id}} data-toggle="modal" data-target="#edit">Edit
                        </button>
                        <button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal"
                                data-target="#delete">Delete
                        </button>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>

Specifically...

<td>{{$cat->class_id}}</td>

From this, I get the class id, but I want its name also.

{{$cat->(class_id)->name}}"

However, it didn't work.

I have edited Models

 class Classs extends Model
{
    //
    protected $fillable = [
        'id',
        'title',

    ];

    public function section()
    {
        return $this->belongsTo('App\Section');

    }
}

Section Model

class Section extends Model
{
    //
    protected $fillable = [
        'id',
        'title',
        'class_id',
        'user_id',

    ];


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

}

3条回答
Root(大扎)
2楼-- · 2020-05-06 10:44

All you have to do is

data-myclassid="{{$cat->class_id}}"

rather than this try this

data-myclassid="{{ \Illuminate\Support\Facades\DB::table('class')->where('id',$cat->class_id)->value('name')}}"

I am assuming that your table name is class as you want to show class name so in that database class name (column name) would be simple 'name' , that why I put name in value field....

查看更多
爷、活的狠高调
3楼-- · 2020-05-06 10:48

You've to declare your relationships at all. In your User Model,

public function class()
{
    return $this->hasOne('App\Class');
}

and Class Model,

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

So after that you can call it like that;

public function index()
{

    $sections = Section::all();
    $classs = Classs::with('user')->get();
    $users = User::with('class')->get();


    return view('sections.index')
        ->with('sections', $sections)
        ->with('classs', $classs)
        ->with('users',$users);
}

Or you can call them when you're fetching in view just like that;

@foreach($users->class()->get() as $class )
    {{ $class->name}}
@endforeach

@foreach($classes->users()->get() as $user )
    {{ $user->name}}
@endforeach

they called 'Eloquent Relationships' Laravel's Relationships

just make sure you were read them because documentation is too much clear to understand something in record times.

hasOne and hasMany are just just example. Which relation type works for you can change.

查看更多
淡お忘
4楼-- · 2020-05-06 11:08

It is pretty easy to do exactly what you want. Laravel is great at creating relations.

You'll need to have the class model set up as a relation on your user model. See the docs on how to do that

Should be something close to this in the User Model:

public function class(){
        return $this->belongsTo(\App\Class::class);
}

Then, when you pull the users from the database in the controller, you can include the relationship directly:

$users= \App\User::with('class')->get();

Now, you have the related class model within your collection for each of your users, you can pull the name directly as you wish within your blade view:

@foreach($users as $user)
   {{$user->class->name}}
@endforeach

I suggest testing with this simple dump above first, as this will work. Then, you can try attaching the different models like the categories afterward.

By the way - using Class as a class name is probably going to cause you issues. Suggest a slight change there.

HTH

查看更多
登录 后发表回答