I'm using Laravel 5.6 and trying to create an access token from my user model with Laravel passport.
When I select my user and send the request by postman I encounter this error:
The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.
My request:
domain.com/api/login?username=admin&password=admin&email=info#example.com
user model:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use Notifiable;
use HybridRelations, HasApiTokens;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
// 'name', 'email', 'password',
];
protected $table = 'users';
/**
* 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',
];
}
and my controller:
public function login(Request $request){
$user = User::where('password',md5($request->password))
->where('email',$request->email)
->first();
if($user){
$token = $user->createToken('Token Name')->accessToken;
dd($token);
}
}
Related
I am trying to implement subscriptions with stripe integration using cashier packages in my Laravel application. I am facing this error whenever I select a plan.
My PlanController.php file code is as followed
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Plan;
class PlanController extends Controller
{
/**
* Write code on Method
*
* #return response()
*/
public function index()
{
$plans = Plan::get();
return view("plans", compact("plans"));
}
/**
* Write code on Method
*
* #return response()
*/
public function show(Plan $plan, Request $request)
{
$intent = auth()->user()->createSetupIntent();
return view("subscription", compact("plan", "intent"));
}
/**
* Write code on Method
*
* #return response()
*/
public function subscription(Request $request)
{
$plan = Plan::find($request->plan);
$subscription = $request->user()->newSubscription($request->plan, $plan->stripe_plan)
->create($request->token);
return view("subscription_success");
}
}
After searching for solutions online I am still stuck at the same problem. Kindly help.
Some other models that might help you in finding solution are as followed:
Plan.php The model file for plans
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class plan extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'stripe_plan',
'price',
'description',
];
/**
* Write code on Method
*
* #return response()
*/
public function getRouteKeyName()
{
return 'slug';
}
}
Model file for users User.php
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'role',
'plan',
'created_by',
'status',
];
/**
* The attributes that should be hidden for serialization.
*
* #var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* #var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Add the Billable trait from Laravel Cashier to your User model.
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable, HasApiTokens, HasFactory, Notifiable;
}
User.php code,
here, whether I use fillable or gaurded, I get the same error.
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
*#var array
*/
// protected $fillable = [
// 'name',
// 'email',
// 'password',
// ];
protected $guarded = [];
/**
* 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',
];
}
UserController.php code,
here, I have tried the mass assignment
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;
class UserController extends Controller
{
public function index()
{
$data = [
'name' => 'elon',
'email' => 'elon#gmail.com',
'password' => 'password',
];
User::create($data);
$user = User::all();
return $user;
}
}
You seem to not be importing the user class from the right namespace in your UserController.php
You are using
use Illuminate\Foundation\Auth\User;
Use
use App\Models\User;
instead.
Edit:
$fillable is not the problem in this case as $guarded is set to an empty array which allows for all fields to be mass assignable through the create method. Eloquent mass assignment
There are two problems in the code provided:
As commented by #sta, you should allow the Model attributes to be mass assignable by using the $fillable property in the User class:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
*#var array
*/
protected $fillable = [
'name',
'email',
'password',
];
protected $guarded = [];
/**
* 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',
];
}
As commented by #Remy, we should make sure to use the correct class:
<?php
namespace App\Http\Controllers;
use App\Models\User; // <-- corrected line
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
class UserController extends Controller
{
public function index()
{
$data = [
'name' => 'elon',
'email' => 'elon#gmail.com',
'password' => 'password',
];
User::create($data);
$user = User::all();
return $user;
}
}
I found this helpful for me in laravel 8, this worked fine in all versions because many times if we import class and it auto import another one so please check that you import this class or another one.
use App\Models\User;
in the UserController.php try to use
use App\Models\User;
in laravel 7 this work for me :
use App\User;
For me, I had to stop the server in terminal with ctrl + c and the restart the server with php artisan serve It worked for me.
by adding this in my model
protected $guarded = [];
it save me from my misery thanks!
I have a simple Userpermission System consisting of 3 tables: users, permissions and the pivot table permission_user.
This is the User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', '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 permissions()
{
return $this->belongsToMany('App\Permission');
}
}
and here is the Permission Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'description', 'level', 'parentpermission',
];
public function users()
{
return $this->belongsToMany('App\User');
}
}
Now when I try to get all the permissions of the currently logged in user with this:
$user_permissions = Auth::user()->permissions()->get();
it works without problems.
But when I try to get another Users Permissions like this:
$user_permissions = User::where('id', '=', $userid)->permissions()->get();
I get the following error:
Method Illuminate\Database\Eloquent\Collection::permissions does not exist.
How do I proceed?
I think you're missing first() here, since you can't get relations of a query builder object. Try this :
$user_permissions = User::where('id', '=', $userid)->first()->permissions()->get();
This first() will actually return User object, and then you can load its relations.
simply you can just add first() method to get just one record and get it's permissions, try this:
$user_permissions = User::where('id', '=', $userid)->first()->permissions;
There's no need to use get() method, this will get all the user permissions directely
Do this -
$user_permissions = User::find($userid)->permissions()->get();
I have defined a field location as Spatial in a model that extends a Voyager model. But I keep getting a BadMethodCallException
Call to undefined method TCG\Voyager\Models\User::getCoordinates()
when I try to access the BREAD.
Here is the Model:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use TCG\Voyager\Traits\Spatial;
class User extends \TCG\Voyager\Models\User
{
use Notifiable;
use Spatial;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', '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',
];
/**
* Map Coordinate fields
*/
protected $spatial = [
'location'
];
}
I have also tried setting the location column to type GEOMETRY and POINT in the schema. But I suspect that has nothing to do with this.
I'm using Laravel 7 and Voyager 1.4
You can't have two lines with use. This should solve the problem.
class User extends \TCG\Voyager\Models\User
{
use Notifiable, Spatial;
...
My user Model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name','plan_id', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function plan()
{
return $this->hasOne('App\Plan');
}
}
And i have plan_id in user table as a foreign key, that refers to the id in plan table.
When i accesss, User::with('plan')->get(); i cant get plan, what did i miss?
If you have plan_id in user table as a foreign key, that refers to the id in plan table.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name','plan_id', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function plan()
{
return $this->belongsTo('App\Plan');
}
}
in your user model :
public function plan()
{
return $this->hasOne('App\Plan','user_id','id');
}
in your controller :
$users=User::with('plan')->get();