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;
...
Related
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!
Why are methods not found in Authenticatable? Im using Laravel 6 Cashier 10 with Stripe API. Please see the following errors in my Subscription Controller.
Method 'createSetupIntent' not found in \Illuminate\Contracts\Auth\Authenticatable|null
Method 'addPaymentMethod' not found in \Illuminate\Contracts\Auth\Authenticatable|null
Unhandled \Stripe\Exception\ApiErrorException
The above errors show in up in PHP storm. When the code is actually ran in the browser, I get this error: ... This makes sense considering PHP Storm cant find my methods. What do you guys think?
This customer has no attached payment source or default payment method.
Here is my User model.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Auth\User as Authenticatable;
use Laravel\Cashier\Billable;
use Illuminate\Foundation\Auth;
class User extends Authenticatable
{
use Billable, Notifiable;
protected $connection = 'mongodb';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'username', 'password', 'phone',
];
/**
* The collection name
*
* #var array
*/
protected $collection = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $dates = ['deleted_at'];
/**
* 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',
];
}
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);
}
}
I just downloaded a search class for laravel called laravelista/comments
The documentation is pretty scanty so I do not really understand everything.
Basically,when I try to after typing in my details in and pressing submit in'localhost:8000/login' it directs me to laravel PrettyPageHandler and shows me this error:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Trait 'Laravelista\Comments\Comments\Traits\Comments' not found
I have already downloaded laravelista/comments and run all the commands like:
php artisan vendor:publish --provider="Laravelista\Comments\Providers\CommentsServiceProvider" --tag=migrations
php artisan migrate
php artisan vendor:publish --provider="Laravelista\Comments\Providers\CommentsServiceProvider" --tag=config
php artisan vendor:publish --provider="Laravelista\Comments\Providers\CommentsServiceProvider" --tag=public --force
but I still get that error
In my User.php:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravelista\Comments\Comments\Traits\Comments;
class User extends Authenticatable
{
use Notifiable;
use Comments;
/**
* 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',
];
}
Please help.
I just had to change User.php to
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravelista\Comments\Commenter;
class User extends Authenticatable
{
use Notifiable,Commenter;
/**
* 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',
];
}
I used the wrong use
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();