how to pass one data view and others view page in laravel - laravel

i have two pages file in my view
first is my home blade then second is category blade and both of them using extends asidenav
my problem is when im redirecting to category file it gives me a error Undefined variable: categories but in my home file when im redirecting it's working fine . i will describe my code.
in my controller HomeController i pass one data categories to home.blade.php
class HomeController extends Controller
{
public function home()
{
$categories = Category::select('id','name')->get();
return view('home', compact('categories'));
}
public function category(){
return view('category');
}
}
the output of my home.blade.php
#extends('asidenav')
#section('section')
<main>
this is homepage
</main>
#endsection
you can see in my home blade there is a extends asidenav . inside of my asidenav extends the data categories i passed is inside of that asidenav to make a list of categories
the output of my extends asidenav
where the data i pass categories
<ul>
#foreach($categories as $category)
<li><span>{{ $category->name }}
#endforeach
</ul>
when i click the ahref to redirect the category blade it gives the error undefined variable categories. but when im redirecting to home blade it's working
the output of my category.blade.php
#extends('asidenav')
#section('section')
<main>
this is category
</main>
#endsection
but when i tried this code in my category controller it's working fine the error of undefined variable categories not showing
public function category()
{
$categories = Category::select('id','name')->get();
return view('category', compact('categories'));
}
my point here is i want to pass only single data categories in my home blade and others file blade like a props but i don't have a idea how to do that

You could create a service provider responsible for inyecting those variables without declaring them in your controller, in your boot method :
use Illuminate\Support\Facades\View;
View::composer(['asidenav', 'home'], function($view){
$categories = Category::all();
return $view->with('categories', $categories);
})

Related

how can I resolve this while working with for else loop and compact function in Laravel?

I'm using laravel version 7.30.1. it shows the error undefined variable $services.
code for home controller.
class HelloController extends Controller
{
public function services()
{
$services = [
'service 1',
'service 2'
];
return view('/services',compact('services'));
}
}
code for blade file.
#section('content')
<H1>welcome to laravel 6 from services</H1>
<ul>
#forelse($services as $service)
<li>{{ $service }}</li>
#empty
<li>this list is empty</li>
#endforelse
</ul>
#endsection
code for route file
route::view('/services','services');
route::view('/services','services');
here your only loading view not controller that's why your data is not pass in to view
use get req. which will be in controller then form controller load view with data
Route::get('services',[\App\Http\Controllers\HelloController::class, 'services']);

How to call multiple methods or controllers to a same route in laravel

Find below the controller code with two methods and suggest me how to call these two methods in the same route or whether I have to create two different controllers for the same page(route).
class TicketController extends Controller
{
public function show(){
$results=Whmcs::GetTickets([
]);
return view('clientlayout.main.index',compact('results'));
}
public function set(){
$test=Whmcs::GetInvoices([
]);
return view('clientlayout.main.index',compact('test'));
}
}
The route file:
Route::get('clientlayout.main.index','TicketController#show');
Route::get('clientlayout.main.index','TicketController#set');
Find the code in the blade file and after running this I'm getting an error
undefined index:Results.
#foreach($results['tickets']['ticket'] as $key)
{{$key['subject']}}
#endforeach
#foreach($test['invoices']['invoice'] as $value)
{{$value['firstname']}}
#endforeach
When I run these two foreach loops in a different blade file it executes correctly, but I need these two results to be viewed in the same file.
How to view both tickets and invoices in the same index page?
Combine the two controllers into one and perform both queries in a single method:
class InvoiceTicketController extends Controller
{
public function show(){
$tickets = Whmcs::GetTickets([]);
$invoices = Whmcs::GetInvoices([]);
return view('clientlayout.main.index',compact('tickets', 'invoices'));
}
}
Then update one of the those routes to use the combined controller:
Route::get('clientlayout.main.index','InvoiceTicketController#show');
You'll have access to both $tickets and $invoices collections in the blade file this way:
#foreach($tickets as $ticket)
{{ $ticket->subject }}
#endforeach
#foreach($invoices as $invoice)
{{ $invoice->firstname }}
#endforeach

Laravel - include few views in one with controllers

