I want to create routes from database Data in Laravel - laravel

I want to retrieve data from the database and create the dynamic route.
For example, in Laravel web route file we write :
Route::get('/{slug}', function($slug){
return $slug;
});
So, I want to create this code after retrieving data from the database. and based on the data I can create the dynamic route with params or passing model data to the view.
And also how do I create dynamic view files. Like for different layout or from the database data.
Thanks in Advance.

Related

defining routes with parameters in laravel

in Laravel i want to do a page with a search box and a form (the route could be /products)
I want to retrieve information using search box typing id from a db and populate the form.
I request datas with the route for example /products/{id}
But in the controller i use the same function products($Request request) with if the id exists do something if no do other things, or there are two different functions?
Thx
Please go through the Laravel Resource Controllers.
To show list of products i.e. /products, create a index() method. To show a specific product i.e. /product/{id}, create show() method.
Probably is better use the same function, if id exist, return the page with the values filling the form, if not returns the same page but with a message showing that product doesn't exist

Accessing Auth'd user data throughout

I am looking to access the Auth'd users' data throughout my views. This information needs to come from a DB query so I can join in various other tables to get the data I need.
The view structure I am working with is as follows: (layout->dashboard). "Layout" being the generic html bits, parent. and "dashboard" being the page specific content.
My first attempt at passing data from the controller to the view outlined that I was only able to access the variable from the child view (dashboard) and not (layout) which I did presume beforehand. My question is, what is the best way to pass around user data, from a DB query, to any view I need it in.
In this one scenario, it is using a peice of data to retrieve the users avatar in the nav bar, found in "layout".
Many Thanks,
Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command:
php artisan make:auth
to make auth interface.
Check Laravel Doc:authentication
And then, try below...
Query DB like this...
//Controller
$users = DB::table('users')->get();
return view('layout', compact('users'));
After get the users collection, then send to view Blade(in the html file) like this...
//`Layout.blade.php` as View file like this..
<div class="container">
#foreach($users as $user)
{{$user->name}}
{{$user->"any you want"}}
#endforeach
</div>
#displaying-pagination-results
Enjoy coding~!! :)

How laravel post redirection works?

I have some confusion in laravel post method redirection.
I have to post data from form. Once the data has been post, I have to retrieve the data and store it into the db and get some data related to the post data and pass the data to laravel.
Here, I have used the redirect() function to redirect to the particular route.
When I redirect with data mean it not get the data only way to use the session to get the data
I mean push the data in session and then retrieve in view.
how to pass the data to another view when post method ?
you can use with() method of laravel to send the data to another view as
return Redirect::route('your_redirect_url')->with('data' => $data);
But it will also store data in the session only, so you can access it as
Session::get('data')

Magento: Where would I place that functionality?

I have a Magento form with a button and some fields. The button click calls a controller and in the controller I query the DB (using fetchAll) and create a csv file from the results.
Whats the best place to store the DB action and the CSV file creation? In a model maybe?
Thanks!
I would create a model, a resource model and a resource model collection for the data you need to export. In the collection I would define functionality that queries data from one or many (if necessary) tables . In the model itself I would create a function that fetches that collection and saves it into a CSV file. The controller action would contain only the model initialization and a call to the export function.

How does my MVC controller query one database table before writing to another?

I am working on a custom Joomla MVC component.
My view has a form where the user enters an ID. I have retrieved the ID ($input_id) in the controller. Now I need to query the database to get the name WHERE ID = $input_id, then write the name to a different database table.
Can this all be done within the controller or do I have to pass my variables to the model somehow? Not sure of the correct way to achieve this within the MVC framework.
All data and data manipulation should be done in the model (e.g. model your data). The controller is there to determine the path of execution and which methods should be called (e.g. the manager aka controller determines what needs to be done).
Have a look at this tutorial which will help you understand MVC for Joomla better by taking you through the development of a simple component.

Resources