A
A
andrew-corput2018-07-19 23:04:10
Laravel
andrew-corput, 2018-07-19 23:04:10

How to display category name by id value of another table?

There are tables
Cars - containing id, make_id, model_id
Makes - id, name

public function index() {
    	$cars = Car::select(['id', 'make_id', 'model_id', 'year', 'start_bid', 'odometer'])->get();
        $makes = Make::all();
    	return view('main', compact('cars', 'makes'));
}

<div class="row">
            @foreach ($cars as $article)
                <div class="col-3 car">
                    <a href="{{ route('carsShow', ['id'=>$article->id]) }}">
                        <div class="box ratio4_3">
                            <div class="box-content grey">
                                <img src="">
                            </div>
                        </div>
                        <div class="box-text">
                            <div class="box-title">{{$article->make_id}} {{$article->model_id}}</div>
                        </div>
                        <div class="box-descr w100 flex">
                            <div><span>{{ number_format($article->start_bid,0,'',' ') }} ₽</span>,&#160;</div>
                            <div >{{$article->year}},&#160;</div>
                            <div>{{ number_format($article->odometer,0,'',' ') }} км</div>
                        </div>
                    </a>
                </div>

            @endforeach
</div>

now $article->make_id is displayed as 1, you need to make it display the name of the car brand from the Makes table according to the received $article->make_id

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-07-20
@Yan-s

A whole section of the documentation is devoted to your question, describing different types of relationships in the database and how to work with them in the context of eloquent ORM: https://laravel.com/docs/eloquent-relationships

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question