Im working on Laravel and i have problem.
I created two controllers: PostController - has a view and PostController has a view.
I created next Controller called HomeController and i want to execute both Controller here PostController and MyProfileController.
I created a method in HomeController:
public function index()
{
$profile_view = app('App\Http\Controllers\MyProfileController')->index();
$post_view = app('App\Http\Controllers\PostController')->index();
return view('home',
[
'profile_view' => $profile_view,
'post_view' => $post_view
]
);
}
And im trying to show in view (home.blade.php)
#extends('layout')
#section('main-content')
Something
{!! $profile_view !!}
{!! $post_view !!}
#endsection
and it is viewing only one view from $post_view.
Anyone has a idea for this problem?
You need to create another view that would use ®include to do that and refer that view from your controller.
See https://laravel.com/docs/5.5/blade#including-sub-views

Laravel: Retrieve data from DB and diplay in View

I have a bug here in my code that show me a probleme while displaying data in the home page
Controller
class Annonce_indexController extends Controller
{
public function index()
{
$annonce_residentiel = Annonce_residentiel::all();
return view('/' , compact('annonce_residentiel'));
}
}
Route
Route::get('/index','Annonce_indexController#index');
Blade View
{{ $annonce_residentiel->prix }}
It says that $annonce_residentiel is undefined
Edit:
The problem is I have two routes to the same view:
Route::get('/','Admin\Annonce_indexController#index');
Route::get('/',array('as' =>'viewville','uses'=>'VilleController#index'));
Solution
Change the second route to post !
Route::get('/','Admin\Annonce_indexController#index');
Route::post('/',array('as' =>'viewville','uses'=>'VilleController#index'));
$annonce_residentiel is an object and not a variable so you cannot just call it and expect it to pop a value. For just demo purpose and to understand how it works try the following code in your view.
#foreach($annonce_residentiel as $data)
{{ $data->prix }}
#endforeach
Controller
class Annonce_indexController extends Controller
{
public function index()
{
$annonce_residentiel = Annonce_residentiel::first();
return view('/' , compact('annonce_residentiel'));
}
}
Blade View
{{ $annonce_residentiel->prix }}

Laravel pass exact parameter to route

i have translated url which i need to redirect to the specific controller function, but i need also to pass an exact parameter.
For example i want to show all football news, but in the url i do not have the ID of the sport football (id=1) so i need to pass the parameter id=1 to the index() function.
Route::get('/football-news/', ['as' => 'news.index', 'uses' => 'NewsController#index']);
it is not an option to pass 'football' as a parameter, because it is just an example. The real route is translated and the code looks like that:
Route::get(LaravelLocalization::transRoute('routes.football.news'), ['as' => 'news.index', 'uses' => 'NewsController#index']);
suppose you have a NewsController to fetch all news be like
class NewsController extends Controller
{
public function index()
{
$news = News::all(); //you have to create News model
return view('news.index', compact('news')); //use to pass data in view
}
public function show($id)
{
$news_detail=News::find($id); //to fetch detail of news from database
return view('news.show', compact('news_detail'));
}
}
create index.php and show.php in views/news folder. in index.php
#foreach($news as $news_item)
<div>
{{ $news_item->title }}
</div>
#endforeach
here using "/news/{{$news_item->id}}" you can pass id of specific news into route file.
in show.php
<h1>news</h1>
<h1>
{{ $news_detail->title }}
</h1>
<ul class="list-group">
#foreach($news_detail->detail as $details)
<li class="list-group-item">{{$details}}</li>
#endforeach
</ul>
in route file
Route::get('/news/{news}', 'NewsController#show');
now you have to create show($id) function in NewsController.php which parameter is id.
You can append the index URL with ?id=1 parameter (eg. domain.com?id=1) and get it in your index controller action by using Request::get('id');
For example:
Url in template file:
<a href="domain.com?id=1" />
In your NewsController:
public function index(Request $request){
$id = $request->get('id');
}
You should be able to have access to the parameter even though you didn't specify wildcards in the route file.
Edit:
you will have to call a different #action for a different route. You can pass in an id wildcard.
For example, in Route file:
Route::get('tennis-news/{id}', 'NewsController#tennisIndex');
Route::get('football-news/{id}', 'NewsController#footballIndex');
Then in the NewsControlleryou must have public methods tennisIndex($id) and footballIindex($id), these methods will have access to the wildcard you set in the route.
For example, in NewsController
public function tennisIndex($id){
$tennnis_news = News::where('sport'='tennis)->where('id', $id)->get();
return view('tennis_news', compact('tennnis_news'));
}

Resources