Data not saved in database in real after model created - laravel-5

I am using CREATE method for mass assignment in laravel eloquent, the result also printed and showing, but when i check table in database, no data inserted.
I am using laravel 5.6, It showing successful, but in real no data saved in table.
here is my code
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
protected $table = 'users';
public $primaryKey = 'user_id';
public $timestamps = false;
protected $fillable = [
'first_name','middle_name', 'last_name', 'role_id', 'email', 'mobile', 'telephone'
];
.... and other code
Here is my insert function
$users = User::create([
'first_name' => $row[1],
'middle_name' => $row[2],
'last_name' => $row[3],
'email' => $row[4],
'role_id' => 6,//$row[5],
'telephone' => $row[6],
'mobile' => $row[7],
]);
var_dump($users);// this shows result.
$users prints data. There is no error showing. BUT when i check database, table is empty. I expect the same data in users table.

Related

Laravel update eloquent not updating the record

I am using Laravel eloquent to update the record. The problem is the update method returns the integer equal to number of records should be updated but this update is not being reflected in database.
I inserted some records to check there is no database mismatch.
I am even using fillable method and defined every column into it.
Here is my code.
Modal
protected $fillable = [
'id',
'user_id',
'ticket_no',
'partner_id',
'partner_user_name',
'partner_user_email',
'partner_user_contact',
'contacted_for_id',
'issue_type_id',
'comm_mode_id',
'opened_by_id',
'assigned_to_id',
'team_id',
'priority_id',
'opened_date',
'tat_id',
'resolved_date',
'status_id',
'description',
];
Controller
public function update(Request $request)
{
$validators = Validator::make($request->all(), [
'assigned_to_id' => 'required',
'team_id' => 'required',
'resolve_date' => 'required',
'status_id' => 'required',
'description' => 'required',
]);
if ($validators->fails()) {
$data['result'] = false;
$data['messages'] = $validators->errors()->first();
return json_encode($data);
}
$assigned_to_id = $request->assigned_to_id;
$team_id = $request->team_id;
$resolve_date = $request->resolve_date;
$status_id = $request->status_id;
$description = $request->description;
$ticket_row_id = $request->ticket_row_id;
$updated = TICKET_TRACKER::where('id', $ticket_row_id)
->update(
['assigned_to_id' => $assigned_to_id],
['team_id' => $team_id],
['resolve_date' => $resolve_date],
['status_id' => $status_id],
['description' => $description]
);
}
I am not getting any error that's the most frustrated thing.
In order to make your model to be updated change:
$updated = TICKET_TRACKER::where('id', $ticket_row_id)
->update(
['assigned_to_id' => $assigned_to_id],
['team_id' => $team_id],
['resolve_date' => $resolve_date],
['status_id' => $status_id],
['description' => $description]
);
By:
$updated = TICKET_TRACKER::where('id', $ticket_row_id)
->update([
'assigned_to_id' => $assigned_to_id,
'team_id' => $team_id,
'resolve_date' => $resolve_date,
'status_id' => $status_id,
'description' => $description
]);
In fact you need to have only one array with keys and values in order to view your model updated.
[EDIT 1]
As said in the comments in Laravel you need to take care about naming conventions,
If the name of the model is TicketTracker, the name of the table should be plural. So it will be ticket_trackers.
or if you want to have a custom name for your table you can configure in your model the $table property as follows:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TicketTracker extends Model
{
protected $table = "other_table_name"
}

Call to a member function details() on null?

My code is:
$user = User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$user_details = [
'name' => $request->name,
'address' => $request->address,
'lastname' => $request->lastname,
'secondname' => $request->secondname,
'inn' => $request->inn,
'fincode' => $request->fincode,
];
$user->details()->create($user_details);
Model User is:
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
public function details()
{
return $this->hasOne(UserDetails::class, 'user_id', 'id');
}
}
UserDetails model is:
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserDetails extends Model
{
protected $table = 'enterprise';
protected $fillable = ['name', 'lastname', 'secondname', 'address', 'inn', 'fincode'];
}
I believe that your $user has not been persisted, hence having the error, in your case $user is null, that's why you cannot call details on null object. Make sure that you use all the required fields on your user.
You might be missing the fillable array in the User model if the one that you shared is the full content, then add this:
protected $fillable = [ 'email', 'password'];

Hashing a fillable password

I have no idea how to hash fillable password input. I'm trying to hash it then gets stored to the database. Here's what I've done so far
use App\User;
use Illuminate\Http\Request;
class RegistrationController extends Controller
{
public function store()
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed'
]);
$pass = bcrypt(request()->password);
$user = User::create(request(['name', 'email', $pass]));
auth()->login($user);
return redirect()->home();
}
}
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password'
];
protected $hidden = [
'password', 'remember_token',
];
}
It gives me a QueryException:
SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into users (name, email, updated_at, created_at) values (the name i inserted, email inserted, date timestamp, date timestamp)
When using: $user = User::create(request(['name', 'email', $pass]));, you are passing an array to the request method and one of the elements ($pass) is not a key of $request.
I believe it should look more like:
$user = User::create([
'name' => request('name'),
'email' => request('email'),
'password' => $pass
]);
EDIT
Also, remember that Laravel provides a Hash facade to help you with encryption:
You can also use the Hash facade to do the same as bcrypt.
Thanks to lagbox for the correction.
$hashedPassword = Illuminate\Support\Facades\Hash:make(request('password'));

Laravel 5 - SoftDelete not working

I've been searching a lot in the web and StackOverflow itself, however I can't find a solution to the problem.
Soft deleting simply refuses to work. The database has the deleted_at field. User model has everything needed to supposedly work, however data is still hard deleted when I call the Delete() method. I don't know what I'm doing wrong.
Laravel Version: 5.3.31
destroy method
public function destroy($id)
{
//$this->authorize('delete', User::class);
$user = User::find($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('users.index'));
}
$user->Delete();
Flash::success('User deleted successfully.');
return redirect(route('users.index'));
}
user model
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
public $table = 'users';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $dates = ['deleted_at'];
public $fillable = [
'name',
'email',
'password',
'role',
'remember_token'
];
protected $casts = [
'id' => 'integer',
'name' => 'string',
'email' => 'string',
'password' => 'string',
'role' => 'string',
'remember_token' => 'string'
];
public static $rules = [
'name' => 'required|max:255',
];
}
I appreciate anything that can help me to solve this.
Thanks in advance.

Fill extra column for User

I have a user table with a referral column and this code in controller/auth:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'referral' => md5($data['email']),
]);
}
It adds a name, email and password but not referral.
There is no error or notice. What should I do to fill also referral column?
Open your User model (probably app/User.php, and change:
protected $fillable = [
'name', 'email', 'password',
];
into
protected $fillable = [
'name', 'email', 'password', 'referral'
];
You need to add column to fillable to save it.

Resources