I have an installation of Laravel 5.1 and I want to share the route name with all my views. I need this for my navigation so I can highlight the corresponding navigation menu button depending on which page the user is on.
I have this code in my app\Providers\AppServiceProvider:
public function boot()
{
$path = Route::getCurrentRoute()->getName();
view()->share('current_route_name', $path);
}
and I am using this namespace:
use Illuminate\Support\Facades\Route;
but I am getting this error in my view:
Call to a member function getName() on a non-object
the interesting part is that if I write this in view it works with no problems at all:
{{ Route::getCurrentRoute()->getName() }}
Could anyone help me? am I not using the correct namespace or maybe it is not even possible to use Route at this point in the application?
Thank you!
you can use view share under view composer.
view()->composer('*', function($view)
{
$view->with('current_route_name',Route::getCurrentRoute()->getName());
});
Or
view()->composer('*', function($view)
{
view()->share('current_route_name',Route::getCurrentRoute()->getName());
})
Related
I have a page, where when user pick a date, choose data from dropdown, and click search.
The page which initialy have 0 data in its table, will be populated with data.
The query to fetch the data is inside a controller class.
I have successfully ran the page before. But I didn't use any authentication (auth) feature.
And now, when I use laravel breeze as the authentication starter. I got an error because the program cannot found my controller class.
Here's the previous route (web.php) code, without auth class.
use App\Http\Controllers\GetEmployeePerformance;
Route::get('/performance', [GetEmployeePerformance::class, 'index']);
require __DIR__.'/auth.php';
Here's the current route (web.php) code
use App\Http\Controllers\GetEmployeePerformance;
Route::get('/performance', function () {
return view('performance', [GetEmployeePerformance::class, 'index']);
})->middleware(['auth', 'verified'])->name('performance');
require __DIR__.'/auth.php';
And here's the controller's code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class GetEmployeePerformance extends Controller
{
public function index()
{
$performance_summary = [];
$performance_detail = [];
if (request('date')){ ...}
return view('performance', compact('performance_summary','performance_detail'));
And this is how the value is called in view
<tbody>
#for ($x = 0; $x < count($performance_summary); $x++)
<tr>
...
This is error, the route failed to find the controller class.
Which parts should I fix?
I just found the fix on my own.
I changed the route code, into this code below
Route::get('/performance', [GetEmployeePerformance::class,'index'])->middleware(['auth', 'verified'])->name('performance');
I hope this answer can help other people who have similar problem as me :D
This error is comeup while i working on laravel 8
Route [product.details] not defined. (View: C:\xampp\htdocs\ecommerce\resources\views\livewire\shop-component.blade.php)
here is the route i made Route Page web.php
Route::get('/product/{slug}',DetailsComponent::class)->name('product.details');
this is line of view where i want that route View Page shop-component.balde.php
<a href="{{route('product.details',['slug'=>$product->slug]) }}" title="{{$product->name}}">
and this one is the Details Component code
namespace App\Http\Livewire;
use Livewire\Component;
class DetailsComponent extends Component
{
public $slug;
Public function mount($slug){
$this->slug = $slug;
}
public function render()
{
$product = Product::where('slug', $this->slug)->first();
return view('livewire.details-component',['product'=>$product])->layout('layouts.base');
}
}
I think this php code doesnot get route of product.details at the same time i create middleware file and define a different route in it this route is working in this page but not Product.details.
Kindly help me i am stuck from 1 week here
You can use
php artisan route:list
to show a list of registered routes - make sure it shows up there. If it doesn't, use
php artisan route:clear
to clear Laravel's route cache
It does not work because of your route. It's not write in the correct way. You have first to make your DetailsComponent::class is bracket then you have to give the name of the method you want to use in this class.
Route::get('/product/{slug}', [DetailsComponent::class, 'method name'])->name('product.details');
I am using laravel 5.4 and trying to get index page, i am using following routes
Route::get('/',
['as' => 'home_page',
'uses' => 'Controller#index']);
and index function in controller looks like this:
public function index()
{
return view('index');
}
But when I visit mydomain.com, I get a different view than index.blade.php.
and it is fine when I use mydomain.com/? or on my local server.
I have searched everywhere in my code and in a google, but didn't found anything, any help?
ie: let me know if any further information required.
First make sure you are calling the right controller, and this dont have a specific middleware blocking the acess to your index method and index.blade.php is inside view folder.
If all of this is fine try this code on your rotes file:
Route::get('', function () {
return view('index');
})
Try this.
First use the make:controller Artisan command to create a controller file. let's say it is homeController.
php artisan make:controller homeController
Then in the homeController file write your code to get the view.
<?php
namespace App\Http\Controllers;
class homeController extends Controller
{
public function index()
{
return view('index');
}
}
Then define a route to this controller.
Route::get('/', 'homeController#index');
For more information please refer https://laravel.com/docs/5.5/controllers
There was a cached view saved on my server, I used
php artisan cache:clear and it got fixed. Thank you everyone for the support.
Here is my situation. I have a layout.blade.php which most of my pages use. Within this file, I have some partial pieces that I include, like #include('partials.header'). I am trying to use a controller to send data to my header.blade.php file, but I'm confused as to exactly how this will work since it is included in every view that extends layout.blade.php.
What I am trying to do is retrieve a record in my database of any Game that has a date of today's date, if it exists, and display the details using blade within the header.
How can I make this work?
I think to define those Game as globally shared is way to go.
In your AppServiceProvider boot method
public function boot()
{
view()->composer('partials.header', function ($view) {
view()->share('todayGames', \App\Game::whereDay('created_at', date('d')->get());
});
// or event view()->composer('*', Closure) to share $todayGames accross whole blade
}
Render your blade as usual, partial.header blade
#foreach ($todayGames as $game)
// dostuffs
#endforeach
In Laravel you can create a service class method that acts like a controller and use #inject directive to access this in your partial view. This means you do not need to create global variables in boot(), or pass variables into every controller, or pass through the base view layout.blade.php.
resources/views/header.blade.php:
#inject('gamesToday', 'App\Services\GamesTodayService')
#foreach ($gamesToday->getTodayGames() as $game)
// display game details
#endforeach
While it's different value you retrieved belong of the game chosen, you can do something like that:
Controller
$data = Game::select('id', 'name', 'published_date')->first();
return view('game')->with(compact('data'));
layout.blade.php
<html><head></head><body>
{{ $date }}
</body></html>
game.blade.php
#extend('layout')
#section('date', $data->date)
#section('content')
#endsection
The better solution would be this
Under your app folder make a class named yourClassNameFacade. Your class would look like this.
class yourClassNameFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'keyNameYouDecide';
}
}
Then go to the file app/Providers/AppServiceProvider.php and add to the register function
public function register()
{
$this->app->bind('keyNameYouDecide', function (){
//below your logic, in my case a call to the eloquent database model to retrieve all items.
//but you can return whatever you want and its available in your whole application.
return \App\MyEloquentClassName::all();
});
}
Then in your view or any other place you want it in your application you do this to reference it.
view is the following code:
{{ resolve('keyNameYouDecide') }}
if you want to check what is in it do this:
{{ ddd(resolve('keyNameYouDecide')) }}
anywhere else in your code you can just do:
resolve('keyNameYouDecide'))
I have a partial view in master layout which is the navigation bar. I have a variable $userApps. This variable checks if the user has enabled apps (true), if enabled then I would like to display the link to the app in the navigation bar.
homepage extends master.layout which includes partials.navbar
My code in the navbar.blade.php is this:
#if ($userApps)
// display link
#endif
However I get an undefined variable error. If I use this in a normal view with a controller it works fine after I declare the variable and route the controller to the view. I dont think I can put a controller to a layout since I cant route a controller to a partial view, so how do I elegantly do this?
What version of Laravel you use? Should be something like this for your case:
#include('partials.navbar', ['userApps' => $userApps])
Just for a test purpose, I did it locally, and it works:
routes.php
Route::get('/', function () {
// passing variable to view
return view('welcome')->with(
['fooVar' => 'bar']
);
});
resources/views/welcome.blade.php
// extanding layout
#extends('layouts.default')
resources/views/layouts/default.blade.php
// including partial and passing variable
#include('partials.navbar', ['fooVar' => $fooVar])
resources/views/partials/navbar.blade.php
// working with variable
#if ($fooVar == 'bar')
<h1>Navbar</h1>
#endif
So the problem must be in something else. Check your paths and variable names.
The other answers did not work for me, or seem to only work for older versions. For newer versions such as Laravel 7.x, the syntax is as follows.
In the parent view:
#include('partial.sub_view', ['var1' => 'this is the value'])
In the sub view:
{{ $var1 }}
I have gone through all the answers but below is the best way to do because you can also run queries in serviceProvider.
You need to create a separate service or you can use AppServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
View::composer('layouts.admin-layout', function ($view) {
$view->with('name', 'John Doe');
});
}
}
In your layout
{{$name}}
This approach is very simple:
In parent view :
#include('partial.sub_view1', ['This is value1' => $var1])
In sub view :
{{ $var1 }}
You can use view composer to send your variable to partial view.
Check the laravel documentation on laravel.com about view composer.
Also you can check the following link that will help you resolve this problem.
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers