Search code examples
databasemodel-view-controllereloquentquery-builderlaravel-9

How to send multiple table data in a single view without joining the table in laravel 9?


I'm new to laravel and I'm trying to build a landing page. In my database, I've multiple tables like:

  • hero_section
  • portfolio
  • about
  • skill..

So, there will be multiple sections on my landing page, and in order to do that, I need to send these tables data separately without joining them and pass it into a single view.

This is my route web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\DB;


Route::get('/', [HomeController::class, 'show']);

My controller HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\HeroSectionModel;
use App\Models\AboutModel;

class HomeController extends Controller
{
    public function show(){

        // $hero_section = HeroSectionModel::select("*")->where('ID', '=', 1)->get();
        $hero_section = HeroSectionModel::all();
        $about = AboutModel::all();

        // return view('home',compact('hero_section'));
        return View('home')
        ->with('hero_section',$hero_section)
        ->with('about',$about);
    }
}

And in my home.blade.php I'm trying to access data like this:

<section>
  <div class="row">
    <p>{{ $hero_section->NAME }}</p>
    <p>{{ $hero_section->CAREER_OBJECTIVE }}</p>
  </div>
</section>

<section>
  <div class="row">
    <p>{{ $about->DESCRIPTION }}</p>
    <p>{{ $hero_section->DESCRIPTION1 }}</p>
  </div>
</section>

In this way, I'm unable to fetch any data. Can anyone help me with this?


Solution

  • in controller:

     $res["hero_section"]=$hero_section;
     $res["about"]=$about;
     return view('home',$res);
    

    in blade file:

     @foreach ($hero_section as $hero)
      <div class="row">
          <p>{{ $hero->NAME }}</p>
          <p>{{ $hero->CAREER_OBJECTIVE }}</p>
     </div>
     @endforeach
     @foreach ($about as $a)
      <div class="row">
          <p>{{ $a->DESCRIPTION}}</p>
          <p>{{ $a->DESCRIPTION1}}</p>
     </div>
     @endforeach