Route Not Hitting Controller - laravel

When I try to create a view for a product, the URL gets built correctly.
http://localhost:8000/product/my-slug
However, I get a 404 page not found and I have no idea why. It's like the controller is not getting called.
Initiation
<a href="{{ route('product.view', $product->slug) }}">
Route
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
Controller
public function view($slug)
{
$product = Product::find($slug);
return view('products.view', compact('product'));
}
View
<h1>{{ $product->name }}</h1>
EDIT
web.php
Route::get('/', 'ProductsController#index')->name('product.index');
Route::get('/products/create', 'ProductsController#create')->name('product.create');
Route::post('/products', 'ProductsController#store')->name('product.store');
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
/*Route::get('/users', 'UsersController');*/
Route::get('/contact', 'PagesController#contact');
Route::get('/about', 'PagesController#about');

Try changing
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
to
Route::get('/product/{slug}', 'ProductsController#view')->name('product.view');
Ref: Laravel Routing

Related

how to pass one data view and others view page in 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);
})

I want make some thing like that in my web.php. But I dont know what should I write

Hi can you please solve my problem? My problem is i want to access my controller in a master page
in my web.php
Route::get('/', [LayoutController::class, 'home']{
$abouts = DB::table('home_abouts')->first();
return view('home', compact('abouts'));
});
in my master.blade.php
```
<div class="section-title">
<h2>About Us</strong></h2>
</div>
<div class="row content">
<div class="col-lg-6" data-aos="fade-right">
<h2> {{ $abouts->title }}</h2>
<h3>{{ $abouts->short_dis }}</h3>
</div>
<div class="col-lg-6 pt-4 pt-lg-0" data-aos="fade-left">
<p>
{{ $abouts->long_dis }}
</p>
</div>
</div>
How can I write this correctly on web.php? I don't know what should I write now
The problem is that you're using two various methods:
Using a closure
Using a controller method
If you want to use a closure, rewrite it like this:
Route::get('/', function() {
$abouts = DB::table('home_abouts')->first();
return view('home', compact('abouts'));
});
And if you want to use controllers (recommended), do this:
Route::get('/', [LayoutController::class, 'home']);
Then implement your home() method in LayoutController.
just to add to #WebPajooh's answer there are view routes
Route::view('/', 'home', ['abouts' => DB::table('home_abouts')->first()]);
but using closure-based routes becomes a mess really fast, so better stick with controller variant
// inside your web.php file
// make sure MasterController is imported
Route::get('/', [MasterController::class, 'index']);
// in your controller
public function index(){
$abouts = DB::table('home_abouts')->first();
return view('master', [
'abouts' => $abouts
]);// thats master.blade.php
}
You can use controllers
Inside your web.php
use App\Http\Controllers\LayoutController;
Route::get('/', [LayoutController::class, 'home']);
And inside your controller LayoutController, create a home function
public function home()
{
$abouts = DB::table('home_abouts')->first();
return view('home', ['abouts' => $abouts]);
}
Then create a blade inside resources/views, for instance, let's say home.blade.php
{{ $abouts->table_column_here }}

laravel pathing in link

I am using same thing but result is different. In posts.blade.php i can call like this
<a href = "posts/{{post->id}}"edit>Edit</a>
But when i use same thing in rooms.blade.php indisde rooms folder i had to use like this.
<a href = "{{$room->id}}/edit" class = 'btn btn-primary'> Edit</a>
It is really confusing me. Any solution? i want to add link for editing post inside room. I have tried many things but i dont understand.
My Route
Route::get('/','PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::get('/register', 'PagesController#register');
Route::get('/logout', 'PagesController#logout');
Route::get('/posts', 'PostsController#posts');
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
You can give the name of the route
Foe example:
Route::get('/post/{id}', 'PostController#showPost')->name('post.show');
And then call the blade
Link to Resource {{ $id }}

Laravel - passing route parameter to blade

I am using Laravel 5.6, with the default make:auth mechanism.
In the routes/web.php, I would like to add a language middleware as follow:-
Route::prefix('{lang}')->group(function () {
Route::get('password/reset', 'Auth\ForgotPasswordController#showLinkRequestForm')->name('password.request');
});
Now I wish to apply this in blade:-
<a href="{{ route('password.request') }}">
But debugger just say:-
Missing required parameters for [Route: password.request] [URI: {lang}/password/reset].
I believe the blade cannot not get the {lang} from route. How can it be achieved?
Try this
<a href="{{ url('en') }}/password/reset">
When you pass a prefix in the route it means that it is going to be included in the route, so when you say
Route::prefix('{lang}')->group(function () {
Route::get('password/reset' .......
Route::get('foo/bar' .......
it means the route will be like this "http://yourwebsite.com/{lang}/password/reset"
"http://yourwebsite.com/{lang}/foo/bar"
so each time you pass any route that falls in that group will need to have a variable called $lang so the link can be initialized.
so in your case
<a href="{{ route('password.request', ['lang' => 'en']) }}"> //you can change en to your preferred language.
if you want to add a middleware to specific route or route group, it is passed in this way
Route::get('/', function () {
//
})->middleware('first', 'second');
or
Route::group(['middleware' => ['first']], function () {
//
});
if you want to create another language, you can check the below links:
Laravel localization
spatie/laravel-translation-loader

NotFoundHttpException Laravel

I am very new in learning Laravel. I want to fetch data from a database and show it. I can do it. But I want to use the title (fetched from the database) as a link. but then I get a NotFoundHttpException.
Routes
Route::get('articles', 'ArticleController#index');
Route::get('articles/{id}', 'ArticleController#show');
Controller
class ArticleController extends Controller
{
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id){
$article = Article::find($id);
return view('articles.show', compact('article'));
}
}
View
#extends('new_welcome')
#section('content')
<h1>Articles</h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop
Can someone help me in this case?
Your problem is because of You've "eat" one curly brace (blade engine skips it):
was:
href="{url ('/articles',$article->id)}"
have to be:
href="{{url ('/articles',$article->id)}}"
as You said:
if I click on any single article title then it can not show me the
specific article. But, if I give the URL "homestead.app/articles/2";
so You can see that when You click on link Your browser's address bar becomes:
homestead.app/{url ('/articles',$article->id)}
Because You're beginner so I'll give You advice to not to set direct url in views using url() helper.
Named routes are better if You want to have app that will work properly if in future You decide to change url from: articles to artcls. In this named routes will save You from bulk changing urls in view files.
set name to Your route using 'as' directive that makes Your routing flexible for changes (when You need to change URL so You change only path and keep views unchanged):
Route::get('articles/{id}', ['as' => 'article', 'uses' => 'ArticleController#show']);
Route::get('articles', ['as' => 'articles', 'uses' => 'ArticleController#index']);
change Your view file (find route helper in href):
#extends('new_welcome')
#section('content')
<h1> Articles </h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop

Resources