I am developing an application in laravel and when I perform the test with insomnia (post), it presents the following "Class illuminate\Support\Facades\Storage" not found .
follow my code below
WarningController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use illuminate\Support\Facades\Storage;
use illuminate\Support\Facades\Validator;
use App\Models\Warning;
use App\Models\Unit;
class WarningController extends Controller
{
public function addWarningFile(Request $request)
{
$array = ['error' => ''];
$validator = validator::make($request->all(), [
'photo' => 'required|file|mimes:jpg,png'
]);
if (!$validator->fails()) {
$file = $request->file('photo')->store('public');
$array['photo'] = asset(Storage::url($file));
} else {
$array['error'] = $validator->errors()->first();
return $array;
}
return $array;
}
}
Validator.php
<?php
namespace Illuminate\Support\Facades;
class Validator extends Facade
{
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor()
{
return 'validator';
}
}
Related
I made a socialite login using Google and Facebook, but in the SocialiteController section there is an error like the question above.
this is my SocialiteController
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Role;
use App\SocialAccount;
use App\User;
class SocialiteController extends Controller
{
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
$user = Socialite::driver($provider)->user();
$authUser = $this->findOrCreateUser($user, $provider);
Auth::login($authUser, true);
return redirect('/personal');
}
public function findOrCreateUser($socialUser, $provider)
{
$socialAccount = SocialAccount::where('provider_id', $socialUser->getId())
->where('provider_name', $provider)
->first();
if($socialAccount) {
return $socialAccount->user;
} else {
$user = User::where('email', $socialUser->getEmail())->first();
if(!$user) {
$user = User::create([
'username' => $socialUser->getName(),
'email' => $socialUser->getEmail()
]);
$user->assignRole('Registered');
}
$user->socialAccounts()->create([
'provider_id' => $socialUser->getId(),
'provider_name' => $provider
]);
return $user;
}
}
}
this is my User model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use App\Profile;
use App\Article;
use App\Video;
use App\Images;
use App\News;
class User extends Authenticatable Implements MustVerifyEmail
{
use Notifiable, HasRoles;
protected $table = "users";
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'username', 'email', 'password'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function profile(){
return $this->hasOne(Profile::class);
}
public function article()
{
return $this->hasMany(Article::class);
}
public function socialAccounts()
{
return $this->hasOne(SocialAccount::class);
}
public function video(){
return $this->hasMany(Video::class);
}
public function news(){
return $this->hasMany(News::class);
}
}
the complete error message like this :
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /home/asyj6686/public_html/sublaravel/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 297
Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given
This error is pretty straight-forward. It means that you have passed a null value to the login.
Auth::login($authUser, true);
I don't see anything wrong with the provided code. Therefore, I'm going to guess that you may have simply forgotten to add the inverse relationship with User in the SocialAccount model. This would cause $socialAccount->user to return null and generate the error you are receiving.
App\SocialAccount.php
class SocialAccount extends Model
{
// ...
public function user()
{
return $this->belongsTo(User::class);
}
}
On a side note, shouldn't a User be able to ->hasMany() SocialAccounts?
I am trying to authenticate my user with the help of Helpers
For this purpose i have make Helper folder in app directory. Add the following lines of code to the composer.json
"files": [
"app/Helpers/UserHelper.php"
],
Make HelperServiceProvider.php in App\Provider directory, and use the following code in it.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
}
after this i have add alias in app.php as well as add provide like this
//this is an alias
'UserHelper' => App\Helpers\UserHelper::class,
//this is an provider
App\Providers\HelperServiceProvider::class,
My User model is
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $table='users';
protected $fillable =['username', 'password', 'firstname', 'lastname', 'email', 'phone', 'groupname', 'about', 'image'];
public static $login = [
'username' => 'required|',
'email' => 'required|',
'password' => 'required'
];
}
This my UserHelper
<?php namespace App\Helpers;
use Illuminate\Support\Facades\Auth;
class UserHelper {
public static function processLogin($inputs){
if(Auth::attempt($inputs)){
return TRUE;
} else {
return FALSE;
}
}
}
Here is my Login Function
<?php
namespace App\Http\Controllers;
use App\User;
use Input;
use Illuminate\Support\Facades\Validator as Validator;
use App\Helpers\UserHelper;
class LoginController extends Controller
{
public function login() {
$inputs = Input::except('_token');
$validator = Validator::make($inputs, User::$login);
if($validator->fails()){
print_r($validator->errors()->first());
} else {
$respones = \UserHelper::processLogin($inputs);
if($respones){
return 'loginView';
} else {
return 'not a user of our DB';
}
}
}
}
I have also updated my composer and after i login to application following error comes up , i am searching this for last 5 hour any help ?
Reards
In your code you are extending the class User extends Model but when you are using auth functionality in laravel you need to extend the auth rather than model..
Keep Illuminate\Foundation\Auth\User and extends the model like this...
class User extends Authenticatable{
//code here
}
I'm trying to use a Repository, but I'm getting this error:
Class App\Repositories\CategoryRepository does not exist
This is my CategoryRepository.php
<?php
namespace App\Repositories;
class SubCate
{
/**
* Get all of the tasks for a given user.
*
* #param User $user
* #return Collection
*/
public function getCategories(){
$categories=\App\category::where('parent_id',0)->get();//united
$categories=$this->addRelation($categories);
return $categories;
}
}
?>
And this is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Product;
use App\Category;
use App\Repositories\CategoryRepository;
class ProductController extends Controller
{
//
public function __construct(CategoryRepository $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
public function index(Request $request)
{
$subcate = new SubCate;
try {
$allSubCategories=$subcate->getCategories();
} catch (Exception $e) {
//no parent category found
}
return view('welcome', [
'allSubCategories' => $allSubCategories,
]);
}
}
What is wrong?
Your category repository class name is
class SubCate
but you are using
use App\Repositories\CategoryRepository; .
So, change your class name to CategoryRepository
I am getting the following error in my Laravel app, could someone help me troubleshoot this exception?
FatalErrorException in SerializableClosure.php(153) : eval()'d code
line 2: Call to a member function getOwnerEmail() on array
My getter is in a Notices.php model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Notice extends Model {
/**
* A notice is created by a user
* #return [type] [description]
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
* Get the email address of the notice
* #return [type] [description]
*/
public function getOwnerEmail()
{
return $this->user->email;
}
NoticesController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Provider;
use App\Notice;
use Illuminate\Http\Request;
class NoticesController extends Controller {
public function store(Request $request)
{
$notice = $this->createNotice($request);
\Mail::queue('emails.dmca', compact('notice'), function($message) use ($notice) {
$message->from($notice->getOwnerEmail())
->to($notice->getRecipientEmail())
->subject('DMCA Notice');
});
return redirect('notices');
}
public function createNotice(Request $request)
{
$notice = session()->get('dmca') + ['template' => $request->input('template')];
\Auth::user()->notices()->create($notice);
return $notice;
}
public function create()
{
// get list of providers
$providers = Provider::lists('name', 'id');
return view('notices.create', compact('providers'));
}
You may try this:
public function createNotice(Request $request)
{
$notice = session()->get('dmca') + ['template' => $request->input('template')];
return \Auth::user()->notices()->create($notice);
}
I am looking to display a list of students that have the same course ID as the current user (tutor).
http://snag.gy/VOHJ3.jpg Here is my database design.
<?php
namespace Simple\ProfileBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
class SecurityController extends Controller
{
public function loginAction(Request $request)
{
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('SimpleProfileBundle:Security:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
public function dumpStringAction()
{
$findStudents = $this->getUser()->getCourses();
$results = $this->_em
->createQuery("SELECT * FROM user where")
->getResult();
return $results;
}
return $this->render('SimpleProfileBundle:Security:dumpString.html.twig', array(
'findstudents'=> $findStudents));
}
}
Anyone have any idea how i can do this ? I was thinking of a custom query however i am unsure how to do so?
Cheers
First of all if you want to use custom queries, you should do that by creating entities' repository.
Example:
Entity:
<?php
namespace YourName\YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* YourClass
*
* #ORM\Entity(repositoryClass="YourName\YourBundle\Entity\Repository\YourClassRepository")
* #ORM\Table(name="your_class")
*/
class YourClass
{
// your entity definition
}
Then you have to create entity repository class:
<?php
namespace YourName\YourBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
/**
* YourClassRepository
*/
class YourClassRepository extends EntityRepository
{
public function getStudentsByCourseID($courseId)
{
$qb = $this->_em->createQueryBuilder();
$qb
->select('student')
->from('YourNameYourBundle:YourClass', 'student')
->leftJoin('YourNameYourBundle:Course', 'course')
->where('course.id == :courseId');
$qb->setParameter('courseId', $courseId);
return $qb->getQuery()->getArrayResult();
}
Then you can call your custom query in your controller:
<?php
namespace Simple\ProfileBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
class SecurityController extends Controller
{
public function loginAction(Request $request)
{
// your code here...
}
public function yourAction($courseID)
{
$repo = $this->getDoctrine()->getRepository('YourNameYourBundle:YourClass');
$students = $repo->getStudentsByCourseID($courseID);
return [
'students' => $students
];
}
}
I think that's what you need.