I would like to understand how to associate more methods to my route. For example:
Route::get('/dashboard', 'DController#showX')->middleware('auth');
Besides showX() I have another function called showY() that I would like to associate with the route, but if I rewrite it twice it doesn't go, how can I solve the problem?
Controller:
public function showY(){
$name=Auth::user()->name;
return view('dashboard',['name'=>$name]);
}
public function showX(){
$y= Y::all();
}
There is no way to do it from the route like that. How would you handle two return values?
Judging by the controller methods, maybe you want to use the value of showX in showY?
The way I see to handle this would be to have one method in the route:
Route::get('/dashboard', 'DController#show')->middleware('auth');
and have it fire both of your other methods:
public function show() {
// decide what to return
$xValue = $this->showX();
return $this->showY($xValue);
}
protected function showY($y){
$name=Auth::user()->name;
return view('dashboard',['name' => $name, 'y' => $y]);
}
protected function showX(){
$y= Y::all();
}
Related
In my Model I have the function
App\Akte.php
public function lvs() {
return $this->hasMany('App\lvs', 'lv_id');
}
In my Controller I call
public function index()
{
$aktes = \App\Akte::all();
return view('admin.akte.index', ['aktes' => $aktes]);
}
And i´d like to extend my collection $aktes with the lvs table. Can somebody explain how to do this?
So my result should be a collection in which every single element of "Akte" has its Many collections of lvs in it..
If you also want the relationship loaded, just use:
$aktes = \App\Akte::with('lvs')->get();
See that. May be usefull for your needs.
https://laravel.com/docs/5.7/eloquent-relationships
public function index()
{
$aktes = \App\Akte::->all()->where('lvl', 2);
return view('admin.akte.index', ['aktes' => $aktes]);
}
somthing like this ...
I have a data that I want to return in all of my controller methods and I thought that'd be cleaner to return from the controller's constructor; because currently it's just repetitive return code.
protected $data;
public function __construct() {
$this->data = "Test";
}
public function index() {
// Stuff
return view('test')->with([
'testData' => $this->data
// other view data
]);
}
public function store() {
// Stuff
return redirect()->back()->with([
'testData' => $this->data
// other view data
]);
}
This is just a pseudo example.
Yes, that's possible. It's done exactly in the same manner you showed.
However, I don't think that's the best way to do it. You might want to take a look into ViewComposers, which help provide a data set to a view (or multiple views) right after the controller but before the view is finally provided to the user.
You could just write a controller method to append the data property for you:
protected function view($name, $data = [])
{
return view($name, $data + $this->data);
}
public function index() {
...
return $this->view('view', ['other' => 'data']);
}
You can use ViewComposer which allow you to attach data to the view every time certain views is rendered
namespace App\ViewComposers;
class DataComposer
{
protected $data = ['1', '2', '3']; // This data is just for sample purpose
public function compose(View $view)
{
$view->with('data', $this->data);
}
}
And to register this composer with the list of all views on which the data must by attached add this code in the boot method off the AppServiceProvider
View::composer(
['view1', 'view2', 'view3', '....'],
'App\ViewComposers\DataComposer'
);
create a after middleware However, this middleware would perform its task after the request is handled by the application
Yes, it is very much possible with the code you have
[EDIT]
If you wish to remove ['testData' => $this->data] from all controller methods, then you can use view composers.
View composers are linked to one view. So for that view, if you have the same set of data to be passed all the time, use view composers!
Question might be unclear. here's the explanation. Let's say I've:
Route file:
Route::get('testing', 'someController#functionOne');
Route::get('testingtwo', 'someController#functiontwo');
Controller file:
public function functionOne() {
$this->data = generateReallyBigArray();
return redirect('testingtwo');
}
public function funtionTwo() {
// Here $this->data is lost. obviously 'coz this controller file got reinstantiated for #functionTwo
return view('someview', ['data' => $this->data]);
}
$this->data is lost the moment testingtwo is hit. How do I pass this data across different route requests? Or if there're other ways of doing it.
I was thinking of doing this:
public function functionOne() {
$this->data = 'somedata';
return $this->functionTwo();
}
public function funtionTwo() {
// Here $this->data is lost. obviously 'coz this controller file got reinstantiated for #functionTwo
// even this doesn't work. Exception: Method get does not exist
return Route::get('testingtwo', function() {
return view('someview', ['data' => $this->data]);
});
}
use with() to send data through session -
public function functionOne() {
$this->data = 'somedata';
return redirect('testingtwo')->with('data', $this->data);
}
Or you could flash() the data for using on next request.
$request->session()->flash('data', $this->data);
the best way for that is
traits
trait Data{
public function getData() {
// .....
}
}
and in your controllers write
use Data;
you can use traits over controllers
or
You can access your controller method like this:
app('App\Http\Controllers\controllerName')->getDataFunction();
This will work, but it's bad in terms of code organisation (remember to use the right namespace for your ControllerName)
how can i use same route for two different controller function methods in laravel
first controller
public function index()
{
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
second controller
public function index()
{
return view('search.index');
}
i want to use these two different controller functions for one route.
This is my route name
Route::get('/computer', [
'uses' => 'ComputerProductsController#index',
'as' => 'computer.list'
]);
laravel needs somehow to identify which exactly method you want. for example you can pass the parameter, which will identify which method to call.
public function index(Request $request)
{
// if param exists, call function from another controller
if($request->has('callAnotherMethod')){
return app('App\Http\Controllers\yourControllerHere')->index();
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
You can't. If you want to add search functionality to your first controller's index page, you should determine which page to show inside your controller.
A possible example controller:
public function index(Illuminate\Http\Request $request)
{
// If the URL contains a 'search' parameter
// (eg. /computer?search=intel)
if ($request->has('search')) {
// Do some searching here and
// show the search results page
return view('search.index');
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
So I have a Laravel Controller (MainController.php) with the following lines:
...
public function _settings_a(){
return view('_settings_a');
}
public function _settings_b(){
return view('_settings_b');
}
public function _settings_c(){
return view('_settings_c');
}
public function _settings_d(){
return view('_settings_d');
}
public function _staff_a(){
return view('_staff_a');
}
public function _staff_b(){
return view('_staff_b');
}
public function _staff_c(){
return view('_staff_c');
}
...
And my routes.php is as follows:
Route::any('_staff_a''MainController#_staff_a');
Route::any('_staff_b''MainController#_staff_b');
...
etc.
It seems there are a LOT of lines and a LOT of things to change if I change my mind...
I was wondering if I can have some regex in routes.php and an equivalent regex in MainController.php for handling routes that begin with an underscore (_)?
Can any Laravel experts share some tips/suggestions? I'm quite new to the framework.
Sure - just add it as a parameter. E.g. like this:
Route::any('_staff_{version}', 'MainController#_staff');
public function _staff($version) {
return view('_staff_'.$version);
}
I don't think you need to mess with regex. You can use implicit controllers Route::controller() which isn't the BEST solution, but will do what I think you are wanting.
So instead of
Route::any(..)
you can do
Route::controller('url', 'MainController');
So your route to whatever 'url' is will send you to this controller. Follow that with a '/' and then add whichever method in the controller you want to call.
Here is an example:
My url: 'http://www.example.com/users'
// routes.php
Route::controller('users', UserController');
// UserController.php
public function getIndex()
{
// index stuff
}
Now I send a request like: http://www.example.com/users/edit-user/125
// UserController.php
public function getEditUser($user_id)
{
// getEditUser, postEditUser, anyEditUser can be called on /users/edit-user
// and 125 is the parameter pasted to it
}
Doing it this way should allow you to be able to just send a request (post or get) to a url and the controller should be able to call the correct method depending on the url.
Here are some more rules about it: http://laravel.com/docs/5.1/controllers#implicit-controllers