Access tables names in middleware in a single transaction - laravel

Let's say CRUD operations are performed on three tables in a single controller method.
Is it possible for me to get all the tables names on which CRUD operations are performed in the middleware.
As of now, when I submit a form I can only access those inputs from the request in the middleware, but when I insert three records into three different tables in a single controller action am not able to access the table names in the middleware.
Any help is much appreciated.

I don't think that it is possible to get table name automatically. You will have to right custom logic in middleware for that controller action. Because the response object we get in middleware after the execution of controller method is html, json, etc response which is to be sent to client.

Related

One model, one table different user types and different controllers, how to handle policies?

I have one model user and one table against that model users I have different roles for user as patient, hospital and so on, each user have different fields and values to enter during registration so I have created different routes and controllers for different roles.
Now I'm facing problem during authorization process how I can authorize. I have only one model user so I can create only one policy called UserPolicy and use the $this->authorize method in the UserController.
I have also other controllers as PatientController, HospitalController which all bound to the one table and model called user and fetching the record only based on the user type. Now how can I create the policies for them and use the $this->authorize method in the Hospital, Patient controllers?
you are using the same table and you require different data from each kind if user ?
anyway if that the situation , you can create a type field in the users table , then create 2 middlewares , in your middleware check the type of the user then throw an exception or make him pass

is it possible to use single view for multiple methods and controllers with different data?

I need to use single view multiple time with different methods as well controllers all methods are sending different data and on submit it effect to different table of database.
suppose i have two methods, method1 and method2
method1 has data like {123,456,789} and go to view with this data and on submit, need to make changes in table1 of my database.
method2 has data like{321,654,987} and go to view with this data and on submit, need to make changes in table2 of my database.
i don't have any idea about to reuse the view with different data, if it is possible then please help me.
Surely you can use same view for different Laravel methods which will not make any difference what so ever.
Eg.
In UserController#login method you can return view('login');
and the same view can be returned in CustomerController#login method also.
you just need to change the Form Action in login.blade.php based on the Controller who is returning the view.
public function func1(data){
return view("document",[data]);
}
public function func2(data){
return view("document",[data]);
}

How to save a form into different tables

I am new with laravel
I used the default Authentication and I added one other input that I wanted it to be saved on an other table how can I do that
If you're talking about how to save registration form input into different table (not to users) when user is registering, add something like this to create() method in app\Http\Auth\RegisterController.php:
Profile::create(['custom_column' => $data->custom_form_field]);
I think in this situation will be better to use connections in model. If this data is about user of course. Also there is important thing about submit method(simple submit or AJAX).

Laravel multiple models in controller

This question regards the structure of applications when using laravel.
I have a view for making a sale/purchase from a company. This single view contains a client search, product list, service list and a staff list. Each of the items listed above has their own model. In the view, i would say search for a client which will call a function within the controller and populate a list. Same for the products and services etc etc.
What confuses me is that for the client search i would click a button that would fire to a url like /clients/search/search string, which would return the array of clients to display on the page. This function seems as if it would be appropriate within the client controller. I am unsure as to how i would be able to maintain the information from the client search and other parts of the sale to then submit it all together under one single controller (let's call it InvoiceController).
Can controllers share the functions from other controller? Do i simply store the information in a session variable? Do i simply put all relative functions to this sale under the InvoiceController?
Thanks for any help!
I would do a search for clients against the client controller, then when you select the client add this (client_id) to your invoice form. then save this with other invoice info via the invoice controller
After much deliberation, it appeared that AJAX was the simplest answer. In this case i used Angular JS and had that running a controller to fetch each segment of the Invoice, then use the main InvoiceController in Laravel to fire the final update.

RESTful API - validation of related records

I implementing RESTful API service and i have a question about saving related records.
For example i have users table and related user_emails table. User emails should be unique.
On client side i have a form with user data fields and a number of user_email fields (user can add any number of fields independently). When the user saves the form i must first make query to create record in users table to get her ID, ​​and only then i can make query to save user emails (because in now i have id of record which come with response after saving user data). But if user enters not unique email in any field then the request will fail. So I create a record in the users table but not create record in user_emails table.
What are the approaches to implement validation of all this data before saving?
This is nor related restful api but transactional processing on the backend. If you are using Java, with JPA you can persist both element in the same transaction then you can notice if there is a problem and rollback the entire transaction returning a response.
I would condense it down to a single request, if you could. Just for performance's sake, if nothing else. Use the user_email as your key, and have the request return some sort of status result: if the user_email is unique, it'll respond with a success message. Otherwise, it'd indicate failure.
It's much better to implement that check solely on the server side and not both with the ID value unless you need to. It'll offer better performance to do that, and it'll let you change your implementation later more easily.
As for the actual code you use, since I'm not one hundred percent on what you're actually asking, you could use a MERGE if you're using SQL Server. That'd make it a bit easier to import the user's email and let the database worry about duplicates.

Resources