Laravel 5.2 Class 'App\authors' not found - laravel

Error is:
FatalErrorException in AuthorsController.php line 28: Class
'App\authors' not found
Line 28:
<?php
namespace App\Http\Controllers;
use App\authors;
use App\Http\Requests;
use Illuminate\Http\Request;
class AuthorsController 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 show()
{
$authors = authors::all(); LINE 28
return view('authors', ['authors'=> $authors]);
}
}
Authors class:
<?php
namespace App;
use App;
use Illuminate\Database\Eloquent\Model;
class authors extends Model
{
protected $table = 'authors';
}
I don t get it where the mistake is... I am new to Laravel.

Try App\Author;
Check your spelling errors. Hope you get fixed

Related

How to Get Model Dynamic in Controller Using Laravel?

I'm trying to get model name on session. Using session I'm try too get model dynamic. But it is not work
My Code
<?php
namespace App\Exports;
use App\Http\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
class Export implements WithHeadings
{
/**
* #return \Illuminate\Support\Collection
*/
public $table="";
public function headings(): array
{
$model="App\Http\Models\\".Session::get('tableName');
$this->table = new $model;
}
}

Class App\Http\Requests\TestRequest does not exist? But I am using this class in Controller

I am using same App\Http\Requests\TestRequest in controller but its display message this Class not exit what can be problem? Plz help me?
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\TestRequest;
use App\Student;
class InsertController extends Controller
{
public function insert(TestRequest $request){
$obj = new Student();
$obj->Name =$request->name;
$obj->City = $request->city;
$obj->save();
return view('submit');
}
}
TestRequest Code is Here!
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class TestRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'Name'=>'required'
'City'=>'required'
];
}
}
Above Both codes are of Request folder And controller folder, I have done everything, i have done this class in Controller but its showing that not exist why? please Help me.

Laravel giving invalid view error for mail

I'm new to Laravel. I wanted to send a mail to users for every time they login. I wrote the authentication logic already.
<?php
namespace App\Http\Controllers\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Events\Login;
class LoginController extends Controller
{
public function __construct(){
$this->middleware('guest')->except('logout');
}
public function index(){
return view('users.login');
}
public function login(Request $request){
$credentials = $request->only('username', 'password');
if(Auth::attempt($credentials)){
event(new Login(auth()->user()));
return redirect()->intended(route('homepage'));
}else{
return redirect()->back()->withInput()->with('error', 'Username/Password Combo Wrong!');
}
}
public function logout(){
Auth::logout();
return redirect()->route('login-form');
}
}
I wrote an event for this
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;
class Login
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
This is the event listener
<?php
namespace App\Listeners;
use App\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Request;
use App\Mail\UserLoggedIn;
class SendLoginNotification
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param Login $event
* #return void
*/
public function handle(Login $event)
{
Mail::send($event)->send(new UserLoggedIn($event));
}
}
I created a mail class
<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserLoggedIn extends Mailable
{
use Queueable, SerializesModels;
public $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('test#mail.com')->view('emails.users.loggedin');
}
}
I then created the view for the mail in resources/views/emails/users/loggedin.blade.php
<!Doctype html>
<html>
<head>
<title>User Logged In</title>
</head>
<body>
{{$user->name}} Logged in
</body>
</html>
But when i run try to run the login, i get an error saying invalid view
There are a couple of issues I've noticed. Currently, you're:
calling send twice in your SendLoginNotification (and you're passing the Event instead of a mailable to one of them)
not specifying who to send the mail to
also passing the $event to the mailable rather than the User
Change:
Mail::send($event)->send(new UserLoggedIn($event));
To:
Mail::to($event->user)->send(new UserLoggedIn($event->user)); //notice the "user" property.
you didn't pass any variable containing user info.
public function build()
{
$user=User::find(1);
return $this->from('test#mail.com')->view('emails.users.loggedin',compact('user'));
}

Can an Eloquent model has multiple Observer?

Hi I want write a trait to add an observer to model but I thought write boot method is not the right way and finnaly i find that i can boot trait like boot[TraitName] but i wonder if i add an observer with code like this:
trait CreateObserver
{
public static function bootCreateObserver()
{
static::creating(function (Model $model) {
// ...
});
}
}
can I add another observer for my model like below or it will overriding my trait observer?
class MyModel extends Model
{
use CreateObserver;
public static function boot()
{
static::creating(function ($model) {
// ...
});
}
...
}
That's not the right way. I think this might help you:
https://laravel.com/docs/5.6/eloquent#observers
You bind observers to your models using a service boot:
<?php
namespace App\Providers;
use App\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
User::observe(UserObserver::class);
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
//
}
}
Inside the observer you can add all desired functionality:
<?php
namespace App\Observers;
use App\User;
class UserObserver
{
/**
* Listen to the User created event.
*
* #param \App\User $user
* #return void
*/
public function created(User $user)
{
//
}
/**
* Listen to the User deleting event.
*
* #param \App\User $user
* #return void
*/
public function deleting(User $user)
{
//
}
}
And to elaborate. Yes it can have multiple observers. Although I never seen a useful situation for that:
public function boot()
{
User::observe(UserObserver::class);
User::observe(AuthenticableModelsObserver::class);
}
This way both the UserObserver() and AuthenticableModelsObserver() are binded to the User() model on boot.

Laravel policy always return false

I made a policy for one model.the policy class as following:
namespace App\Policies;
use App\User;
use App\Models\Address;
use Illuminate\Auth\Access\HandlesAuthorization;
class AddressPolicy
{
use HandlesAuthorization;
public function delete(User $user, Address $address)
{
return $user->id === $address->user_id ;
}
}
And I register the policy in authServiceProvider
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\Models\Address ;
use App\Policies\AddressPolicy;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Address::class => AddressPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
//
}
I use this policy in a controller
namespace App\Http\Controllers\User;
use App\Models\Address;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth ;
class AddressController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function destroy(Address $address)
{
$this->authorize('delete', $address);
$address->delete();
return response()->json(['msg'=>'success'], 200);
}
when I request the destroy method in the controller, it always return a 403 status, when I remove this line "$this->authorize ('delete', $address)",the data can be deleted.I've tried several solutions and searched many same problem, none of them resolved my problem.

Resources