I want to call variable to views in laravel, but something not working
here is my controller
public function index($id,$slugify, Request $request,WiuCookie $wcookie)
{
$user = ActionUsers::where('id', $id)->first();
$petition = Petitions::where('slug',$slugify)->first();
$isSigned = $wcookie->checkId($petition->id);
return view('layouts.master',compact('user','petition','isSigned'));
}
and here is my view
#if($isSigned)
<li>
<a href="#">
{{$user->name}}
</a>
</li>
#else
<li>
<a href="#">
register
</a>
</li>
<li>
<a href="#">
log-in
</a>
</li>
#endif
and error is Undefined variable: user
return view('layouts.master',compact(['user'=>$user,'petition'=>$petition,'isSigned'=>isSigned]));
It just works for me perfectly.
Try this:
$compactData=array('user','petition','isSigned');
return View::make('layouts.master', compact($compactData));
try this in passing remove compact and pass using with
->with('user',$user)
Related
I wants to show users list into my dashboard pannel but it shows me this problem :
Too few arguments to function App\Http\Controllers\ProduitController::show(), 0 passed in C:\wamp64\www\Ecommerce\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
Controller File :
public function show($id)
{
$produit = Produit::find($id);
return view('produits', compact('produit'));
}
blade file :
#foreach($produits as $produit)
<div class="product__item__pic set-bg" data-setbg="{{ asset('img/uploads/'.$produit->image)}}">
<ul class="product__item__pic__hover">
<li><i class="fa fa-heart"></i></li>
<li><i class="fa fa-retweet"></i></li>
<li><i class="fa fa-shopping-cart"></i></li>
</ul>
</div>
<div class="product__item__text">
<h6>{{ $produit->designation }}</h6>
<h5>{{ $produit->prix_uni }} Mad</h5>
</div>
#endforeach
Route :
Route::get('/produits','ProduitController#show');
If you have an id in your function definition, this should also be there in route definition. Route variables is defined with {id}.
Route::get('/produits/{id}','ProduitController#show');
For a best practice, use model binding, if you call your route variable the same as the model, you can automatically load it by typehinting it.
Route::get('/produits/{produit}','ProduitController#show');
public function show(Produit $produit)
I have a menu and in this menu items come from Categories
For the test, I create a new section called Menu for this menu to create the menu manually.
But after that, I return all codes to the previous version, But still, I have an error for undefined $menus
But I don't have this menu anywhere, where is the problem?
Error
Undefined variable $menus (View:
C:\Web\projects\thermotajhiz\resources\views\layouts\header.blade.php)
View
<ul class="list-menu-level-2">
#foreach($categories as $category)
#if($category->menu == 1)
<li class="item-menu-2">
<a href="#" class="list-category-menu-2" rel="nofollow">
<i class="fa fa-desktop"></i>
{{ $category->name }}
</a>
<ul class="megamenu-level-3" style="display:block;">
#include('layouts.categories-group',['categories' => $category-
>get_sub($category->id)])
</ul>
</li>
#endif
#endforeach
</ul>
Controller
$categories = Category::with('child')->where('parent_id', 0)->get();
return view('index', compact('categories'));
Please run a command php artisan view:clear, because laravel makes a cache for every view file. after runnig this command, all compiled views should clear and rebuild cache for view for present file.
I don't know why the error is being thrown, because the route already exists.
master.blade.php:
<!-- Nav Item - Transaksi -->
<li class="nav-item">
<a class="nav-link" href="{{route('transaksi.index')}}">
<i class="fas fa-fw fa-folder"></i>
<span>Transaksi</span></a>
</li>
web.blade.php:
Route::resource('transaksi','TransaksiController');
TransaksiController:
public function index()
{
$data = DB::table('tbl_transaksi')
->where('tbl_transaksi.nama_peminjam','like',"%{$request->keyword}%")
->paginate(20);
return view('admin.transaksi.index',['data'=>$data]);
}
The error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [transaksi.index] not defined. (View: C:\xampp\htdocs\SistemPerpustakaan\resources\views\admin\master.blade.php)
http://localhost:8000/dashboard
resolved, I just forgot to delete the route with the same name
i am updating my application from laravel 5.2 to 5.3. Most of the things seems to work fine.
But i dont know what is happening but when i am trying to define route in anchor tag, its not working. I have done something similar to this:
<a href="{{route('backend.pages.index')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
Its showing error Route [backend.pages.index] not defined.. Here is how the created the route.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController');
});
I have a template called 'mainmenu.blade.php' in which i have use this route. This mainmenu is called in main structure through #include('layouts.backend.backendstructure.mainmenu').
Is routing method is changed in laravel 5.3? Or is there any mistake from my side?
Thank you!(Advance)
The problem here is
{{route('backend.pages.index')}}
instead use
<a href="{{route('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
The route is defined as backend/pages. To return view add a method in PagesController and return the view there.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController#dummymethod');
});
Dummy method
public function dummymethod
{
return view('backend.pages.index');
}
Edit
I think you're looking for something like this
Route::resource('backend/pages','Backend\PagesController', ['names' => ['index' => 'backend.pages.index']]);
Check the docs here
You should write your code like this:
<a href="{{ route('backend/pages')}} " class="nav-link ">
<span class="title">All Pages</span>
</a>
or like this:
<a href="{{ url('backend/pages') }}" class="nav-link ">
<span class="title">All Pages</span>
</a>
Try:
<a href="/backend/pages" class="nav-link ">
<span class="title">All Pages</span>
</a>
https://laravel.com/docs/5.3/routing
You can try link with URL to like, I use in following manner
<a href="{{URL::to('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
I cant't figure out what's going wrong with a link from my "welcome" page to a "profile" page.
It says "Undefined variable: restaurants (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/welcome.blade.php)"
The strange thong is that I pass the Variable to my view.
My PagesController is:
public function welcome() {
$restaurants = User::orderByRaw('RAND()')->take(3)->get();
return view('welcome')->withRestaurants($restaurants);
}
My View is:
#foreach($restaurants as $key => $restaurant)
<div class="col-md-4">
<div class="card">
<div class="image">
<img src="{{asset('images/frontend/profile/dummy/profilehero/hero4.png')}}" alt="..." />
<div class="filter filter-white">
<a href="{{ URL::to('profile/'.$restaurant->id)}}" type="button" class="btn btn-info btn-round btn-fill">
<i class="fa fa-heart"></i> View Restaurant
</a>
</div>
</div>
</div>
</div>
#endforeach
The Restaurants Variable is passed properly to my welcome view, but when I click the link the error occurs on /profile/{id} url.
Change return view('welcome')->withRestaurants($restaurants);
to
return view('welcome')->with('restaurants', $restaurants);
OR
return view('welcome', compact('restaurants');
Try with this,
return view('welcome')->with("restaurants",$restaurants);
OR
return view('welcome',["restaurants" => $restaurants]);
Check laravel docs : https://laravel.com/docs/master/views#passing-data-to-views
Change the following line:
return view('welcome')->withRestaurants($restaurants); // there is nothing like withRestaurants in laravel
to
return view('welcome', array('restaurants' => $restaurants));
and try again.