I'd like to add a table in resources\views\welcome.blade.php. So I add the html table code there and it works.
Then I try to edit app\Http\Controllers\HomeController.php to see a vardump in welcome.blade.php but nothing shows there, continue only my table.
Any ideas what I'm doing wrong? I'm new using laravel.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Model\ModelHome;
use App\User;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
$this->objUser=new User();
$this->objBook=new ModelFuncionarios();
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
dd($this->objUser);
return view('home');
}
}
Your Homecontroller doesn't use welcome.blade.php but home.blade.php. dd(...) is short for "dump & die" - it dumps the information and stops script execution.
Also, you need to be logged in/auth'd to use HomeController, you are redirected to welcome.blade.php before executing your index method.
Edit: if you want to "var_dump" information and not stop script execution but continue to show the view you can use dump() instead of dd()
When you do dd() the view will not load as this function ends the execution of the script.
Have a look here: laravel 8 dd function
Related
I have a code in HomeController.php in the Laravel framework, but I could not access the index/homepage it redirects to the login page. what is the blunder I am committing?
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
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()
{
$users = User::get();
return view('home', compact('users'));
}
public function user($id){
$user = User::find($id);
return view('user', compact('user'));
}
public function ajax(Request $request){
$user = User::find($request->user_id);
$response = auth()->user()->toggleFollow($user);
return response()->json(['success'=> $response]);
}
}
The answer is simple. In the __construct() method, you specified that every function under this controller must use the auth middleware. This will redirect to login page is the user is not logged in.
There are a number of ways to work around this, the first being to remove the __construct entirely. However, this is not so recommended.
Another would be to add an except() function
public function __construct()
{
$this->middleware('auth')->except('index');
}
This will allow the condition to apply anywhere else other than the index function.
Based on the code you posted, the homepage route is protected by the 'auth' middleware, which means users have to login to access the resource page (Home page), so you should first login and then it will direct you to the intended page. If you don't want the page to be protected, then you could either remove the $this->middleware('auth') in your constructor, or you could put the index() function separately in a different Controller file and leave it unprotected
you can also control it in the routes (routes/web.php).
Or as already mentioned with middleware: Assigning Middleware To Routes
Route Group
Use route grouping in your web.php file
This way you can determine middleware for specific routes, you can also use grouping inside route group also
And don't use middleware in controller, use it in web.php so you can have a proper idea that how many routes are using middleware
I was trying to register user using the Laravel Auth. First I got error with App\User not found then I fix it with App\Models\User it works. I don't really know what with Laravel 8 becuase I never had a problem with user registration with previous version. Then I got this problem
BadMethodCallException
Method App\Http\Controllers\HomeController::home does not exist.
I don't really know which code to provide since I don't even touch the default code of the Authentication/HomeController.
But I did changed the namespace in RouteProvider
protected $namespace = 'App\Http\Controllers';
web.php
Route::get('/home', [HomeController::class,'home']);
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
in your web.php top you need to import
use App\Http\Controllers\HomeController;
then u can use
Route::get('/home', [HomeController::class,'index']);
Or else
Route::get('/home', 'HomeController#index')->name('home');
use this here no need to import
Note - your error is showing u don't have home method in your controller so create home method or change the correct method by default it is index
Try Route::get('/home', [HomeController::class,'index']);
Using Laravel 5.6, I'm trying to get the number of received links a logged-in user may have in my application.
public function getReceivedLinksCount() {
return $count = App\Link::where([
['recipient_id', Auth::id()],
['sender_id', '!=', Auth::id()]
])->count();
}
So far, so good. Question is not about that piece of code, but where I can use it. I'd like to display this counter on the navigation bar of the website (Facebook's style) which is in my header.blade.php which is included in every page.
I'd like to do it with a clean code, of course. It seems like I need to use View Composers but I'm not sure it's the right way to do it, and not sure on how the code is supposed to look.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
view()->composer('layouts.header', function ($view) {
$view->with('variable_name', \App\Path-To-Your-Model::something());
});
}
You can share a value across all views by using View::share(), see docs
For example
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$linksCount = Link::getReceivedLinksCount();
View::share('linksCount', $linksCount);
}
...
}
This works well if you want to set the value everywhere. Personally, I would set the value in the constructor of a 'BaseController' that gets extended by other controllers. This makes the code more discoverable because most people would expect view values to be set in a controller. And it's also a bit more flexible if you plan on having a section of your app that doesn't require that value to be computed.
I am trying to create an application where i have a user activity log. I dont want to write a special function in all the pages. I just want to metnion it once and run on all the pages or controller where ever i go. Which keep showing on the header.blade.php. And i want to keep using that log all time.
I have a function with like this.
public function headerLogs()
{
$latestActivities = Activity::with('user')->latest()->limit(10)->get();
return view('layouts.header')->with('logs', $latestActivities);
}
How can i do that?
Laravel has built-in functionality for that: View Composers. They are what you use if you want some data to be in every view that is loaded (of course you specify which views exactly)
So from the docs we would create a service provider for our view composer:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ActivitiesComposerServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* #return void
*/
public function boot()
{
// Using Closure based composers...
view()->composer('layout.header.activity', function ($view) {
$latestActivities = Activity::with('user')->latest()->limit(10)->get();
$view->with('logs', $latestActivities);
});
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
//
}
}
And then you register your ActivitiesComposerServiceProvider service provider by simply adding it to the providers array in config/app.php.
So now you can simply #include('layout.header.activity') and the logs will show with no extra line of code in your controller or view
In your base controller:
view()->composer('layouts.header', function($view){
//your code for header logs which gives $logActivites.
$view->with('logs', $logActivites);
}
So, whenever your view layouts.header will be created, this $logActivites will be available.
Everyone had a situation when one variable had to be accessible in every view (blade) file. One possible solution – letting the service provider load our global view information. For Example, type this in your service provider.
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Using view composer for specific view
view()->composer('welcome',function($view) {
$view->with('latestActivities', Activity::with('user')->latest()->limit(10)->get());
});
}
}
Then you can access the latestActivities variable from all you view files
I'm working on Laravel framework, I have created a class under controllers/admin/PlacesController.php
I'm placing it in a name space controller/admin;
But as you can see below I can't use standart Laravel classes without "\"
Please see \View
class PlacesController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$places=\Place::all();
return \View::make('admin.places.index',compact('places'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return \View::make('admin.places.createOrEdit');
}
}
But I would want to use it as View without "\" how can I do this? It is really a problem to fix all View to \View
Thanks all.
You should import View class because it's in other namespace (root namespace).
Add:
use View;
at the beginning of your file, for example:
<?php
namespace yournamespacehere;
use View;
Now you will be able to use in your controllers return View instead of return \View
If you want more explanation you could look at How to use objects from other namespaces and how to import namespaces in PHP