I am filling a database with seeders and factory, the problem is that I need to fill the CREATED_AT field with dates that are not today's date but random, to be able to fill the different graphs that the page has.
I tried and sometimes it inserts data and other times it throws error that the field is invalid and that is in date format the error that throws me in the console.
ERROR MESSAGE
"Incorrect datetime value: '2021-09-05 00:00:00' for column 'created_at' at row 1"
Error image
CODE
$factory->define(Opportunity::class, function (Faker $faker) {
$account_id = Account::all()->random()->id;
$account = Account::find($account_id);
$contact = Contact::where('account_id',$account_id)->inRandomOrder()->limit(1)->first();
$created = $this->faker->dateTimeBetween($startDate = '-3 month', $endDate = 'now +6 month');
$date = strtotime('+2 days', strtotime(Carbon::parse($created)));
return [
'created_at' => Carbon::parse($created)->format('Y-m-d H:i:s'), ///line error
'name' => $this->faker->name .' '.$this->faker->sentence(2),
'amount' => $this->faker->numberBetween($min = 120000, $max = 20000000),
'probability' => $faker->randomElement(['0','10','20','30','40','50','60','70','80','90','100']),
'description' => $this->faker->paragraph,
'lead_source_id'=> LeadSource::all()->random()->id,
'sales_stage_id'=> 1,
'account_id' => $account_id,
'user_id' => $account->user_id,
'contact_id' => ( $contact != null ? $contact->id : null),
'close_date' => Carbon::parse($date)->format('Y-m-d'),
'product_line_id'=> ProductLine::all()->random()->id
];
});
Try by changing $this->faker to $faker and migration should have $table->timestamps();
then u can use dateTimeBetween directly like this
'created_at'=>$faker->dateTimeBetween($startDate = '-3 month',$endDate = 'now +6 month')
OpportunitySeeder Class
public function run()
{
factory(App\Opportunity::class,5)->create();
}
DatabaseSeeder class
$this->call([
OpportunitySeeder::class
]);
https://laravel.com/docs/6.x/database-testing#using-seeds
Related
I'm trying to add unique validation to my model, but there is an error when I tried to update the data.
The table:
acq_m_budgets
==================================
budget_id serial NOT NULL,
budget_code character varying(15) NOT NULL,
budget_name character varying(100) NOT NULL,
ma_code character varying(10),
start_period timestamp without time zone NOT NULL,
end_period timestamp without time zone NOT NULL,
budget numeric(16) DEFAULT 0,
credit numeric(16) DEFAULT 0,
debet numeric(16) DEFAULT 0,
balance numeric(16) DEFAULT 0,
reserve numeric(16) DEFAULT 0,
created_by character varying(100) NOT NULL,
created_on timestamp without time zone DEFAULT now(),
updated_by character varying(100) NOT NULL,
updated_on timestamp without time zone DEFAULT now(),
CONSTRAINT PK_AcqMBudgets PRIMARY KEY (budget_id),
CONSTRAINT UN_AcqMBudgets UNIQUE (budget_code)
My model: AcqMBudgets.php
class AcqMBudgets extends Model
{
public $timestamps = false;
protected $primaryKey = 'budget_id';
public $sortable = ['budget_code', 'budget_name', 'ma_code', 'balance', 'updated_on'];
protected $fillable = ['budget_code', 'budget_name', 'ma_code', 'start_period', 'end_period', 'budget', 'credit', 'debet', 'balance', 'reserve', 'created_by', 'created_on', 'updated_by', 'updated_on'];
protected $attributes = [
'budget' => 0,
'credit' => 0,
'debet' => 0,
'balance' => 0,
'reserve' => 0,
];
public static function createRules()
{
return [
'budget_code' => 'required|unique:acq_m_budgets,budget_code|max:15',
'budget_name' => 'required|max:100',
'ma_code' => 'max:10',
'start_period' => 'required',
'end_period' => 'required',
];
}
public static function updateRules($id)
{
return [
'budget_code' => 'required|unique:acq_m_budgets,budget_code,' . $id . '|max:15',
'budget_name' => 'required|max:100',
'ma_code' => 'max:10',
'start_period' => 'required',
'end_period' => 'required',
];
}
}
My Controller: BudgetController.php
...
public function create(Request $request)
{
$validateData = $request->validate(AcqMBudgets::createRules());
$model = new AcqMBudgets;
$post = $request->only($model->getFillable());
$post['start_period'] = (!empty($post['start_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['start_period']))) : null;
$post['end_period'] = (!empty($post['end_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['end_period']))) : null;
$model->fill($post);
$model->save();
return redirect()->route('acq.view.master.budget', ['id' => $model->budget_id, 'rf' => 'a']);
}
...
public function update($id, Request $request)
{
$validateData = $request->validate(AcqMBudgets::updateRules($request->input('budget_id')));
$model = AcqMBudgets::find($id);
$post = $request->only($model->getFillable());
$post['start_period'] = (!empty($post['start_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['start_period']))) : null;
$post['end_period'] = (!empty($post['end_period'])) ? date('Y-m-d', strtotime(str_replace('/', '-', $post['end_period']))) : null;
$model->fill($post);
$model->save();
return redirect()->route('acq.view.master.budget', ['id' => $model->budget_id, 'rf' => 'e']);
}
...
On the model, I already separated the rules for create and update method. The difference is in the updateRules(), there is a primary key parameter which is needed in the array of rules.
On the controller, on update function, there is an error which stated: SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist LINE 1: ...from "acq_m_budgets" where "budget_code" = $1 and "id" <> $2 ^ (SQL: select count(*) as aggregate from "acq_m_budgets" where "budget_code" = N01 and "id" <> ).
The primary key I used is integer and incremental, but due to some circumstances, the name of the primary key cannot be just id, so I changed it into budget_id and already declared it at the beginning of the model. Going by the error message, it seems Laravel keeps trying to compare with this id field instead the one I declared. What needs to be done to fix this?
UPDATE IN CODE:
I used Rule namespace on createRules and updateRules on model:
public static function createRules()
{
return [
'budget_code' => ['required', Rule::unique('acq_m_budgets', 'budget_code'), 'max:15'],
'budget_name' => ['required', 'max:100'],
'ma_code' => ['max:10'],
'start_period' => ['required'],
'end_period' => ['required'],
];
}
public static function updateRules($id)
{
return [
'budget_code' => ['required', Rule::unique('acq_m_budgets', 'budget_code')->ignore($id, 'budget_code'), 'max:15'],
'budget_name' => ['required', 'max:100'],
'ma_code' => ['max:10'],
'start_period' => ['required'],
'end_period' => ['required'],
];
}
When I tried to update the data, I made changes to some fields except the budget_code. The changes won't be saved if I didn't change the budget_code field as well, since it always give an error: "budget_code" has already been taken. I use dd($post), and the fields I changed is passed on perfectly.
I would use the Rule namespace, where you can call unique through that. For this to work you have to use arrays, for validation rules instead of strings, this is the better approach for readability anyways.
Rule::unique has the method ignore() where the second parameter is the id column, this can be seen here.
'budget_code' => [
'required',
Rule::unique('acq_m_budgets', 'budget_code')->ignore($id, 'budget_id'),
'max:15'
]
I am trying to update date_of birth column in database and when I submit my form I get this error
DateTime::__construct(): Failed to parse time string (25/03/1995) at position 0 (2): Unexpected character
Now in my blade I formated date of birth to show d/m/Y and when updating I think it updates Y/m/d, because when I remove format function from my blade it works fine. So I need help on how to update with format('d/m/Y') in my database and how to validate it properly in my form request validation. Any help is appreciated. Here is my code.
index.blade.php
<input type="text" placeholder="dd/mm/yyyy" name="date_of_birth" value="{{ $userForShowProfile->date_of_birth ? $userForShowProfile->date_of_birth->format('d/m/Y') : "" }}">
UserController.php
public function updateProfileCharacteristics(UpdateProfileCharacteristicsRequest $request)
{
$user = Auth::user();
$user->update(
[
'date_of_birth' => $request->date_of_birth,
'age' => Carbon::now()->diffInYears($request->date_of_birth),
'updated_at' => Carbon::now()
]
);
return redirect()->route('profile.show', [$user->username]);
}
UpdateProfileCharacteristicsRequest.php
public function rules()
{
return [
'date_of_birth' => ['date'],
];
}
Since you are sending the date in a custom format in the request, you will need to parse it to a format that matches the one in the database column before inserting it:
$user->update(
[
'date_of_birth' => Carbon::createFromFormat("d/m/Y", $request->date_of_birth)->format('Y-m-d'), // parse the right format here
'age' => Carbon::now()->diffInYears(Carbon::createFromFormat("d/m/Y", $request->date_of_birth)),
'updated_at' => Carbon::now()
]
);
And for that date format to pass validation you can use the date_format:format rule instead of date:
public function rules()
{
return [
'date_of_birth' => ['date_format:"d/m/Y"'],
];
}
What is the column type in you database migration? If it is check if it DATE or DATETIME or TIMESTAMP, It is supposed to be DATE hence you can format your date to be Y-m-d.
If you are to save a DATE to DB it should be in the format of Y-m-d.
so try this:
public function updateProfileCharacteristics(Request $request)
{
$user = Auth::user();
$user->update([
'date_of_birth' => Date('Y-m-d',strtotime($request->date_of_birth)),
'age' => Carbon::now()->diffInYears($request->date_of_birth),
'updated_at' => Carbon::now()
]);
return redirect()->route('profile.show', [$user->username]);
}
I am using laravel 5.8
I want to create an invoice of the customer on login,
the scenario is that I want to check on login that if the due date of an invoice is equal to the current time so create new invoice of the same customer.
here is my code
Login Controller
public function login(Request $req)
{
$this->validate($req, [
'email' => 'required',
'password' => 'required'
]);
if (\Auth::attempt(['email' => $req->email, 'password' => $req->password])) {
$today = Carbon::now()->format('Y-m-d');
$all = Invoice::where('due_date', $today)->get()
foreach($all as $row) {
$addinvoice = Invoice::create([
'customer_id' => $row->customer_id,
'account_title' => $row->account_title,
'slug' => str_slug($row->account_title),
'perpared_date' => $today,
'amount' => $row->amount,
]);
if ($row->due_date == '1 month') {
$interval = $today->addMonths()->format('Y-m-d');
}
if ($row->due_date == '3 month') {
$interval = $today->addMonths(3)->format('Y-m-d');
}
if ($row->due_date == '6 month') {
$interval = $today->addMonths(6)->format('Y-m-d');
}
if ($row->due_date == '12 month') {
$interval = $today->addMonths(12)->format('Y-m-d');
}
$addinvoice['due_date'] = $interval;
}
return redirect()->to('/admin/customers/list');
} else {
return redirect()->back()->with(['msg' => 'Invalid Email or Password']);
}
}
Here I am getting an error after login
syntax error, unexpected 'foreach' (T_FOREACH)
Can one help me in fixing it?
you forgot semicolon for this line
$all = Invoice::where('due_date', $today)->get()
put semicolon for->get()
I want to fill the table blog_posts using the faker, but I get this error.
InvalidArgumentException : indexSize must be at most 5
BlogPostFactory.php
<?php
/* #var $factory \Illuminate\Database\Eloquent\Factory */
use App\Models\BlogPost;
use Faker\Generator as Faker;
$factory->define(BlogPost::class, function (Faker $faker) {
$title = $faker->sentence(rand(3, 8), true);
$text = $faker->realText(1000, 8000);
$isPublished = rand(1, 5) > 1;
$created_At = $faker->dataTimeBetween('-2 months', '-3 months');
$data = [
'category_id' => rand(1, 11),
'user_id' => (rand(1, 5) == 5 ) ? 1 : 2,
'title' => $title,
'slug' => Str::slug($title),
'excerpt' => $faker->text(rand(40,100)),
'content_raw' => $text,
'content_html' => $text,
'is_published' => $isPublished,
'published_at' => $isPublished ? $faker->dataTimeBetween('-2 months',
'-1 days'): null,
'created_at' => $created_At,
'updated_at' => $created_At
];
return $data;
});
DatabaseSeeder.php
<?php
use App\Models\BlogPost;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(BlogCategoriesTableSeeder::class);
factory(BlogPost::class, 100)->create();
}
}
I use Laravel 5.8
Looking at the error stack trace, it is pointing to a line in the Faker code for realText:
public function realText($maxNbChars = 200, $indexSize = 2)
{
if ($indexSize > 5) {
throw new \InvalidArgumentException('indexSize must be at most 5');
}
}
Thus, Faker only allows an index size of 5 at most. So, to fix your issue, change your code from:
$text = $faker->realText(1000, 8000);
To something less than 5 on the second parameter:
$text = $faker->realText(100, 4);
Hope this helps
I want to use single validate for birthday year, birth month, birth day as birthday for registration in laravel 5.4 here is my code
public function register(Request $request)
{
// here i want to add bithday input on reqeust but nothing happen
$request->birthday = implode('-', array(
$request->birth_year,
$request->birth_month,
$request->birth_date
));
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
return redirect()->back()->with('info', 'User successfully registered');
}
nothing happen with that code, I can validate those 1 by 1 using date_format. the problem is what if the user select February and day is 31
According to the source, you can use the merge method.
$request->merge(['birthday' => implode('-', [
$request->birth_year,
$request->birth_month,
$request->birth_date
])]);
There are many ways to do that. For example, you can use add() method to add data to the Request object:
$request->request->add(['birthday', implode('-', [
$request->birth_year,
$request->birth_month,
$request->birth_date
)]);
But here, I'd just do something like this:
$data = $request->all();
$data['birthday'] = implode('-', [
$request->birth_year,
$request->birth_month,
$request->birth_date
]);
$this->validator($data)->validate();
event(new Registered($user = $this->create($data)));
my way is:
$all = $request->all();
$year = $all['birth_year'];
$month = $all['birth_month'];
$day = $all['birth_date'];
// Create Carbon date
$date = Carbon::createFromFormat('Y-m-d', $year.'-'.$month.'-'.$day);
// $date = Carbon::createFromFormat('Y-m-d', $request->birth_year.'-'.$request->birth_month.'-'.$request->birth_date); another way
//add new [birthday] input
$request->request->add(['birthday' => $date->format('Y-m-d')]);
$validatedData = $request->validate([
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'password' => 'required|string',
'birthday' => 'required|date_format:Y-m-d|before:today',// validate birth day
]);
Hope this helps someone