0

I have a problem retrieving data from a third table, as you can see I have three tables:

  1. Models : id , code , title
  2. levels : id , code . title
  3. models_level : model_id,level_id

I have as function :

 $model = model::paginate(10);
    $level = level::all();
    $models_level = models_level::all();
    return view('pg',compact('model','level','models_level'));

How can I retrieve the level name based on the model id retrieved?

Here is my blade

 <tbody>
            @foreach($model as $f)
                <tr class="item{{$f->id}}">
                    <td style="font-size: 13px;"> {{$f->title}}</td>
                    <td style="font-size: 13px;">{{$f->code}}</td>
                    <td style="font-size: 13px;">{{$f->models_level->level_id}}</td> 
                </tr>
            @endforeach
            {{ $filiere->links() }}

            </tbody>

I need to show the title in level instead of the ID.

in this case I have 2 different tables and the 3 rd is associative table that contains the two , so I have to pass by model to get model_level . get the level id depending on the model_id and the go to level and find the title ..

MnlLBD
  • 13
  • 5

2 Answers2

0
Models
– id

levels
– id

models_level
– product_id
– shop_id

here models_level table is called as pivot table app/Model.php

class Model extends Model
{

    public function levels()
    {
        return $this->belongsToMany('App\level','models_level');
    }
}

app/level.php

class Level extends Model
{

    public function Models()
    {
        return $this->belongsToMany('App\Shop','models_level');
    }
}

$model = model::with('levels')->paginate(10);
return view('pg',compact('model'));

<tbody>
            @foreach($model as $f)
                <tr class="item{{$f->id}}">
                    <td style="font-size: 13px;"> {{$f->title}}</td>
                    <td style="font-size: 13px;">{{$f->code}}</td>
                    <td style="font-size: 13px;">{{$f->levels[0]->id}}</td> 
                </tr>
            @endforeach
            {{ $filiere->links() }}

            </tbody>

You can read more about many to many relationships in the docs.

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
  • thank you for your efforts, in my model for model I have it like this public function levels() { return $this->belongsToMany(\App\Models\level::class) ->withPivot('mode_formation_id'); } – MnlLBD Jan 03 '20 at 10:01
  • @MnlLBD yes it is pivot table – Kamlesh Paul Jan 03 '20 at 10:03
-1

I assume you are using a One to Many relationship and you have the correct methods in you model classes.

This way you can change your controller method to:

$model = model::with('level')->paginate(10);
return view('pq', $model);

and in your blade files loop:

<td style="font-size: 13px;">{{$f->level->title}}</td>

You should read up on eloquent relationships in the laravel documentation: https://laravel.com/docs/6.x/eloquent-relationships

Niko Peltoniemi
  • 494
  • 3
  • 7
  • i have tried this but it gives me an error, they are 3 tables and the model_level is an association table – MnlLBD Jan 03 '20 at 09:39