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()
Related
This is my code in the controller store function, and I want to clean my codes in the new instance of $adminActivityLog.
public function store(UserAdminRequest $request)
{
if (Gate::denies('create_super_admin')) {
return \view('errors.403');
}
$save_account = User::create([
'name' => $request->name,
'email' => $request->email,
'branch_office_unit' => $request->branch,
'remark' => $request->remark,
'password' => $request->password,
'access_type_id' => $request->access_type_id
]);
$adminActivityLog = new AdminActivityLog();
$admin = Auth::user()->load('access');
$dt = Carbon::now()->format('d-M-Y');
$dts = Carbon::now()->timezone('Asia/Hong_Kong')->format('H:i:s');
$time = $dts;
$date = $dt;
$adminActivityLog->access_type_name = $admin->access->access_type_name;
$adminActivityLog->activity = 'Created Admin Account: ' . ' ' . $request->name;
$adminActivityLog->time = $time;
$adminActivityLog->date = $date;
$adminActivityLog->save();
return \redirect()->route('system.admin')->with('success', 'Created successfully');
}
Thank you for you kind and respectful answer <3
I'm still a beginner in Laravel, and I'm confused by this error.
Undefined variable: today (View: D:\Documents\My
Project\todo-app\resources\views\edit.blade.php)
This my error
this my controller
public function update(Request $request, $id)
{
$this->validate($request,[
'title' => 'required|string|max:25',
'finished' => 'nullabel'
]);
$home = Home::find($id);
$home->title = $request->input('title');
if($request->has('finished')){
$home->finished = true;
}
$home->user_id = Auth::user()->id;
$home->save();
$mytime=time::now();
$date=$mytime->toRfc850String();
$today= substr($date, 0, strrpos($date, ","));
$date = Carbon::now()->format('d-m-Y');
return redirect('/home', compact('homes'), [
'title'=>'Home',
'date' => $date,
'today' => $today
]);
}
What should I do in my controller? is calling $today correct? I'm so confused please
All things are right, but does not declare the variable for today($today)
Even the date variable is also declared. That one also you should be declared.
public function update(Request $request, $id)
{
$this->validate($request,[
'title' => 'required|string|max:25',
'finished' => 'nullabel'
]);
//This line added
$today = date('Y-m-d H:i:s');
$date = date('Y-m-d H:i:s');
$home = Home::find($id);
$home->title = $request->input('title');
if($request->has('finished')){
$home->finished = true;
}
$home->user_id = Auth::user()->id;
$home->save();
return redirect('/home', compact('homes'), [
'title'=>'Home',
'date' => $date,
'today' => $today
]);
}
Then its works 100%.....
Happy Coding and thanks :)
$project = new Project;
$cities = City::all();
$states = State::all();
$amenities = Amenity::query()->active()->get();
$brands = Brand::all();
$unit_types = UnitType::query()->active()->get();
$accommodations = AccommodationType::query()->active()->get();
return view('admin.projects._form', compact('project', 'cities', 'states', 'amenities', 'brands', 'unit_types', 'accommodations'));
You can send multiple variable like above code
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
I am trying to update attendance column outtime of all selected employees with checkout time.During intime check-in time successfully stored in the database, but I am getting trouble with the update. I did something like below, but it's updating the only first row. What should be written query would someone help me, please?
AttendanceController.php
public function checkout(Request $request)
{
$request->validate([
'outtime' => 'required',
'employee_id' => 'required',
]);
$outTime = Carbon::parse($request->outtime)->format('H:i:s');
$date = date('Y-m-d');
$employeeIds = $request->employee_id;
$data = [];
foreach ($employeeIds as $employeeId) {
$data[] = [
'employee_id' => $employeeId,
'attendance_date' => $date,
];
}
$update = Attendance::where('employee_id', $data)
->where('attendance_date', $date)
->update([
'outtime' => $outTime
]);
return back()->with('success', 'Checked Out');
}
Use this:
whereIn('employee_id', $employeeIds)
I'm tying to figure out how to save, for example 4 days of schedule in one view, and have each field validation message show if validation fails.
My approach was at first to use $this->SomeModel->saveAll() but couldn't save so I tried another way using foreach and all data is saved (pass validation) but no validation message is shown. if you guys know better way to do this I'm open for any suggestions.
Model
public $validate = array(
'hour_from'=>array(
'some mgs' => array(
'rule' => 'good_hours',
),
),
);
public function good_hours($data) {
//throw new Exception(($data['hour_from'] >= $this->data['Hour']['hour_to']));
if ($data['hour_from'] >= $this->data['Hour']['hour_to']) {
return false;
}
return true;
}
Controller:
if ($this->request->is('post')) {
$all_good = true;
foreach ($this->request->data['Hour'] as $day){
if ($this->Hour->save($day)){
}else {
$all_good = false;
$this->Session->setFlash('hours not saved');
}
}
//if all saves are correct rediredt to index
if ($all_good) {
$this->Session->setFlash(__('Hours saved'));
return $this->redirect(array('action' => 'index'));
}
}
View
foreach ($days as $count => $day):
$form_model ='Hour.'.$count. '.';
?>
<fieldset>
<legend><?php
$day_array = (array) $day;
$day = $day_array['date'];
echo $day;
?></legend>
<?php
echo $this->Form->input($form_model.'type_holiday_id', array(
'label'=> 'Typ urlopu',
'type' => 'select',
'options' => $type_holidays,
'empty' => true
));
echo $this->Form->input($form_model.'hour_from', array('label' => 'od'));
echo $this->Form->input($form_model.'hour_to', array('label' => 'do'));
echo $this->Form->input($form_model.'date', array('type' => 'hidden', 'value' => $day));
echo $this->Form->input($form_model.'subordinate_id', array('type' => 'hidden', 'value' => $user['User']['id']));
echo $this->Form->input($form_model.'supervisor_id', array('type' => 'hidden', 'value' => $current_user['id']));
?>
</fieldset>
Request->data array
Hour(array)
0(array)
type_holiday_id
hour_from 8
hour_to 15
date 2014-01-20
subordinate_id 193
supervisor_id 557
1(array)
type_holiday_id
hour_from 7
hour_to 14
date 2014-01-21
subordinate_id 193
supervisor_id 557
Ok i found solution, and everything works perfectly now in controller i needed to change save to saveAll, function add in Controller should look like this:
if ($this->request->is('post')) {
if ($this->Hour->saveAll($this->request->data['Hour'], Array('validate' => 'first', 'deep' => true))){ <--- most important is that data['Hour']
$this->Session->setFlash(__('Godziny robocze zapisane'));
return $this->redirect(array('action' => 'index'));
} else{
$this->Session->setFlash('Godziny robocze nie zostaĆy zapisane.');
}
}