How can I Simplify this code of Laravel 8? - laravel

I have this code in routes, is it possible to simplify it?
Thanks
Route::get('/post1', function () {
return view("post1");
})->name("/post1");

There is nothing wrong with that code, the only way you can "simplify" that code, better to say "abstract it" is by creating a controller with a method that returns the view.
In your case, if your route is very specific you can create a single action controller with the command:
php artisan make:controller PostController -i.
Then in the controller:
public function __invoke(Request $request)
{
return view("post1");
}
And in your routes file:
Route::post('/post1', PostController::class);
More info in the Single Action Controller docs
and in the views docs

Related

bind a route to a method (controller) in laravel7

I cannot bind the "contact" route to the "contact" method which is existing in the "TestController.php" controller
You don't need to include the file extension when you define routes, so your route should be:
Route::get('/contact','TestController#contact');
Then make sure you have a controller with that name in your controller directory:
app/Http/Controllers/TestController.php
namespace App\Http\Controllers
class TestController extends Controller
{
public function contact()
{
// your code
}
}
You can see a working demo here.
I usually find running php artisan route:clear will solve these issues sometimes.
Run Command php artisan optimize will solve issue

Creating a route to custom function in controller in Laravel

I still can't understand why I can't point my blade to the custom function I made in my controller. I create a route like this,
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
and in my controller I have this,
public function storeInitialItems()
{
return view('orders.storeInitialItems');
}
but when I run the page, storeInitialItems.blade.php, the error seems calling the show() function of my controller.
Why is that happening?
update
Complete routes for ORDERS
Route::get('/orders','OrdersController#index')->name('orders.index');
Route::get('/orders/create', 'OrdersController#create')->name('orders.create');
Route::post('/orders', 'OrdersController#store')->name('orders.store');
Route::get('/orders/{order}/edit', 'OrdersController#edit')->name('orders.edit');
Route::post('/orders/{order}', 'OrdersController#update')->name('orders.update');
Route::delete('/orders/{order}', 'OrdersController#destroy')->name('orders.delete');
Route::resource('orders', 'OrdersController');
Route::put('orders/{order}/pub', 'OrdersController#publish')->name('orders.publish');
Route::put('orders/{order}/cancel', 'OrdersController#cancel')->name('orders.cancel');
Route::put('orders/{order}/delivered', 'OrdersController#delivered')->name('orders.delivered');
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
Route::get('/orders/{order}/delivery', 'OrdersController#viewdeliveryItems')->name('orders.delivery');
Route::get('/orders/acceptDelivery', 'OrdersController#acceptDelivery')->name('orders.acceptDelivery');
Add your orders.storeInitialItems route
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
before,
Route::resource('orders', 'OrdersController');
or add some extra path with your storeInitialItems
Route::get('/orders/storeInitialItems/add-some-extra-path', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');

laravel route issue .the page you are looking for could not be found

I want to show data from the database when I press the button in one view it displays me the specific data by the id in the other view this is my first view.
In my controller:
public function show($id) {
$symptoms=symptoms::all();
foreach ($symptoms as $symptom) $symptom=symptoms::find($id);
return view('front.second',compact('symptom'));
}
My problem is when displays the second view it can't find the page and the route just display the id not the method/id :(
Try this:
public function show(symptoms $symptom)
{
return view('front.second', compact('symptom'));
}
Or:
public function show($id)
{
$symptom = symptoms::find($id);
return view('front.second', compact('symptom'));
}
And read Laravel's Controllers Documentation carefully.
As you shared image, is clearly shows that you are using below route
{{route('show',['id'=>111])}}
Check your route by php artisan route:list and use right one, I guess you should use like
{{route('user.show',['id'=>111])}}
If you Controller name is UserController in resource

The return of view() in Laravel?

I am just learning Laravel 5.1 framework, I find a puzzling problem.
First, I create a model named 'Page', then I create a controller named 'HomeController', the method code is following:
public function index()
{
return view('home')->withPages(Page::all());
}
I cannot find 'withPages()' function, so I find helper function view() return \Illuminate\View\View, so I find 'vendor/laravel/framework/src/Illuminate/View/View.php', there is a "__call()", so I get it.
But I try to delete this function, my site is still normal.
did I find the wrong place? I am very puzzled.
... there is a "__call()", so I get it. But I try to delete this function, my site is still normal. did I find the wrong place? I am very puzzled.
Probably.
Laravel 'compiles' all it's core classes into a single file as a performance optimisation.
Try running php artisan clear-compiled and your site should start failing.
This is how I would do it -
public function index()
{
return view()->with('pages', Page::all());
}
If you want to use withPages method, you need to have a variable $pages set in the method.
So your method would look like:
public function index()
{
pages = Page::all();
return view('home')->withPages($pages);
}
Other two options:
public function index()
{
return view('home')->with('pages', Page::all());
}
or
public function index()
{
pages = Page::all();
return view('home')->with(compact('pages));
}
You can use any of these methods.

Dynamic Routes not found

I am having trouble getting dynamic routes to work in Laravel 4
Routes
Route::any('/browse/{$id}', 'BrowseController#showProfile');
Controller
<?php
class BrowseController extends BaseController {
public function showProfile($id)
{
return $car_id;
}
}
When I go to http://localhost:8000/browse/10018
I receive a not found error
Any Idea what is wrong? Sorry I am new to Laravel
You don't need a $ in the variable name in your route. Try using
Route::any('/browse/{id}', 'BrowseController#showProfile');
Also, you should add validation to only allow numbers:
Route::any('/browse/{id}', 'BrowseController#showProfile')->where('id', '\d+');
The problem is in {$id}, try only {id}

Resources