Can't validate user attempt - laravel-5

I'm using laravel 5.0 and I can't validate the login.
If I type a wrong match of email/pass, it redirects me perfect.
But when I type a correct email/pass then it returns me this:
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given ...
I'm following the documentation and it looks like everything is allright.
Model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
}
Controller
public function postsignin(Request $request)
{
if( Auth::attempt( ['email' => $request['email'], 'password' => $request['password'] ] ) )
return redirect()->route('dashboard');
return redirect()->back();
}
No idea what it is.

try to implement the AuthenticatableContract and Authenticatable trait
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Model implements AuthenticatableContract{
use Authenticatable
}

Related

Call to undefined method Auth::user()->can() in laravel 8 error

I have a problem with determining the permission with Auth::user()->can().
For example Auth::user()->can('trunk.index) returns always error;
But I have a problem.
If I dump $user->getPermissionsViaRoles(); ,I will get a large result.
I'm using different table user_view table.
And according to it I have changed in Auth.php file. and login is working fine.
'providers' => [
'users' => [
'driver' => 'self-eloquent',
'model' => App\Models\UserView::class,
]
],
But when I try to check permission the via Auth::user()->can('trunk.index) then it gives error below error.
Call to undefined method App\\Models\\UserView::can()
Below is my UserView model code.
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Traits\HasRoles;
use Laravel\Lumen\Auth\Authorizable;
use Laravel\Sanctum\HasApiTokens;
class UserView extends Model implements AuthenticatableContract
{
use Authenticatable;
use HasFactory;
use HasRoles;
use HasApiTokens;
protected $table = 'user_view';
protected $primaryKey = "user_id";
protected $fillable = [
'username', 'password',
];
protected $guarded = [];
public function getAuthPassword()
{
return ['password' => $this->attributes['user_password']];
}
// public function can($abilities, $arguments = []) {
// }
}
help me if I'm missing something. Thank you.
You are using a custom User model which does not implement the \Illuminate\Contracts\Auth\Access\Authorizable interface and \Illuminate\Contracts\Auth\Access\Gate\Authorizable trait. You do actually have an import for this interface, but it is not used anywhere.
Also be careful about the existing import Laravel\Lumen\Auth\Authorizable. I'm not familiar with Lumen, but this might be a different implementation / wrong import.
You essentially need to update your model with the following two lines:
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Foundation\Auth\Access\Authorizable; // <-- add import
use Spatie\Permission\Traits\HasRoles;
use Laravel\Sanctum\HasApiTokens;
class UserView extends Model implements AuthenticatableContract,
AuthorizableContract // <-- add interface
{
use Authenticatable;
use Authorizable; // <-- add trait
use HasFactory;
use HasRoles;
use HasApiTokens;
// ... other code ...
}

How to get the "catagory name" in Laravel?

How to get "category name" show in the product table not as "category_id"?
I already try to combine any solutions for this. But still can't solve this prob.
I'd love to hear other suggestions from the masters here.
Category Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Kategori extends Model
{
use HasFactory;
protected $casts = [
'updated_at' => 'datetime:d/m/Y, H:i:s'
];
public function Kategori()
{
return $this->hasMany('App\Models\Produk');
}
}
Product Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Produk extends Model
{
use HasFactory;
protected $casts = [
'updated_at' => 'datetime:d/m/Y, H:i:s'
];
public function Produk()
{
return $this->belongsTo('App\Models\Kategori', 'kategori_id');
}
}
Category Table
Product Table
Product Controller >>>> in my opinion may be my prob at here, but not so sure.
<?php
namespace App\Http\Controllers;
use App\Models\Produk;
use RealRashid\SweetAlert\Facades\Alert;
use Yajra\Datatables\DataTables;
use Illuminate\Http\Request;
class ProdukController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function json(){
return Datatables::of(Produk::all())->make(true);
}
public function index(){
return view('back.produk.show');
}
}
Pass the other model using your relationship
public function json(){
return Datatables::of(Produk::with('produk')->get())->make(true);
}
to which say that is named $produks, you can access it as
$produk->produk->nama;
As a side note, Do name your relationships the names of the other model.
for example, in the Kategori class, the relationship to Produk should be named produks (it is a hasMany relationship) as opposed to Kategori. Similarly, in the Produk class, the relationship to Kategori being named kategori() to which from the above answer you access it like
$produk->kategori->nama;
You should use:
public function json(){
return Datatables::of(Produk::with('Kategori')->all())->make(true);
}
I recommend to use camelCase for method names. I'm not sure how Datatables will handle this case.
In your ProdukController you need pass below code.
$result = Produk::with('Produk')->get();
When you dd($result);, you should see the related models in the relations array attribute.
To access the relations' properties from there, it is simply
$result->Produk->catagory_name

Laravel: undefined method timeline

I am extending the Tweety application from Laracast.com. I run info a problem that I struggle to find the source of. (Btw I am using VS Code).
The request handler in web.php:
Route::middleware('auth')->group(function() {
Route::get('/tweets', 'TweetsController#index')->name('home');
The index method in the tweetscontroller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tweet;
class TweetsController extends Controller
{
public function index()
{
return view('tweets.index', [
'tweets' => auth()
->user()
->timeline(),
]);
}
In my problems window I get the warning:
Tweetscontroller.php app\Http\Controllers
Undefined method 'timeline'. intelephense(1013) [17,19]
But the timeline method is defined in the User model:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable, Followable;
public function timeline()
{
$friends = $this->follows()->pluck('id');
return Tweet::whereIn('user_id', $friends)
->orWhere('user_id', $this->id)
->withLikes()
->orderByDesc('id');
}
If I login to the aplication I get the error message:
Facade\Ignition\Exceptions\ViewException
Call to undefined method Illuminate\Database\Eloquent\Builder::links() (View: C:\xampp\htdocs\tweety\resources\views\__timeline.blade.php)
Anyone a good suggestion?
Kind regards,
HUbert

ORM not working in Default UserController & User Model in laravel

I want to get User wise Role. here is I'm facing error ....
UserController.php ( user controller file )
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\Employee;
use App\Role;
use App\User;
use App\Site;
use App\Client;
use App\ProjectType;
use App\UserPermission;
use Auth;
use DB;
use App\Project;
class UsersController extends BaseController {
public function __construct() {
$this->isSetClientAndProjectType();
$data = User::with('Role')->first();
echo "<pre>";print_r(json_decode($data)); die;
}
}
User.php ( user model file )
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes;
use Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get users roles
public function Role() {
return $this->hasMany('App\Role', 'role_id', 'id');
}
}
Error is
How can i solve this error?
Help me guys.
Thank You.
If a user has many "roles" it should be public function roles().
You have defined:
A single user has a role_id
Therefore you need:
If a user has a single role it would be:
public function role() {
return $this->belongsTo('App\Role');
}
The reverse on the Role model would be:
public function users() {
return $this->belongsToMany('App\User');
}
Since many users can have the same role.
Hope this helps.
You need to add belongsTo relationship
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes, Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get user's role
public function role() {
return $this->belongsTo('App\Role');
}
}
Now fetch data
$user = User::with('role')->find(1);
$role = $user->role;
You need to make sure your table structure and foreign key references are compatible with the model relationship methods used.
For example, you have used "HasMany" relationship on User model. For that, you will have to make sure that each record/row in users table(User model) "has many" associated records/rows in roles table(Role model).
Here HasMany method assumes a foreign key "role_id" on roles table(Role Model). On not finding of which, it throws error.
You first need to take in consideration the table structure of roles and users table(User and Role model) as per your requirement, and than add model relationship methods accordingly.
It can be a bit tricky if you are using the methods for the first time, you can refer the laravel documentation for the same:
eloquent-relationships

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model

I am pretty new to Laravel and I am trying to add the post, create by a user into the database. But when I do so, following error comes:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save()
must be an
instance of Illuminate\Database\Eloquent\Model, string given,
called in C:\xampp\htdocs\lar\app\Http\Controllers\PostController.php on line
25 and defined
User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('App\Post');
}
}
Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User') ;
}
}
PostController:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
$post->$request['body'];
$request->user()->posts()->save('$post');
return redirect()->route('dashboard');
}
}
Post Route:
Route::post('/createpost',[
'uses' => 'PostController#postCreatePost',
'as'=>'post.create'
]);
Form action:
<form action="{{route('post.create')}}" method="post">
Please tell me how to fix this.. How to fix this?
Thank you in advance.. :)
I think what you want is this:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
// here you set the body of the post like that
$post->body = $request->body;
// here you pass the $post object not as string
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
You need to pass the $post object as an object to the save method. You was doing this: $user->posts()->save('$post') when you need to do this: $user->posts()->save($post).
Hope it helps.

Resources