Too few arguments to function App\Http\Controllers\ProduitController::show() - php

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)

Related

Undefined variable $menus in laravel

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.

Route [transaksi.index] is not defined. but the route already exists with route:resource

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

href returning another URL

After I click in the menu navigation (for exemple href='home') when i am in the view .../public/single/1 they send me to .../public/single/home and not .../public/home
Here is my code:
MENU LIST
<div class="header_content d-flex flex-row align-items-center justify-content-start">
<div class="logo"><span>m</span>a <span> B</span>ibli<span>o</span></div>
<nav class="main_nav">
<ul class="d-flex flex-row align-items-start justify-content-start">
<li class="{{$home}}">Acceuil</li>
<li class="{{$about}}">About us</li>
<li class="{{$listing}}">Produits</li>
<li class="{{$blog}}">Nouveauté</li>
<li class="{{$contact}}">Contactez nous!</li>
</ul>
</nav>
<div class="submit ml-auto">Recherche >></div>
<div class="hamburger ml-auto"><i class="fa fa-bars" aria-hidden="true"></i></div>
</div>
</div>
WEB.PHP
Route::get('/single/{id}','AnnonceController#annonce');
Route::get('/home','PagesController#home');
Route::resource('home','AnnonceController');
And this is my controller:
public function annonce($id) {
$annonce = DB::table('annonce')
->join('article', 'annonce.ID_ANNONCE', '=', 'article.ID_ANNONCE')
->join('photo_articles', 'photo_articles.ID_ARTICLE', '=', 'article.ID_ARTICLE')
->select('annonce.*', 'article.NOM_ARTICLE','PHOTO_ARTICLE')
->where('annonce.ID_ANNONCE',$id)
->get();
return view('single')->with('annonce',$annonce);
}
It's happening because you giving href='home' which will add to your current URL , which means you are on single/1(route parameter) and when you click it will become single/home(parameter).
so give the full path.
I suggest you should use the route function of laravel and also give the name to your route like below
Route::get('/home','PagesController#home')->name('myhome');
see Documentation for naming route.
then call this in your blade like this
{{route('myhome')}}
You return statement should be like this return view('single',['id' => $id])->with('annonce',$annonce);
Note: Please use relationship in Laravel instead of DB::table
public function annonce($id) {
$annonce = DB::table('annonce')
->join('article', 'annonce.ID_ANNONCE', '=', 'article.ID_ANNONCE')
->join('photo_articles', 'photo_articles.ID_ARTICLE', '=', 'article.ID_ARTICLE')
->select('annonce.*', 'article.NOM_ARTICLE','PHOTO_ARTICLE')
->where('annonce.ID_ANNONCE',$id)
->get();
return view('single',['id' => $id])->with('annonce',$annonce);
}

model function call in blade view laravel

My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.

Dynamic menu laravel

I am starting with laravel, and made an integration with bootstrap. I want to make a menu bar to access to the information by year, the menu is made I this way to the view
<li class="dropdown-submenu">
<a tabindex="-1" href="#">2007</a>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="<?php echo url('/convenios/2007/registrar', $parameters = array(), $secure = null); ?>">Registrar</a></li>
<li>Consultar</li>
</ul>
</li>
I made this on a view called base.blade.php, there is a better way to make the menus or I made this in the right way?
menus table
id - parent_id - title - url - order - created_at - updated_at
Make Menu model
class Menu extends Model
{
public $timestamps = false;
protected $table = 'menus';
protected $fillable = array('parent_id','title','url','order');
public function parent()
{
return $this->belongsTo('App\Menu', 'parent_id');
}
public function children()
{
return $this->hasMany('App\Menu', 'parent_id');
}
}
in View
#foreach(App\Menu::orderBy('order','asc')->get() as $menuItem)
#if( $menuItem->parent_id == 0 )
<li {{ $menuItem->url ? '' : "class=dropdown" }}>
<a href="{{ $menuItem->children->isEmpty() ? $menuItem->url : "#" }}"{{ $menuItem->children->isEmpty() ? '' : "class=dropdown-toggle data-toggle=dropdown role=button aria-expanded=false" }}>
{{ $menuItem->title }}
</a>
#endif
#if( ! $menuItem->children->isEmpty() )
<ul class="dropdown-menu" role="menu">
#foreach($menuItem->children as $subMenuItem)
<li>{{ $subMenuItem->title }}</li>
#endforeach
</ul>
#endif
</li>
#endforeach
you can make controller as you like
this way i spent all day trying to get this working code, when i finished i decide to search for anybody ask for that issue
Have fun :)

Categories