Hello i am trying to use a trait from controller in my register controller but it can't seem to find it
the error message:
Trait 'MailVerification' not found
The class in which i want to use the trait
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
use MailVerification;
Here i call the function
protected function create(array $data)
{
$mail = $data['email'];
$this->sendVerification($mail);
Here is the trait in the class i am trying to import it from
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
trait MailVerification
{
public function sendVerification($mail)
{
$verification_code = str_random(30);
Mail::send('mail.verify', ['verification_code' => $verification_code, 'mail' => $mail], function ($message) use ($mail)
{
$message->from('test#laravel.com');
$message->to($mail);
});
Session::flash('message', "Please check you're email to verify your account");
return redirect('/');
}
}
class MailController extends Controller
{
I have the trait outside of my class i don't know if this is correct but it was giving me an error while it was inside the class.
The namespace of your controller RegisterController and your trait MailVerification is different...
So, you'll have to add this line to your RegisterController
use App\Http\Controllers\MailVerification;
Also, I suggest you to put all your traits inside App\Traits folder instead of your controller. Try following a simpler way if possible :)
Edit --
This is how you register controller should look like
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MailVerification;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers, MailVerification;
//Your code here....
}
Related
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(Auth::Attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Change the Auth namespace to:
use Illuminate\Support\Facades\Auth;
You may use the auth helper instead and then no worries about any class name
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class maincontroller extends Controller
{
public function home(Request $request)
{
if(auth()->attempt($request->only('email','password'))) {
return redirect('/');
}
}
}
Try this :
\Auth::attempt([...]);
I think this is helpfull..
Use the Auth namespace to:
use Illuminate\Support\Facades\Auth;
Here is my code in controller, am trying to sort columns in view file by just clicking on corresponding column name
<?php
namespace App\Http\Controllers;
use App\Http\Controllers;
use Illuminate\Http\Request;
use App\Service;
use DB;
use JsValidator;
use Validator;
use Session;
use Redirect;
use Carbon\Carbon;
use App\Libraries\GlobalHelpers;
use App\Libraries\ImageHelpers;
use Auth;
use Response;
class ServicesController extends Controller
{
protected $serviceValidationRules = [
'service_name' => 'required'
];
public function index()
{
$services = Service::all()->sortable()->get();
return view('services.index')->with('services', $services);
}
?>
Am getting following error
Method Illuminate\Database\Eloquent\Collection::sortable does not exist.
Kindly help me to resolve
I make a middleware RedirectIfNotStudent
public function handle($request, Closure $next, $guard = 'student')
{
if (!Auth::guard($guard)->check()) {
return redirect('/student/login');
}
return $next($request);
}
I create a new directory As StudentAuth for Authentication and these methods are there
public function showLoginForm()
{
if (Auth::guard('student')->check())
{
return redirect('/student');
}
return view('student.login');
}
public function logout(){
Auth::guard('student')->logout();
return redirect('/student/login');
}
I add a new row in Kernel File
'user' => \App\Http\Middleware\RedirectIfNotUser::class,
My Route for this is here
Route::get('student','StudentAuth\StudentAuthController#showLoginForm');
Route::post('student','StudentAuth\StudentAuthController#login');
StudentAuthController
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
RedirectIfNotUser
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
Change your namespace :
namespace App\Http\Controllers\Auth;
With
namespace App\Http\Controllers\StudentAuth;
StudentController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
I'm having a trouble using a model in a controller in Laravel 5.0. I created the model in a folder model under App. The code of the model id described as:
<?php namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Observacion extends Model{
protected $table = 'obs_usuarios';
protected $fillable = ['observaciones', 'usuario_id','autor_id','tipo'];
}
Part of the code of the controller where I pretend to use it, is:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\Contrato;
use app\models\Observacion;
use App\models\Perfil;
use Illuminate\Http\Request;
use App\models\Configuracion;
use App\models\Usuario;
use App\models\Categ_profesores;
use Carbon\Carbon;
use Illuminate\Support\Facades\Input;
use Auth;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class SecretariaController extends Controller {
public function observacion($msg,$user_id,$autor_id,$type){
$observacion = new Observacion();
$observacion->observaciones=$msg;
$observacion->usuario_id=$user_id;
$observacion->autor_id=$autor_id;
$observacion->tipo=$type;
$observacion->save();
}
I even made dump-autoload but I get this error:
FatalErrorException in SecretariaController.php line 155:
Class 'app\models\Observacion' not found
The line 155 is where I do: $observacion = new Observacion();
So, I don't know what else to check.
Change:
use app\models\Observacion;
to:
use App\models\Observacion;
The first letter is a capital. In PHP names are case-sensitive.
I have a controller in Laravel 5.0 like this-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CustomersController extends CustomerLayoutController
{
public function getDashboard()
{
return $this->view('layouts.customer.dashboard', []);
}
public function getTest()
{
return $this->view('layouts.admin.webinar', ['qustions' => DB::table('qustions')->get()]);
}
}
So, I want to pass DB::table('qustions')->get() as a parameter to my views, but I m getting error.
What I am doing wrong?
You're getting an error because Laravel is searching the class is the wrong namespace (it's "appending" the class to the current class's namespace, if you note).
You either import the DB class with the use keywords:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
or let it know that DB resides in the "global" application namespace, so call it with a backslash:
return $this->view('layouts.admin.webinar', ['questions' => \DB::table('qustions')->get()]);