Pass data to view in Laravel - php

I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
#foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
#endforeach
</ul>
I use Laravel version 5.5

Make sure in your route file on routes/web.php you have a route to the index function like so:
Route::get('urlToYourView', 'YourController#index');

Related

Undefined variable Issue with laravel blade

In my laravel application, I'm having a list of regions in my DB
This is my controller method
public function index()
{
$region_options = Region::get();
return view('home',compact('region_options'));
}
and I have following in my blade,
#if(!$region_options->isEmpty())
<ol class="region-pop">
#foreach ($region_options as $key => $region)
<li>{{ $region->name }}</li>
#endforeach
</ol>
#endif
But when I run this it gave me an error saying, Undefined variable: region_options
I'm struggling to find what the issue is...

ErrorException Undefined variable:

Why I am getting this error?
ErrorException
Undefined variable: features (View: C:\xampp\htdocs....views\layouts\index.blade.php)
FeaturedController.php
public function index()
{
$features = Feature::get();
return view ('layouts.index')->with(compact('features'));
}
ProductsController.php
public function index()
{
$products = Product::get();
return view ('products')->with(compact('products'));
}
layouts page- index.blade.php
#yield('content')
#foreach($features as $f)
<li>
<div class="prodcut-price mt-auto">
<div class="font-size-15">LKR {{ $f ['features_id'] }}.00</div>
</div>
</li>
#endforeach
view page - index.blade.php
#extends('layouts.index')
#section('content')
#foreach($products as $p)
<div class="mb-2">{{ $p ['prod_sub_category'] }}</div>
<h5 class="mb-1 product-item__title">{{ $p ['prod_name'] }}</h5>
<div class="mb-2">
<img class="img-fluid" src="{{asset('/storage/admin/'.$p ['prod_image_path'] ) }}" alt="Image Description">
</div>
<div class="flex-center-between mb-1">
<div class="prodcut-price">
<div class="atext">LKR {{ $p ['prod_price'] }}.00</div>
</div>
<div class="d-none d-xl-block prodcut-add-cart">
<i class="ec ec-shopping-bag"></i>
</div>
web.php
Route::resource('/products', 'ProductsController');
Route::resource('/layouts/index', 'FeaturedController#index');
Aside from not passing your variables to your blade views appropriately which other answers have pointed out, your trying to access features from a controller that does not have features set.
The controller below sets features and then makes use of it in the layouts.index blade file.
FeaturedController.php
public function index()
{
$features = Feature::get();
return view ('layouts.index')->with(['features' => $features]);
// or
// return view ('layouts.index', compact('features'));
}
While this controller sets products but then makes use of a blade file that extends another blade file that has a features variable in it. This is why your getting the error
ProductsController.php
public function index()
{
$products = Product::get();
return view ('products', compact('products'));
}
And to fix it you must pass the features variable along side products like so:
ProductsController.php
public function index()
{
$products = Product::get();
$features = Feature::get();
return view ('products')->with(['features' => $features, 'products' => $products]);
}
But if more than one blade file is going to extend this layouts.index file then this approach is not advisable, and situations like this is why Taylor Otwell introduced Blade Components. You can now move the features blade view and logic to a component that can wrap around any other file you want or be included.
The documentation is straight forward but if you want me to show you how to implement it to solve your dilemma then hit me up on the comment below.
as u r using data in layout u should use laravel view composer to share data to layout file ref link https://laravel.com/docs/7.x/views#view-composers
in your AppServiceProvider.php
inside boot() add this line
public function boot()
{
\View::composer('layouts.index', function ($view) { // here layout path u need to add
$features = Feature::get();
$view->with([
'features'=>$features,
]);
});
}
It share data based on specif view file like here layouts.index data is send to this view so if u not send data from controller it will get data from view composer
You can change your controller to this:
public function index()
{
$features = Feature::all();
return view ('layouts.index', compact('features'));
}
A your blade you should actually do #section instead:
#section('content')
#foreach($features as $f)
<li>
<div class="prodcut-price mt-auto">
<div class="font-size-15">LKR {{ $f->features_id }}.00</div>
</div>
</li>
#endforeach
#endsection

Want to add {{$value['category']}} in {{$value['category']}} in Laravel

