I'm busy with my own news system and I'm using Laravel 5. I'm quite new to Laravel and using tutorials to walk my way through it. Now I'm having this error "Undefined variable: articles".
Here is my ArticlesController.php
<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function index()
{
return view('pages.news');
}
}
Here is my news.blade.php
#foreach ($articles as $article)
<!-- Post -->
<div class="blog-item">
<!-- Post title -->
<h2 class="blog-item-title font-alt">
{{ $article->title }}
</h2>
<!-- Post data -->
<div class="blog-item-data">
<i class="fa fa-clock-o"></i> 5 December
<span class="seperator"> </span>
<i class="fa fa-user"></i> John Doe
<span class="seperator"> </span>
<i class="fa fa-folder-open"></i>
Category, Category
<span class="seperator"> </span>
<i class="fa fa-comments"></i> 5 Comments
</div>
<!-- Media gallery -->
<div class="blog-media">
<ul class="clearlist content-slider">
<li><img src="images/portfolio/full-project-1.jpg" alt="Blog image"></li>
</ul>
</div>
<!-- Text intro -->
<div class="blog-item-body">
<p>{{ $article->body }}</p>
</div>
<!-- Read more -->
<div class="blog-item-foot">
Read More <i class="fa fa-angle-right"></i>
</div>
</div>
#endforeach
I have tried couple things, such as removing everything and starting over. Nothing really did the trick.
You have to pass the variable to your view, else the view does not "know" the variable. Like this:
<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function index()
{
return view('pages.news', ['articles' => Article::all(),]);
}
}
Reference here.
You do not pass any data from the controller to the view, so do something like this:
$articles = Article::get();
return view('pages.news', ['articles' => $articles]);
Or:
return view('pages.news', compact('articles'));
Or:
return view('pages.news')->with('articles', $articles);
https://laravel.com/docs/5.5/views#passing-data-to-views
Related
Hi i am trying to paginate data from the database and i am getting the error below
"Call to undefined method App\Material::links() (View: C:\xampp\htdocs\project104\resources\views\swapview.blade.php)"
I don`t know where i am getting this wrong, i tried changing variables but still got an error
below is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Material;
class SwapViewController extends Controller
{
public function index()
{
return view('swapview');
}
public function show()
{
$inf = Material::paginate(4);
$info = Material::orderBy('created_at', 'DESC')->get();
return view('swapview',['info'=>$info]);
}
}
below is my view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
#foreach($info as $inf)
<div style="background-color:#162238; color:white;" class="card mb-3" style="max-width: 540px;">
<div class="row g-0">
<div class="col-md-4">
<img
src="{{asset('images/' . $inf->imogi)}}"
alt="Trendy Pants and Shoes"
/>
</div>
<div class="col-md-8">
<div class="card-body">
<div style="text-align: right;">
<i style="color:#3490dc" class="fa fa-building-o" style="font-size:24px"></i> <small>{{$inf->organazation}}</small> <small><i class="fa fa-calendar" style="color:#3490dc" aria-hidden="true"></i> {{ $inf->created_at->format('d/m/Y') }}</small>
</div>
<br/>
<!--LIVEWIRE online status-->
#if($inf->isOnline())
<small><div class="circle"></div></small> <small style="color:#22bb33;">operative</small>
#else
<small><div class="ocircle"></div></small> <small>inoperative</small>
#endif
<h5 class="card-title"><i class="fa fa-user-circle-o" style="color:#3490dc" aria-hidden="true"></i> {{$inf->name}}</h5>
<small style="color:#3490dc;"><i class="fa fa-clock-o" style="color:#3490dc;"></i></small> <small style="color:#3490dc;"> posted {{ \Carbon\Carbon::parse($inf->created_at)->diffForHumans() }}</small><br/>
<small><i class="fa fa-map-marker" style="color:#3490dc" aria-hidden="true"></i> {{$inf->location}}</small> | <small><i class="fa fa-phone" style="color:#3490dc" aria-hidden="true"></i>{{$inf->contact}}</small>
<hr color=3490dc>
<p class="card-text">
<small><small style="color:#3490dc;"><i class="fa fa-exchange" aria-hidden="true"></i></small> {{$inf->swap}}</small>
</p>
<hr color=3490dc>
<p class="card-text">
<small><small style="color:#3490dc;"><i class="fa fa-exchange" style="font-size:10px"></i></small> {{$inf->exchange}}</small>
</p>
</div>
</div>
</div>
<a style="color:red;" href="{{ route('report') }}"><i class="fa fa-flag" style="color:red;" aria-hidden="true"></i> <small style="color:#3490dc;">Report</small></a>
</div>
<br/>
#endforeach
{{ $inf->links() }}
</div>
</div>
</div>
#include('footer')
#endsection
I will appreciate any help, thanks in advance
In your controller, this is your show method:
public function show()
{
$inf = Material::paginate(4);
$info = Material::orderBy('created_at', 'DESC')->get();
return view('swapview',['info'=>$info]);
}
So in your return, you send the $info variable to your view, but not you $inf.
And then, in your view, you are using $info as $inf, so your $inf variable is one Material item, not the paginate Material items you get in the controller. So then, when you use $inf->links(), Laravel can't get you some pagination links... because you are not on a paginate element.
It seems there is some confusion in variable naming.
I am currently trying to display todos in my app-todo view, but I keep getting the error mentioned in the title.
In my controller I have:
public function index()
{
$todos = Todo::all();
return view('content.apps.todo.app-todo', ['todos' => $todos]);
}
and in my app-todo:
<div class="todo-task-list-wrapper list-group">
<ul class="todo-task-list media-list" id="todo-task-list">
#foreach($todos as $todo)
<li class="todo-item">
<div class="todo-title-wrapper">
<div class="todo-title-area">
<i data-feather="more-vertical" class="drag-icon"></i>
<div class="title-wrapper">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="customCheck1" />
<label class="form-check-label" for="customCheck1"></label>
</div>
<span class="todo-title">Drink more water</span>
</div>
</div>
<div class="todo-item-action">
<div class="badge-wrapper me-1">
<span class="badge rounded-pill badge-light-primary">Team</span>
</div>
<small class="text-nowrap text-muted me-1">Aug 08</small>
<div class="avatar">
<img
src="{{asset('images/portrait/small/avatar-s-4.jpg')}}"
alt="user-avatar"
height="32"
width="32"
/>
</div>
</div>
</div>
</li>
#endforeach
I have tried changing the code in the controller to return view('content.apps.todo.app-todo')->with(['todos' => $todos]); and using compact('todos'); ,but I get the same error.
Update:
The model is the following:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Todo extends Model
{
protected $fillable = ['todoTitleAdd','task_assigned','task_due-date','task_tag', 'task_desc'];
}
and the web.php route:
Route::resource('todos', TodoController::class);
public function index(){
$todos = Todo::all();
return view('content.apps.todo.app-todo',compact('todos'));
}
i want to create some CMS project with the laravel, when the user trying to click the categories, the app will show the post that only shows the category that user clicked. When the user click the categories, the app says error Bad Method Call. What should i do? Any help will be appreciated.
Here's the code from web.php
<?php
use App\Http\Controllers\Blog\PostsController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'WelcomeController#index')->name('welcome');
Route::get('blog/posts/{post}', [PostsController::class, 'show'])->name('blog.show');
Route::get('blog/categories/{category}', [PostsController::class, 'category'])->name('blog.category');
Route::get('blog/tags/{tag}', [PostsController::class, 'tag'])->name('blog.tag');
Auth::routes();
Route::middleware(['auth'])->group(function () {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('categories', 'CategoriesController');
Route::resource('posts','PostsController');
Route::resource('tags','TagsController');
Route::get('trashed-posts','PostsController#trashed')->name('trashed-posts.index');
Route::put('restore-post/{post}', 'PostsController#restore')->name('restore-posts');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('users/profile', 'UserController#edit')->name('users.edit-profile');
Route::put('users/profile', 'UserController#update')->name('users.update-profile');
Route::get('users','UserController#index')->name('users.index');
Route::post('users/{user}/make-admin', 'UserController#makeAdmin')->name('users.make-admin');
});
Here's the code from PostsController.php
<?php
namespace App\Http\Controllers\Blog;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
use App\Tag;
use App\Category;
class PostsController extends Controller
{
public function show(Post $post) {
return view('blog.show')->with('post', $post);
}
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->posts()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
public function tag(Tag $tag) {
return view('blog.tag')
->with('tag', $tag)
->with('posts', $tag->posts()->simplePaginate(3));
}
}
Here's the code from App\Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function post() {
return $this->hasMany(Post::class);
}
}
Here's the code from category.blade.php
#extends('layouts.blog')
#section('title')
Category {{ $category->name }}
#endsection
#section('header')
<header class="header text-center text-white" style="background-image: linear-gradient(-225deg, #5D9FFF 0%, #B8DCFF 48%, #6BBBFF 100%);">
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h1>{{ $category->name }}</h1>
<p class="lead-2 opacity-90 mt-6">Read and get updated on how we progress</p>
</div>
</div>
</div>
</header><!-- /.header -->
#endsection
#section('content')
<main class="main-content">
<div class="section bg-gray">
<div class="container">
<div class="row">
<div class="col-md-8 col-xl-9">
<div class="row gap-y">
#forelse($posts as $post)
<div class="col-md-6">
<div class="card border hover-shadow-6 mb-6 d-block">
<img class="card-img-top" src="{{ asset($post->image) }}" alt="Card image cap">
<div class="p-6 text-center">
<p>
<a class="small-5 text-lighter text-uppercase ls-2 fw-400" href="#"></a>
{{ $post->category->name }}
</p>
<h5 class="mb-0">
<a class="text-dark" href="{{ route('blog.show',$post->id) }}">
{{ $post->title }}
</a>
</h5>
</div>
</div>
</div>
#empty
<p class="text-center">
No Results found for query <strong>{{ request()->query('search') }} </strong>
</p>
#endforelse
</div>
{{-- <nav class="flexbox mt-30">
<a class="btn btn-white disabled"><i class="ti-arrow-left fs-9 mr-4"></i> Newer</a>
<a class="btn btn-white" href="#">Older <i class="ti-arrow-right fs-9 ml-4"></i></a>
</nav> --}}
{{ $posts->appends(['search' => request()->query('search') ])->links() }}
</div>
#include('partials.sidebar')
</div>
</div>
</div>
</main>
#endsection
If you guys didn't see which file that i don't show up, just ask,.
If you guys see any problems that confusing, i can use teamviewer.
Any Help will be appreciated.
Thank you.
The error told that you don't have posts. Indeed it is. What you have is post() method on Category class, as evidently seen in this line:
public function post() {
You should change this ->with('posts', $category->posts()->simplePaginate()) to be:
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->post()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
In your model, you defined a post relationship whereas you use a posts relationship then, they're not the same one. You should replace post by posts because it's a to-many relationship.
Your relationship is post and you have multiple ->with('posts', $category->posts()->simplePaginate()). Can you change those to ->with('post') and try again?
I am getting an error 404 from a view that actually exists in my routes.
This is the view where I select the data from (sidebar.blade.php)
<div class="col-xl-3 col-lg-3 col-sm-12 col-xs-12 sidebar-shop-left">
<div class="product-categori">
<div class="search-product">
<form action="#">
<input class="form-control" placeholder="Search here..." type="text">
<button type="submit"> <i class="fa fa-search"></i> </button>
</form>
</div>
<div class="filter-sidebar-left">
<div class="title-left">
<h3>Categories</h3>
</div>
<div class="list-group list-group-collapse list-group-sm list-group-tree" id="list-group-men" data-children=".sub-men">
#foreach($categories as $cat)
<div class="list-group-collapse sub-men">
<a class="list-group-item list-group-item-action" href="#{{$cat->id}}" data-toggle="collapse" aria-expanded="true" aria-controls="sub-men1">{{$cat->name}}
</a>
<div class="collapse hide" id="{{$cat->id}}" data-parent="#list-group-men">
#foreach($cat->categories as $subcat)
#if($subcat->status==1)
<div class="list-group">
{{$subcat->name}}
</div>
#endif
#endforeach
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
This my routes file i.e (web.php).
// use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// All Routes after Login
Route::group(['middleware'=>['frontlogin']],function(){
Route::get('/view-job','frontController#view');
Route::match(['get', 'post'], '/browse', 'frontController#browse');
Route::get('/resume/{url}','ResumeController#resumes');
// Resume Detail Page
Route::get('/resume/{id}','ResumeController#resume');
});
Route::match(['get', 'post'], '/how', 'frontController#how');
Route::get('/logout', 'AdminController#logout');
This is the controller of the view file frontController.blade.php (browse_categories.blade.php). The function is browse()
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;
use App\Category;
use App\Country;
use App\Enquiry;
use App\Resume;
use App\User;
use App\Job;
use Validator;
use Auth;
use Session;
use Image;
use DB;
class frontController extends Controller
{
public function browse(){
$title='Browse Categories';
$resume = Resume::get();
foreach($resume as $key => $val){
$category_name = Category::where(['id' => $val->category_id])->first();
$resume[$key]->category_name = $category_name->name;
}
$resume = json_decode(json_encode($resume));
// echo "<pre>"; print_r($resume); die;
$categories_menu = "";
$categories = Category::with('categories')->where(['parent_id' => 0])->get();
$categories = json_decode(json_encode($categories));
return view('pages.browse_categories')->with(compact('categories_menu', 'categories', 'resume'), 'title', $title);
}
}
This is the view file I get the error 404 (detail.blade.php)
#section('content')
<!-- Start All Title Box -->
<div class="all-title-box">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h2>Details</h2>
<!-- <ul class="breadcrumb">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item active">Browse</li>
</ul> -->
</div>
</div>
</div>
</div>
<!-- End All Title Box -->
<!-- Start Shop Page -->
<div class="shop-box-inner">
<div class="container">
<div class="row">
#include('layouts.frontLayouts.frontSidebar')
<div class="col-xl-9 col-lg-9 col-sm-12 col-xs-12 shop-content-right">
<div class="right-product-box">
<div class="product-item-filter row">
<div class="col-12 col-sm-8 text-center text-sm-left">
<!-- <div class="toolbar-sorter-right">
<span>Sort by </span>
<select id="basic" class="selectpicker show-tick form-control" data-placeholder="$ USD">
<option data-display="Select">Nothing</option>
<option value="1">Popularity</option>
<option value="2">High Price → High Price</option>
<option value="3">Low Price → High Price</option>
<option value="4">Best Selling</option>
</select>
</div> -->
<p>Profile Details</p>
</div>
<div class="col-12 col-sm-4 text-center text-sm-right">
<!-- <ul class="nav nav-tabs ml-auto">
<li>
<a class="nav-link" href="#grid-view" data-toggle="tab"> <i class="fa fa-th"></i> </a>
</li>
<li>
<a class="nav-link active" href="#list-view" data-toggle="tab"> <i class="fa fa-list-ul"></i> </a>
</li> -->
</ul>
</div>
</div>
<div class="row product-categorie-box">
<div class="tab-content">
<!-- <div role="tabpanel" class="tab-pane fade" id="grid-view">
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-4 col-xl-4">
#foreach($resume as $res)
<div class="products-single fix">
<div class="box-img-hover">
#if(!empty($res->image))
<img src="{{ asset('/images/uploads/passport/small/'.$res->image) }}" class="img-fluid" alt="Image">
#endif
<div class="mask-icon">
<a class="cart" href="{{ url('/resume/'.$res->id) }}">View Profile</a>
</div>
</div>
<div class="why-text">
<h4>{{ $res->user_name}}</h4>
<h5> {{ $res->category_name}}</h5>
</div>
</div>
</div>
#endforeach
</div>
</div> -->
<div role="tabpanel" class="tab-pane fade show active" id="list-view">
<div class="list-view-box">
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-4 col-xl-4">
<div class="products-single fix">
<div class="box-img-hover">
<!-- <div class="type-lb">
<p class="sale">Sale</p>
</div> -->
<img src="{{ asset('/images/uploads/passport/medium/'.$resumeDetails->image) }}" class="img-fluid" alt="Image">
<!-- <div class="mask-icon">
<ul>
<li><i class="fas fa-eye"></i></li>
<li><i class="fas fa-sync-alt"></i></li>
<li><i class="far fa-heart"></i></li>
</ul>
</div> -->
</div>
</div>
</div>
<div class="col-sm-6 col-md-6 col-lg-8 col-xl-8">
<div class="why-text full-width">
<h4>{{ $resumeDetails ->user_name }}</h4>
<h5>Expected Salary ${{ $resumeDetails->salary}}</h5>
<p>Staff Information: {{ $resumeDetails->summary}}</p>
<a class="btn hvr-hover" href="#">Message</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- End Shop Page -->
#endsection
This is the controller of the view file where I get the error 404 i.e detail.blade.php (Resume Controller) The function is resume
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use App\Resume;
use App\Category;
use App\Country;
use App\User;
use Auth;
use Session;
use Image;
use DB;
class ResumeController extends Controller
{
public function resumes($url=null){
$categoryCount = Category::where(['url'=>$url,'status'=>1])->count();
if($categoryCount==0){
abort(404);
}
$categories = Category::with('categories')->where(['parent_id' => 0])->get();
$categoryDetails = Category::where(['url'=>$url])->first();
if($categoryDetails->parent_id==0){
$subCategories = Category::where(['parent_id'=>$categoryDetails->id])->get();
$subCategories = json_decode(json_encode($subCategories));
foreach($subCategories as $subcat){
$cat_ids[] = $subcat->id;
}
$resumeAll = Resume::whereIn('resume.category_id', $cat_ids)->where('resume.status','1')->orderBy('resume.id','Desc');
$breadcrumb = "<a href='/'>Home</a> / <a href='".$categoryDetails->url."'>".$categoryDetails->name."</a>";
}else{
$resumeAll = Resume::where(['resume.category_id'=>$categoryDetails->id])->where('resume.status','1')->orderBy('resume.id','Desc');
$mainCategory = Category::where('id',$categoryDetails->parent_id)->first();
$breadcrumb = "<a href='/'>Home</a> / <a href='".$mainCategory->url."'>".$mainCategory->name."</a> / <a href='".$categoryDetails->url."'>".$categoryDetails->name."</a>";
}
$resumeAll = $resumeAll->paginate(6);
// $resumeAll = json_decode(json_encode($resumeAll));
// echo "<pre>"; print_r($resumeAll); die;
return view('pages.listing')->with(compact('categories','resumeAll','categoryDetails','url','breadcrumb'));
}
public function resume($id = null){
// Show 404 Page if resume is disabled
$resumeCount = Resume::where(['id'=>$id,'status'=>1])->count();
if($resumeCount==0){
abort(404);
}
// Get resume Details
$resumeDetails = Resume::with('experience')->where('id',$id)->first();
$relatedResumes = Resume::where('id','!=',$id)->where(['category_id' => $resumeDetails->category_id])->get();
$categories = Category::with('categories')->where(['parent_id' => 0])->get();
$categoryDetails = Category::where('id',$resumeDetails->category_id)->first();
if($categoryDetails->parent_id==0){
$breadcrumb = "<a href='/'>Home</a> / <a href='".$categoryDetails->url."'>".$categoryDetails->name."</a> / ".$resumeDetails->user_name;
}else{
$mainCategory = Category::where('id',$categoryDetails->parent_id)->first();
$breadcrumb = "<a style='color:#333;' href='/'>Home</a> / <a style='color:#333;' href='/resume/".$mainCategory->url."'>".$mainCategory->name."</a> / <a style='color:#333;' href='/resume/".$categoryDetails->url."'>".$categoryDetails->name."</a> / ".$resumeDetails->user_name;
}
$meta_title = $resumeDetails->user_name;
$meta_description = $resumeDetails->summary;
$meta_keywords = $resumeDetails->user_name;
return view('pages.detail')->with(compact('resumeDetails','categories','relatedResumes','meta_title','meta_description','meta_keywords','breadcrumb'));
}
public function deleteResume($id = null){
Resume::where(['id'=>$id])->delete();
return redirect()->back()->with('flash_message_success', 'Resume has been deleted successfully');
}
}
Overall Solution: Replace url('/resume/'.$res->id) with action('ResumeController#resume', $resume). You may have to rearrange the order of your two url same routes. I highly recommend that you make them separate urls or remove the parameter from the one.
Solution to Current Problem: You can run php artisan route:list and it'll give you an output of every single valid route. From there, you'll be able to see the issue.
Side note: You can use any instead of match.
How can I call a method of a controller from blade template and receive the data is returned by the method. For Example:
MeetingRoomController
class MeetingRoomController extends \BaseController
{
public function index()
{
//get all the meeting room
$meetingRoomCountry = MeetingRoom::select('country')->distinct()->orderBy('country')->get();
return View::make('index')->with('meetingroom', $meetingRoomCountry);
}
public function findLocationForNavBar( $country )
{
$meetingRoomLocation = MeetingRoom::select('location')
->distinct()
->where('country', '$country')
->orderBy('country')
->get();
return View::make('index')- >with('meetingroomLocation', $meetingRoomLocation);
}
}
View - index.blade.php
<div id="dropdown-lvl1" class="panel-collapse collapse">
<div class="panel-body">
<ul class="nav navbar-nav">
#foreach($meetingroom as $key => $value)
<li class="panel panel-default" id="dropdown">
<a data-toggle="collapse" href="#dropdown-lvl2">
<span class="glyphicon glyphicon-off"></span> {{$country = $value->country}} <span class="caret"></span>
</a>
<div id="dropdown-lvl2" class="panel-collapse collapse">
<div class="panel-body">
<ul class="nav navbar-nav">
<li>Location</li>
</ul>
</div>
</div>
</li>
#endforeach
</ul>
</div>
</div>
I want to collect the location based on corresponding country. According to my logic, First of all I collected the distinct country then searched the location by findLocationForNavBar method in MeetingRoomController.
If you're using Laravel 5.1, you could try a Service Injection from your blade template.
http://laravel.com/docs/5.1/blade#service-injection
Example from the docs:
#inject('metrics', 'App\Services\MetricsService')
<div>
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>
You can do all this stuff in the index() method and send location and country to view.