Laravel: Model not working if I named it "Auth" - laravel

Why when I name my model Auth it not working at all? But when I change name to different model work correctly?
Not working:
<?php
class Auth extends Eloquent {
public static function check()
{
return "working";
}
}
Working:
<?php
class MyAuth extends Eloquent {
public static function check()
{
return "working";
}
}

Laravel already has a built-in Auth class.
You could remove the line:
'Auth' => 'Illuminate\Support\Facades\Auth',
from app/config/app.php if you're not using Laravel's built-in Auth class.

Auth is a predefined class for user authorization in Laravel. To name a second class Auth too, you will need to put the new one in a different Namespace

Related

Fresh laravel lumen cannot find controller reside under subfolder of controllers

Fresh laravel lumen 5.8 installed but controller name cannot be found in route
I created UserController in App\Http\Controllers\User
User Controller Content:
namespace App\Http\Controllers;
class UserController extends Controller {
public function index() {
return 'User list';
}
}
Loaded in route:
$router->get('/user', 'User\UserController#index')
Error:
Class App\Http\Controllers\User\UserController does not exist
I also tried with absolute namespace App\Http\Controllers\User\UserController#index but it still doesn't work.
It works if I don't put UserController in User folder
Change your UserController's namespace to:
namespace App\Http\Controllers\User;
class UserController extends Controller
{
// ...
}
Try changing namespace of the controller to
namespace App\Http\Controllers\User;
class UserController extends Controller
{
// ...
}

Pass Customer FormRequest to GET in Laravel

I'm curious is that possible to pass (as type-hinted) of custom class that extends FormRequest to be passed in to action within GET request ?
for example:
at routes/api.php
Route::get('/schema', '\App\Http\Controllers\TestController#getSchema');
then I have App\Http\Requests\SchemaRequest.php
and at controller, I want get this request from route within GET method.
class TestController extends Controller {
public function getSchema(\App\Http\Requests\SchemaRequest $request) {
// do other stuff here
}
}
I've tried to look deeper and doing some hack but nothing success yet?
Is that possible?
Any input would be appreciated, and thanks for reading
How about this workaround?
Route::get('/schema', 'TestController#getSchema',namespace =>'App\Http\Controller');
class TestController extends Controller {
public function getSchema(Request $request) {
// do other stuff here
}
}

How do I do the type-hint 'automatic injection' custom class laravel

Below is the EmailNotifier Class
class EmailNotifier
{
public function notify()
{
echo 'Sending payment notification via email' ;
}
}
Below is my AppServiceProvider
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->make(App\EmailNotifier::class); // resolve the EmailNotifier Class
}
}
Below is billing class
class Billing
{
protected $notifier;
public function __construct(EmailNotifier $notifier)
{
$this->notifier = $notifier;
}
public function pay()
{
// Process the bill payment
$this->notifier->notify();
}
}
and in my controller I did
$data = new Billing(1);
As you can see I already resolve the EmailNotifier Class at the AppServiceProvider Class but when I call that like the code above, it throws an error said 'must be an instance of EmailNotifier'
and based on the laravel documentation, it's stated that :
you may "type-hint" the dependency in the constructor of a class that
is resolved by the container (for the automatic injection)
how do I achieve automatic injection for the type-hint in laravel ?
Use $data = resolve(Billing::class); instead of $data = new Billing(1); and you can remove $this->app->make(App\EmailNotifier::class); // resolve the EmailNotifier Class from service provider's register method.

Laravel 5.3 Custom Class

Create a custom class in laravel when I am call in controller construct then
Auth::user() not return any data
When call from in a function then it's work
Class Code
<?php namespace App\Libraries;
use App\User;
use Auth;
use Illuminate\Support\Facades\DB;
use App\Friends;
class AppLibrarie
{
private static $friends_ids = array();
public function __construct()
{
self::$friends_ids=Auth::user();
}
public function getfriends(){
return self::$friends_ids;
}
}
And Controller
<?php
namespace App\Http\Controllers;
use App\Libraries\AppLibrarie;
use Illuminate\Http\Request;
use App\Http\Requests;
class LiveController extends Controller
{
protected $lib;
public function __construct(AppLibrarie $appLibrarie)
{
$this->lib = $appLibrarie;
}
public function search(Request $request){
return response()->json($this->lib->searchdata($request->get('query')));
}
}
Accessing authenticated user sessions has been deprecated in Laravel 5.3. Here is the paragraph in the upgrade guide
In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.
As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:
You will need to rethink your Authentication structure a bit if you are to upgrade

Call to undefined method Illuminate\Support\Facades\Log::save()

I created 'Log' class in app/models :
class Log extends Eloquent {
public function user() {
return $this->belongsTo('user');
}
}
When i try to save log object in my controller i got this error (Call to undefined method Illuminate\Support\Facades\Log::save() ) I thik because in (app/config/app) in providers laravel define Log class => 'Log'=> 'Illuminate\Support\Facades\Log',.
How can i resolve this problem without change class name ?
Yes the problem is indeed a conflict with the Log facade alias. To fix it use namespaces:
<?php namespace YourApp;
class Log extends Eloquent {
public function user() {
return $this->belongsTo('user');
}
}
And then you can use your class like so:
$log = new YourApp\Log();
You could of course rename the alias name, but namespacing your classes is a much better approach.

Resources