NotFoundHttpException Laravel - 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

Related

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 #include controller data

I'm trying to receive data on a sidebar that is included in the blade template but i'm not getting any data delivered. I've tried adding #include('admin.sidebar',['message_counter' => $message_counter]) and in the sidebar view show as {{$message_counter}}. I'm getting a Undefined variable: message_counter.
My router:
Route::get('/admin/sidebar', [
'uses' => 'MessagesController#counter',
'as' => 'admin.sidebar'
]);
My controller
use App\Message;
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar')->with('message_counter', $message_counter);
}
My View
<span class="menu-collapsed">Messages <span class="badge badge-pill badge-primary ml-2"> {{$message_counter}} </span></span>
What i ultimately intend to do is to show the amount of unread messages in the sidebar of the administrator backend, which is #includein every page.
It may be because i'm accessing two different controllers everytime I enter any page on the admin backend.
I've looked into Including Sub-Views but i'm probably missing something silly or not understanding some key concept, help is appreciated!
Thank you!
Note: I think this is inconvenient and unrecommendable. This is just to answer the question, you can scroll down to see other answers or approach.
Controller
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar');
}
View
#php
$message_counter = App\Message::where('status', 0)->get();
#endphp
Messages <span class="badge badge-pill badge-primary ml-2"> {{$message_counter}} </span></span>
You can try like this way
In Route:
Route::get('/admin/sidebar', 'MessagesController#counter');
In Controller
use App\Message;
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar', compact('message_counter));
}
And your view is ok.. Try this and if it is not working please let me know....
With a View Composer: add this to App\Providers\AppServiceProvider#boot()
View::composer('admin.sidebar', function ($view) {
$message_counter = Message::where('status', 0)->get();
$view->with([''message_counter' => $message_counter]);
});

Route Not Hitting Controller

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

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'));
}

Store method not working using resource route

I am having trouble figuring out why my data is not being posted and stored in my database. I have used the resource routes for another form and it works fine, but here for some reason it won't work. Clicking submit just seems to refresh the page, no errors to work from!
So I have a form which gets the workout routines from a database, and on submission I want this to create a new Workout "session" in my database table (called "Workouts"). The form is this:
{{ Form::open(array('url' => '/')) }}
<div class="form-group">
{{ Form::text('workout_name', Input::old('workout_name'), array('class' => 'form-control', 'placeholder' => 'Session Name')) }}
</div>
<div class="form-group">
{{ Form::select('routines', $routine_names, null, array('class' => 'form-control')) }}
</div>
{{ Form::submit('Select Routine', array('class' => 'btn btn-success pull-right')) }}
{{ Form::close() }}
In my HomeController I have this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Routine;
use App\Workout;
class HomeController extends Controller
{
public function index()
{
$routines = Routine::all();
$routine_names = Routine::lists('routine_name');
return view('workout')->with(array('routines'=>$routines, 'routine_names'=>$routine_names));
}
public function store()
{
$workout = new Workout;
$workout->workout_name = Input::get('workout_name');
$workout->save();
}
}
I have a model created for the Workout, and the route for this page is the following:
Route::resource('/', 'HomeController');
I can't figure out where I'm going wrong. The index method in my controller is working, as it is returning the correct view with the data I need. The form also looks OK I think, as I'm posting to the same page, but submitting doesn't seem to carry out the code I have in the store method of the HomeController.
Any help would be appreciated!
Thanks :)
Change your route declaration from:
Route::resource('/', 'HomeController');
To something like this:
Route::resource('/workout', 'WorkoutController');
If you are using the resources controller creator command of php artisan then all the specific routes are created for you. To see all listed routes you can type , php artisan routes. This will show you RESTFUL routes even for your POST method .
And also even you did not created the resources controller and did made the routes with manual way then you can create ,
Route::POST('/workout' , SomeController#post);
I am trying to say , you have to use the different POST method for the form submission .
Hope this will solve your problem . Thanks.

Resources