Update controller data into laravel after middleware - laravel

I have one ProjectController with function getAll(). In getAll() I use eloquent ModelName::get() to fetch all projects and assigned to one variable $projectList.
I register one after middleware. In that middleware, I want to do some data filter on $projectList which I get in the controller.
Please help me how can I do that?

Related

how to use two functions from same controller in single page route using get in laravel

Am trying to use two different functions from one controller in a single page route
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
But the problem is the function alldata works where the function index doesn't
You can't have 2 GET routes with the same path.
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart/all','App\Http\Controllers\Frontend\CartController#alldata');
The /cart route is overwritten by the alldata(). So the alldata() is calling instead of index().
kindly remove the alldata()'s route and pass the data from index().
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
Try to manipulate your logic in controller rather than in route file.
Use conditional in controller function.

what middleware can what controller in Laravel cannot do?

while learning Laravel i came across this concept called middleware. I am wondering why we need middleware in the first place? We can implement the same logic in our controller too , then , whats the point of having middleware? what is it what we cannot do in controller and we need middleware for it?
If you have multiple controllers and you want to apply same condition on then, rather then writing same code in multiple files(Controllers). Write code in one file(middleware) and apply on all controllers.
Route::middleware('checkRole')->group(function() {
Route::get('user', 'UserController#index')
Route::get('order', 'OrderController#index')
Route::get('customers', 'CustomerController#index')
});

Laravel resource route doesn't display show in controller

According to the Laravel documentation:
This single route declaration creates multiple routes to handle a
variety of actions on the resource. The generated controller will
already have methods stubbed for each of these actions, including
notes informing you of the HTTP verbs and URIs they handle.
In my route I have this:
Route::resource('admin/companies', 'CompaniesController');
In my controller I have index, create, store, show etc.
Specifically in show I have this:
public function show(Company $company)
{
//
dd('hi');
}
I would expect when I hit this route:
http://127.0.0.1:8000/admin/companies/onecompany
for it to dd my response. Instead I get a 404 error. What am I doing wrong?
This route http://127.0.0.1:8000/admin/companies/onecompany is not a valid route that triggers resource functions according to Laravel documentaion.
The right URL that will trigger the show(Company $company) function is:
//This route will extract the `id` column from the model and show the required record.
http://127.0.0.1:8000/admin/companies/{company}
or
http://127.0.0.1:8000/admin/companies/{id}
Try an existing record in your database;
//Assuming there is a record with an id of 1
http://127.0.0.1:8000/admin/companies/1
cause of your problem seems related to Route model binding
,try it with id in uri.
to see list of your application's routes: php artisan route:list

Laravel register extra fields placement

I have some extra fields that need to be filled right after a user is registered.
Right now I've done it with an Event and couple of Listeners but I can't seem to find an elegant way to carry the data from the Registration page to the Listener so it can insert it in.
Should I use Events/Listeners or is there a better way (without changing the registerController).
I was thinking of getting rid of the Events and redirecting to another form right after registering where the other data to be inserted, but it's just couple of fields, so if there's a better way I would appreciate you sharing it.
Latest Laravel version with the default Auth installed.
I know you said without changing the RegisterController, but this way it doesn't change any of the logic inside it:
class RegisterController extends Controller
{
use RegistersUsers;
protected function registered(Request $request, $user)
{
// ... your code here
return redirect($this->redirectPath());
}
}
If your class has a registered() method it will be called right after a successful registration, so you can save the additional data there and then redirect the user.

Laravel 5 - Middleware get user ID and send to controllers

Hello guys !
I'm working on an API that has a middleware authenticating the user with a unique ID. After making sure that this user exists, I want to send his database ID to the controller coming next, whichever it is.
Is that a good idea ? Or should I get that ID somehow after the middleware finished ?
How do I do that ?
Thanks !
Is that a good idea ? Or should I get that ID somehow after the middleware finished ?
It depends on what you want to do and how you routes are declared.
The routing is one of the first thing initialized by Laravel. You cannot pass parameter at run time (correct me if I'm wrong).
Plus, the controllers called after all midlewares has done their work.
I cannot garanty it's the more "beautiful" way to do this, but what i'm use to do is using Session::flash() or Session::put() when I want to pass parameters to my controllers at run time.
I use Session::flash() if the parameter has a one request life time, and Session::put() when I want the variable be more 'consistent' across the whole application.
I don't know if I am clear or not, tell me :)
Well, as long as you don't send that ID passing through the HTTP protocol, you should be fine since the user won't be able to tamper with the data.
That said, if you are using Laravel's built-in Auth module, you should just do an Auth::user() call at the other controller and it will give you the authenticated user.
If that isn't an option, you should create a function in the other controller that accepts $id as a parameter. You can call that function from within the first controlling by constructing the second controller throug $secondController = App->make(SecondController) and then $secondController->receiverFunction($id)
If you want the currently-authenticated user available in your application, just add it your base controller:
<?php namespace App\Http\Controllers;
use Illuminate\Contracts\Auth\Guard;
abstract class Controller {
protected $auth;
protected $currentUser;
public function __construct(Guard $auth)
{
$this->auth = $auth;
$this->currentUser = $this->auth->user();
}
}

Resources