Why i have NotFoundHttpException???
My code is working fine, except for this route.
I have another route which looks like this and it's working fine.
I tried to remove (id) and write id in controller
function npa()
{
$news = Post::where('category_id', 28)->orderBy('created_at', 'desc')->paginate(12);
return view('npa', compact('news'));
}
Route::get('/npa/{id}', 'HomeController#npa');
Because your id is not optional in the route change your route to this:
Route::get('/npa/{id?}', 'HomeController#npa');
Pass Id to your route -> Route::get('/npa/{id}', 'HomeController#npa') as /npa/28
And change your function as
function npa($id)
{
$news = Post::where('category_id', $id)->orderBy('created_at', 'desc')->paginate(12);
return view('npa', compact('news'));
}
Related
I have two routes for web middleware in my laravel app listed below:
Route::get('package/{slug}','GetPublicController#tourDetail')
->name('single');
And
Route::get('/trips/{category}','GetPublicPageController#getBycategory')
->name('getBycategory');
The first route works well but second route doesn't. The second route is in conflict with the first route as it tries to get view of first route, causing error and app to breakdown.
Method for 1st route:
public function tourDetail($slug)
{
$tour = Tour::where('slug', '=', $slug)
->first();
$itineraries = Itinerary::where('tour_id','=', $tour->id)
->orderBy('id', 'asc')->get();
$depature_dates = $tour->datePrice()->FixedDates($tour->id, date('m'),date('Y'))->get();
return view('public.tour.tour-detail')
->withTour($tour)
->withItineraries($itineraries)
->withDepatures($depature_dates);
}
And method for 2nd route
public function getByCategory($category)
{
$query = Tour::whereHas('category', function($r) use($category) {
$r->where('tcategories.name','=', $category);
});
return view('public.pages.category-list')
->withResults($query);
}
It would be very helpful if anyone could suggest the best approach to resolve this issue.
Thank you.
You have to call ->get() on the query in getByCategory.
I am trying to 'redirect to' from one controller method to another through the route. However, I want to pass some data as well. I tried Session::get('name') but doesn't seem to work. This is what I tried:
public function before() {
return Redirect::to('later')->with('x', 'y');
}
public function later() {
dd( Session::get('x') ); // null
dd( $x ) // not working
}
My route is like classic:
Route::get('/later', 'TheController#later')->middleware('auth');
What am I missing?
Instead of Session::get('x') try with session('x') as below.You can check for it using if (session()->has('x'))
public function later() {
dd( session('x'));
}
I have a route of GET type with some parameters. For example
Route::get('/my-route/{id}',array('uses'=>'myController#myAction'));
I want to check value of the parameter id and if this id=1 then redirect to another route else continue with it. What I am doing is like this
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
//What should I do here so my route works as before.
}
});
In else part I want my routes to myController#myAction along with parameters.
Thanks
You can do it as:
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
return app()->call(myController::class, ['id' => $id], 'myAction');
}
});
The easiest way to get this to work and route to your controller is put your Route back to the way it was and then the if statement to the top of your controller condition:
public function myAction() {
if ($id == 1) {
return Redirect::to(URL::route('my-another-route'));
}
to the top of your controller method.
Also, if you're only using uses => 'Controller#method' on with your route you can just do:
Route::get('/my-route/{id}','myController#myAction');
Hope this helps!
I have some problem here.
I wanna view all data sort by "kelompok".
*kelompok means group
This is the code :
Controller
public function pengelompokan()
{
$view = DB::table('tb_siswa')->where('id', $kelompok)->get();
return view('pengelompokan')
->with('view', $view);
}
Route
Route::get('kelompok', 'belajarController#kelompok');
You can use the groupBy collection method:
$view = DB::table('tb_siswa')
->where('id', $kelompok)
->get()
->groupBy('kelompok');
Edit
Based on your comments, you could do this:
Route::get('kelompok/{groupId}', 'belajarController#kelompok');
public function pengelompokan($kelompok)
{
$view = DB::table('tb_siswa')
->where('id', $kelompok)
->get()
->groupBy('kelompok');
return view('pengelompokan', compact('view'));
}
Following is the code to resolve this
public function pengelompokan()
{
$view = DB::table('tb_siswa')->where('id', $kelompok)
->groupBy('kelompok')->get();
return view('pengelompokan')->with('view');
}
You can access groupBy data using a variable $view on blade as well.
I am using Routes but you can apply it on your Controller#show
Route::get('tutorial/{id}', function($id){
$tutorial = Tutorial::findOrFail($id);
return view('tutorial.show')->with('tutorial', $tutorial);})->name('show-tutorial');
and Also Check on your show.blade.php
I am calling getting_started route after successfully login :
protected $redirectTo = '/getting_started';
Here is my getting_started route code :
Route::get('/getting_started','UserController#getting_started');
And controller code :
public function getting_started()
{
$id= Auth::id();
$user = DB::table('user_profiles')->where('user_id', '=', $id)->first();
if($user->dashboard_access == 0)
{
DB::table('user_profiles')
->where('user_id', $id)
->update(['dashboard_access' => 1]);
return view('user.getting_started');
}
return view('user.dashboard');
}
It works perfectly and show in url :
http://localhost:8080/getting_started
Now I actually want that if user.dashboard view is call it show in url like :
http://localhost:8080/dashboard`
And on getting_started view show :
http://localhost:8080/getting_started
It is possible to call dashboard route instead of :
return view('user.dashboard');
My dashobard route is :
Route::get('/dashboard',['middleware' => 'auth', function () {
return view('user.dashboard');
}]);
What I understand it is that you are looking for is this function
return redirect()->route('dashboard');
It's my understanding of your question which can be wrong. Maybe you are asking something else.
That called Redirection and especially you want to Returning A Redirect To A Named Route, you route called user.dashboard so you could redirect to it using redirect()->route(route_name) :
return redirect()->route('user.dashboard');
Hope this helps.