How to show page via resource controller laravel 5? - laravel

Home.blade.php is starting when I route it directly but not when I call it from resource controller !!
Route::resource('list','listcontroller');
And I call it in index method :
class listcontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return View('list.Home');
}

Change your index function to the following-
public function index()
{
return View('Home');
}
And you will get the corresponding view at base_url/list
or if you want to get with /list/home url then change your route to-
Route::resource('list/home','listcontroller');
If you want to set a base_url othen you can use route prefix
Route::group(['prefix' => 'list'], function () {
Route::resource('home','listcontroller');
});

Any view should be addressed by their path from view folder separated by . (Dot) and with correct character case.
For example if you have home.blade.php in view folder you should call it by view(“home”).
Beside that you can address to any routes by “route” helper function and passing route name to it.
As #amini.swallow said you can reach to your route list by running php artisan route:list command.
In your case you can create link like this :
<a href=“{{route(“list.index”)}}”>click here</a>
Hope it helps.

1)Your controller name is not standard write listController.
2)make sure about your route names:
php artisan route:list
3)in blade write
and try it

Related

Route is working when referred but when I go to that provided link it will not find the controller function in laravel

I have added a custom function to save pictures and added the route as well and when I reference the route it does work but when visiting it gives error that the function can not be found with reflectionexception error ReflectionException
Function () does not exist
Controller
/**
* show the form for uploading profile picture
*
* #return \Illuminate\Http\Response
*/
public function changePicture()
{
return view('usersinformation.profilepicture');
}
route -- web.php
Route::get('usersinformation/changePicture',[usersinformationController::class, 'changePicture'])->name('usersinformation.changePicture');
Route::post('usersinformation/savePicture', 'usersinformationController#savePicture');
you should add this line to above of web.php file.
use App\Http\Controllers\usersinformationController;
(if your controller is in default path)
but if your controller path is different you should modify and add your path to the code.
the issue resolved by adding below route.
Route::get('usersinformation/saveePicture', [
'as' => 'usersinformation.savePicture',
'uses' => 'usersinformationController#savePicture'
]);

Route Model Binding not working Laravel 6

Route Model Binding not working
model: Accommodation
Controller: AccommodationController
Route::resource('accommodation', 'AccommodationController');
Working
public function index()
{
$accommodation = Accommodation::get();
return view('admin.accommodations.index', compact('accommodation'));
}
Not working
public function index(Accommodation $accommodation)
{
return view('admin.accommodations.index', compact('accommodation'));
}
You can see your route list in your php artisan terminal to avoid confusion.Use command.
php artisan route:list
Then see in name list what was name , then put in your route function.
However, I think your route will be
admin.accommodation
remove 's'.

send get request to specific file

I have a file called: test.php located in public/inc/test.php. Through jQuery, I can make a get request through jQuery ($.get), by typing:URL: "public/inc/test.php?" + param1` etc.
I want to do the same, but through Laravel. I want to have a route called: /sendparams/{param1}/{param2}, and do a get request to the test.php file from inside a route in web.php and pass the params variables.
How should the route be written?
edit:
by implementing the classes in laravel as external classes, this is no more needed.
you have in your laravel project a file named web.php inside the routes folder.
Define within the web.php file the below code:
Route::get('/sendparams/{param1}/{param2}', 'ControllerName#index');
Perform the below command to create the controller
php artisan make:controller ControllerName
Open ControllerName (app/Http/Controllers/ControllerName)
Your new controller has an structure like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MailController extends Controller
{
public function __construct()
{
}
/* This is the method executed in your route defined in web.php*/
public function index(Request $request) {
$param1 = $request->param1;
$param2 = $request->param2;
$allParams = $request->all(); // returns an associative array with all params
}
}
you can check your routes performed the below command
php artisan route:list
Regards

How to alter functionality of login controller in Laravel to pass a variable

I'm trying to set up my Laravel installation so that upon login it will populate the home page with information from my database. How can I compact a variable to send it to the home page?
I've tried declaring a variable like this in the login controller:
$post = Post::all();
But when i do that i get the following error:
syntax error, unexpected '$post' (T_VARIABLE), expecting function
(T_FUNCTION) or const (T_CONST)
Also how can i compact a variable to send to the home page? The only code that directs the user to the home page is this line: protected $redirectTo = '/home';
and I can't compact a variable in that line because compact requires parentheses like this: return view('/home', compact('post'));
What I'm trying to accomplish is to get the home page to display the users posts.
namespace App\Http\Controllers;
use App\Models\Post;
class HomeController extends Controller
{
public function index()
{
$posts = Post::all();
return view('home', compact('posts'));
}
}
Explanation
To accomplish your goal, you don't have to change the $redirectTo property of the LoginController. Because it's the URL that needs to redirect, it needs to stay the same because still, you need to redirect to the home page.
All you have to do is alter HomeController which Laravel has already defined for you. The index() method is the one that's taking care of how to show the home page. So as the answer says alter that function.
As after successful login you are redirected to home page, try to display the posts in HomeController#index and you do not need to make any modification to your login controller.
In your HomeController just import the Post model and return the posts to index.blade.php and display there, your HomeController will look like
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post; // path to your Post model
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('home', compact('posts'));
}
}
and in your index.blade.php, you can loop through your posts.

How to pass data from two different controller to single view in laravel5

I am new to Laravel5. There are two controllers Dashboard.php and Stats.php. I want to pass variables from both controllers to my single view DashBoard.blade.php
Guide me is this possible and also guide me the about the route of project.
here is my routing code:
` Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/DashBoard', 'DashBoard#ratingdata')->name('DashBoard');
Route::get('/Stats', 'Stats#statsdata')->name('Stats'); `
It works when views are different. but i need data on single view
About the routes, you can use the resources routing, it will create all your basic routes. Check this out (https://laravel.com/docs/5.8/controllers#resource-controllers)
A example of that, would be:
Route::resource('/dashboard', 'DashboardController');
Route::resource('/stats', 'StatsController');
To display data of multiple models in a view all you need to do is declare the model in the beggining of your controller.
Example:
<?php
namespace App\Http\Controllers;
use App\Stats;
use Auth;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$stats = Stats::all();
$dashboard = Dashboard::all();
return view('the_view_you_wanna_show', compact('stats', 'dashboard'));
}
Then, all you need to do now, is go to your view and display your data.
Example:
<h1>Displaying the names of my dashboard</h1>
#foreach($dashboard as $names)
{{$names->name}}
#endforeach
<h1>Displaying the stats name</h1>
#foreach($stats as $stats_names)
{{$stats_names->name}}
#endforeach
Tip:
When you create your models, go to your prompt and type:
php artisan make:model NameOfYourModel -a
It will create all the things you need to manipulate this model.
Below is the example of get route to fetch user data and display in view -
Route
Route::get('edit_profile', 'AdminController#editProfile');
In AdminController
public function editProfile(){
$data['title'] = 'Edit Profile';
$data['profile_info'] = Admin::where('id',1)->first();
return view("Admin::layout.edit_profile", $data);
}
You can print profile info in view as -
{{$profile_info->name}}

Resources