Eloquent updateOrCreate error: General error: 1364 Field doesn't exist - laravel

I am trying to use the updateOrCreate function and it was working earlier however when I added a new row called user it has stopped working even though my updateOrCreate look like this:
SystemCoreStats::updateOrCreate(['id' => $systemId],['type' => $type, 'stat_build_store' => $val, 'user' => $user->id]);
I don't understand what I am doing wrong.

Basically, this error means the user that your trying to updateOrCreate doesn't exist.
try to check in your Model if this is present:
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'column_name',
'column_name',
'column_name',
];
in your case
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'type',
'stat_build_store',
'user',
];
and try to check in your db if the column exist.
Can you post more of your code?

Related

Invisnik Laravel Steamauth login errors

So I've just created a project in laravel 7 and added the invisnik steamauth to it, I haven't added the actual auth package yet so that may be why I'm having the issue, however I don't want to lose what I've done.
I ran this migration:
Schema::create('users', function (Blueprint $table) {
$table->id("steamid");
$table->string('username');
$table->string('avatar');
$table->string('rank');
$table->rememberToken();
$table->timestamps();
});
Then I edited the create function in the steamauth controller to this:
return User::create([
'username' => $info->personaname,
'avatar' => $info->avatarfull,
'steamid' => $info->steamID64,
'rank' => 0
]);
When I go to login, I get this error:
SQLSTATE[HY000]: General error: 1364 Field 'username' doesn't have a default value (SQL: insert into `users` (`updated_at`, `created_at`) values (2020-05-06 18:32:42, 2020-05-06 18:32:42))
EDIT: Here's the User.php model
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',
];
}
Thanks.

Laravel 5.8 cannot generate api_token when new user register

I am playing around with Laravel Authentication.
On a freshly created Laravel app with Composer, I followed literally the instructions until this point (included)
https://laravel.com/docs/5.8/api-authentication#generating-tokens
When I register a new user, however, the api_token field is NULL.
What else do I need to do in order to start generating the api token when a user registers??
Create method in RegisterController:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}
Migration (I called it Token) updating the users table:
class Token extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 80)->after('password')
->unique()
->nullable()
->default(null);
});
}
App\User model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
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',
];
}
In your user model add 'api_token' to your fillables
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
'api_token'
];
After creating migration , run the migrate Artisan command:
php artisan migrate
If you are using the Homestead virtual machine, you should run this command from within your virtual machine:
php artisan migrate --force

Why all the fields are correctly stored except one?

I am creating a Provider like this:
$provider = Provider::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'is_available' => 1,
'is_activated' => 1,
'insurance' => 1
]);
Log::info($provider);
The Provider is actually well created, and all fields are stored correctly except the insurance field.
The insurance field isn't working and I can't figure out why.
It was created with Laravel migration tools, like this:
class AddInsuranceToProvidersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('providers', function($table) {
$table->integer('insurance');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
And then from terminal:
php artisan migrate
php artisan cache:clear
And here is how it looks like from phpMyAdmin:
Any idea why I can't save any value to insurance field?
Also when I log the $provider variable, there is nothing related with insurance field, as you can see there:
[2017-12-13 12:36:54] local.INFO:
{"first_name":"fef","last_name":"ewfe","email":"fel.bruno2njtz3#gmail.com","is_available":1,"is_activated":1,"updated_at":"2017-12-13
12:36:54","created_at":"2017-12-13 12:36:54","id":276}
Thanks to the answers, I was able to solve the issue by adding the insurance field to Provider Model, as it looks like this now:
protected $fillable = [
'name','first_name','last_name', 'email', 'password','is_available','is_activated','insurance'
];
Since you're using create() method, you need to add the field to $fillable array:
protected $fillable = ['insurance', ....];
You need to do that since create() is using fill() method to fill model fields for you.
https://laravel.com/docs/5.5/eloquent#mass-assignment
If you set a "fillable" array in your model, you must add the "insurance" field to it.
As you have altered table using migration you just need to update InsuranceProvider model in app directory and add insurance field to the fillable array in order to make this working.

Override attempt auth method in laravel 5

I'm having a big problem with Auth:attempt method in laravel 5.2, and I wish I can override it.
I have the class users, by default, but doesn't containt the attribut 'email', this last one is containing in an other class user_contacts, with relation one to one with users.
Can I override this function? because by default, it takes 'email', which is not in users in my case
I tried to make that in the model, but not working:
lass User extends Authenticatable
{
public function __construct(array $attr = array())
{
parent::__construct($attr);
$this->email = $this->user_contacts()->email;
}
/**
* 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',
];
public function user_contacts()
{
return $this->hasOne('App\UserContact', 'user_id', 'id');
}
}
It just says that the email field is not defined.
Any suggestion plz?
Thanks alot.
Manually authenticating user https://laravel.com/docs/5.3/authentication#authenticating-users
You can use any field you want, 'email' field is just the default.
Something like this will work perfectly fine:
if(Auth::attempt(['username' => $username, 'password' => $password])){
return redirect()->intended('admin');
}

how to update data into table using in laravel

I want to update data into table if the record exits if not then create a new record.
if(!empty($request->meta_title)){
$meta = new \stdclass();
$meta = VendorMeta::firstOrNew(['vendor_id' => $id]);
$meta->meta_title = $data['meta_title'];
$meta->meta_desc = $data['meta_desc'];
$meta->meta_keyword = $data['meta_keyword'];
$meta->save();
}
But I am getting this error:
MassAssignmentException in Model.php line 445:vendor_id
You should define which model attributes you want to make mass assignable, so in your VendorMeta class add following code:
protected $fillable = ['vendor_id'];
You need to set the $fillable property on your model.
Take a look at the docs under Mass Assignment.
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['meta_title', 'meta_desc', 'meta_keyword'];

Resources