How to see table fields from foreign keys in Laravel

后端 未结 3 802
小蘑菇
小蘑菇 2021-01-26 10:04

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

相关标签:
3条回答
  • 2021-01-26 10:35

    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

    0 讨论(0)
  • 2021-01-26 10:50

    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....

    0 讨论(0)
  • 2021-01-26 10:51

    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.

    0 讨论(0)
提交回复
热议问题