Home > Software engineering >  I want make some thing like that in my web.php. But I dont know what should I write
I want make some thing like that in my web.php. But I dont know what should I write

Time:06-07

Hi can you please solve my problem? My problem is i want to access my controller in a master page

in my web.php

Route::get('/', [LayoutController::class, 'home']{

    $abouts = DB::table('home_abouts')->first();
    
    return view('home', compact('abouts'));

});

in my master.blade.php ```

<div >
  <h2>About Us</strong></h2>
</div>

<div >
  <div  data-aos="fade-right">
    <h2> {{ $abouts->title }}</h2>
    <h3>{{ $abouts->short_dis }}</h3>
  </div>
  <div  data-aos="fade-left">
    <p>
    {{ $abouts->long_dis }}
    </p>
  </div>
</div>
How can I write this correctly on web.php? I don't know what should I write now  

CodePudding user response:

The problem is that you're using two various methods:

  • Using a closure
  • Using a controller method

If you want to use a closure, rewrite it like this:

Route::get('/', function() {
    $abouts = DB::table('home_abouts')->first();
    return view('home', compact('abouts'));
});

And if you want to use controllers (recommended), do this:

Route::get('/', [LayoutController::class, 'home']);

Then implement your home() method in LayoutController.

CodePudding user response:

just to add to @WebPajooh's answer there are view routes

Route::view('/', 'home', ['abouts' => DB::table('home_abouts')->first()]);

but using closure-based routes becomes a mess really fast, so better stick with controller variant

CodePudding user response:

// inside your web.php file 
// make sure MasterController is imported
Route::get('/', [MasterController::class, 'index']);

// in your controller
public function index(){

    $abouts = DB::table('home_abouts')->first();
    return view('master', [
           'abouts' => $abouts
    ]);// thats master.blade.php 
}
  • Related