Laravel 7. How to fetch records from various tables using foreign key in laravel?

  laravel, mysql, php

Hello.
Laravel 7 and 2 tables: comp, computers.
I would like to display in the view index.blade.php the name of the computer e.g. DELL, IBM, LENOWO instead of id of this name.
What the foreach syntax should look like to retrieve the computer name from the computers table.

And when you add a new PC, what should the dropdownlist look like?

 class CreateCompTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comp', function (Blueprint $table) {
            $table->id();
            $table->integer('name_id')->unsigned();
            $table->string('number');
            $table->string('year');
            $table->timestamps();

            $table->foreign('name_id')->references('id')->on('computers')
                ->onDelete('cascade');
        });
    }

Table Computers

class CreateComputersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('computers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

CompControllers

namespace AppHttpControllers;


use AppComp;
use IlluminateHttpRequest;

class CompController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        $comp = Comp::latest()->paginate(5);

        return view('comp.index',compact('comp'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create()
    {
        return view('comp.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {
        $request->validate([
            'name_id' => 'required',
            'number' => 'required',
            'year' => 'required',

        ]);

        comp::create($request->all());

        return redirect()->route('comp.index')
            ->with('success','xxx.');
    }

    /**
     * Display the specified resource.
     *
     * @param  AppComp  $comp
     * @return IlluminateHttpResponse
     */
    public function show(Comp $comp)
    {

        return view('comp.show',compact('comp'));
    }

  
   

View index.blade.php

@extends('comp.layout')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Comp</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('comp.create') }}"> Add comp</a>
            </div>
        </div>
    </div>

    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif

    <table class="table table-bordered">
        <tr>
            <th>L.p</th>
           
            <th>Name comp</th>
            <th>Number comp</th>
            <th>Year</th>
            <th width="250px">Acction</th>
        </tr>
        @foreach ($comp as $comp)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $comp->name_id }}</td>

            <td>{{ $comp->number }}</td>

            <td>{{ $comp->year }}</td>

            <td>
                <form action="{{ route('comp.destroy',$comp->id) }}" method="POST">

                    <a class="btn btn-info" href="{{ route('comp.show',$comp->id) }}">View</a>

                    <a class="btn btn-primary" href="{{ route('comp.edit',$comp->id) }}">Edit</a>

                    @csrf
                    @method('DELETE')

                    <button type="submit" class="btn btn-danger">Delete</button>


                </form>
            </td>
        </tr>
        @endforeach
    </table>



@endsection

Source: Ask PHP

LEAVE A COMMENT