I want to set {{$value['category']}} in {{route('')}}
This is my code:
#foreach($details as $value)
{{$value['category']}}
#endforeach
Controller function:
public function viewhome(Request $req){
$product = product::all()->unique('category');
return view('homepage',['details'=> $product]);}
Route:
Route::get('/homepage/db_val', 'HomeController#db_val')->name('db_val');
How to declare href properly. And what will be the route. Thank you.
1) warn : you call db_val function in HomeController but you show viewhome method in your question.
Route::get('/homepage/db_val', 'HomeController#<b>db_val</b>')->name('db_val');
if you want use method viewhome :
Route::get('/homepage/db_val', 'HomeController#<b>viewhome</b>')->name('db_val');
2) route is used with named route
you have a route named 'db_val' --> Route::.... ->name('db_val');
so it must be used like that ,
<a href='{{route('db_val')}}
3) in your case , assuming $value in foreach is an array with a 'category' index inside
you can use url instead route
#foreach($details as $value)
<a href="{{url('/your_url')}}/{{$value['category']}}">
link to {{$value['category']}}
</a>
#endforeach
4) but blade spirit is
route.php
Route::get('/showcategory/{id}','HomeController#showcategorie')->name('showcat');
view
#foreach($details as $value)
<a href="{{route('showcat', $value['category'])}}">
#endforeach
it means : you have one named route /showcategory with a parameter /{id}
Route:
Route::get('/homepage/db_val_1', 'HomeController#db_val_1')->name('db_val_1');
Route::get('/homepage/db_val_2', 'HomeController#db_val_2')->name('db_val_2');
Route::get('/homepage/db_val_3', 'HomeController#db_val_3')->name('db_val_3');
...
Controller Function :
public function db_val_1(Request $req){
$all = product::all()->where('category', 'db_val_1');
return view('homepage', ['details'=> $all]);
}
public function db_val_2(Request $req){
$all = product::all()->where('category', 'db_val_2');
return view('homepage', ['details'=> $all]);
}
public function db_val_3(Request $req){
$all = product::all()->where('category', 'db_val_3');
return view('homepage', ['details'=> $all]);
}
...
View home: In route the value will be ($value->category) like this.
#foreach($details as $value)
{{$value['category']}}
#endforeach
I got the solution. Thank you for helping.

Missing required parameter in laravel

I've been trying to get this fixed for days now. I don't know what i'm doing wrong. I'm trying to edit a post in the dashboard.
My route looks like this
Route::get('/edit-post/post_id/{post_id}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostedit',
]);
for that route i have a controller
public function showPostEdit(Request $request, Post $post, $post_id){
$posts= $post->where('post_id',$post_id)->get();
return view('pages.dashboard.user.edit-post',compact('posts',auth()-
>user()->id));
}
My blade syntax looks like this
#if(Auth::check())
#if(auth()->user()->id === $posts->user_id)
<li class="list-inline-item"><span class="fa fa-edit"></span></li>
<li class="list-inline-item"><span class="fa fa-trash"></span></li>
#endif
#endif
Solution 1: primary key should be id instead of post_id in post table.
Route::get('/edit-post/{post}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostEdit',
]);
public function showPostEdit(Post $post){
return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
}
Solution 2:
Route::get('/edit-post/{postId}',[
'as'=>'edit-post',
'uses'=>'dashboardController#showPostEdit',
]);
public function showPostEdit($postId){
$post= (new Post())->where('post_id',$postId)->get();
return view('pages.dashboard.user.edit-post',compact('post',auth()->user()->id));
}

"Use of undefined constant..." error in Laravel

I am a Laravel newbie. I want to pass the results of a database query to a view. I get an error message "Use of undefined constant tasks - assumed 'tasks'". What am I doing wrong?
My code is as follows:
class TasksController extends BaseController{
public function index(){
$tasks = Task::all();
//return View::make(tasks.index, ['tasks' => $tasks]);
return View::make(tasks.index, compact('tasks'));
}
A snippet from my template page is shown below:
<body>
<h1>All tasks!</h1>
#foreach($tasks as $task)
<li>{{ $task-title }} </li>
#endforeach
return View::make('tasks.index')->with(compact('tasks'));
also change:
<li>{{ $task-title }} </li>
to
<li>{{ $task->title }} </li>
should be like this.
Try this,
return View::make(tasks.index, $tasks);
instead of
return View::make(tasks.index, compact('tasks'));

Categories