i have a problem this controller is not working how can i do? should send mutiple emails how do i solve?
I don't know how to handle it
function submit(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'file' => 'mimes:pdf,doc,docx'
]);
$data = array(
'name' => $request->name,
'cognome' => $request->cognome,
'luogo' => $request->luogo,
'date' => $request->date,
'telefono' => $request->telefono,
'email' => $request->email,
'citta' => $request->citta,
'provincia' => $request->provincia,
'studio' => $request->studio,
'lingua' => $request->lingua,
'livello' => $request->livello,
'lingua2' => $request->lingua2,
'livello2' => $request->livello2,
'file' => $request->file,
'agree' => $request->agree
);
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('luis#gmail.com', 'luis')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if($request->hasFile('file')){
$message->attach($request->file('file')->getRealPath(), array(
'as' => $request->file('file')->getClientOriginalName(),
'mime' => $request->file('file')->getMimeType())
);
}
});
Session::flash('success', 'Mail spedita con sucesso');
}
I wish I could solve the problem
any advice? on how to do it?
Related
How to set custom validation rule that accept only 1.00, 1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 2.75 and 3.00 in input text? The grades and grades1 are my inputs.
1.35 should be invalid (incase you're thinking the solution of divisible by 5)
1.26 should be invalid
FormRequest
public function rules()
{
return [
'user_id' => 'required',
'app_id' => 'nullable',
'subjects.*' => 'required|string',
'subjects1.*' => 'required|string',
'school_year' => 'required',
'grades.*' => ['required','numeric','lt:2.50'],
'grades1.*' => 'required|lt:2.50',
'units.*' => 'required|integer|min:1',
'units1.*' => 'required|integer|min:1',
'term' => 'required',
'term1' => 'required',
'total.*' => 'nullable',
'total1.*' => 'nullable',
'gwa_1st' => 'required|lte:1.75',
'gwa_2nd' => 'required|lte:1.75',
'year_level' => 'required|string',
'image' => 'required|mimes:jpeg,png,jpg',
'award_applied' => 'required|string',
'course_id' => 'required|string'
];
}
Controller
public function store(AchieversAwardRequest $request)
{
$data = $request->validated();
$award = new StudentApplicants();
$award->user_id = $data['user_id'];
$award->school_year = $data['school_year'];
$award->gwa_1st = $data['gwa_1st'];
$award->gwa_2nd = $data['gwa_2nd'];
$award->year_level = $data['year_level'];
if ($request->hasfile('image')) {
$file = $request->file('image');
$filename = time() . '.' . $file->getClientOriginalExtension();
$file->move('uploads/', $filename);
$award->image = $filename;
}
$award->award_applied = $data['award_applied'];
$award->course_id = $data['course_id'];
$award->save();
$lastid = $award->id;
}
One possible solution would be to use the in validation rule, something like the following:
$acceptable = [1.00, 1.25, 1.5, 2.75, 3.00];
$validator = Validator::make(['numbers_field' => 2.75], [
'numbers_field' => [Rule::in($acceptable)]
]);
Update
Add the rule to your AchieversAwardRequest custom FormRequest rules() method along with all your other rules:
public function rules()
{
return [
'user_id' => 'required',
'app_id' => 'nullable',
'subjects.*' => 'required|string',
'subjects1.*' => 'required|string',
'school_year' => 'required',
'grades.*' => ['required','numeric','lt:2.50'],
'grades1.*' => 'required|lt:2.50',
'units.*' => 'required|integer|min:1',
'units1.*' => 'required|integer|min:1',
'term' => 'required',
'term1' => 'required',
'total.*' => 'nullable',
'total1.*' => 'nullable',
'gwa_1st' => 'required|lte:1.75',
'gwa_2nd' => 'required|lte:1.75',
'year_level' => 'required|string',
'image' => 'required|mimes:jpeg,png,jpg',
'award_applied' => 'required|string',
'course_id' => 'required|string'
'field_name' => [Rule::in([1.00, 1.25, 1.5, 2.75, 3.00])]
];
}
Obviously, replace field_name with the name of your input field. You will also need to include use Illuminate\Validation\Rule; at the top of your AchieversAwardRequest.
I'm still new to laravel and I have a simple app and aSo I have a route that will store data based on the request in my controller.
public funtion store(Request $request, $id){
if ($request->has('work_experiences')) {
WorkExperience::create([
'user_id' => $user->id,
'position' => $request->work_experiences['position'],
'company' => $request->work_experiences['company'],
'start_date' => $request->work_experiences['start_date'],
'end_date' => $request->work_experiences['end_date'],
]);
}
if ($request->has('education')) {
Education::create([
'user_id' => $user->id,
'degree' => $request->education['degree'],
'university' => $request->education['university'],
'start_date' => $request->education['start_date'],
'end_date' => $request->education['end_date'],
]);
}
if ($request->has('job_interests')) {
JobInterest::create([
'user_id' => $user->id,
'job_position' => $request->job_interests['position'],
]);
}}
}
and in my test
public function test_authenticated_user_can_edit_education_profile()
{
$this->withoutExceptionHandling();
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post('/candidate' . '/' . $user->id, [
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
]);
$this->assertCount(1, WorkExperience::all());
}
when I run the test, the assertCount seems to fail because the response didn't work/insert the data to DB. where do I do wrong?
Well, the test is right.
It should fail because there is no work_experiences key in your request data.
The test request should look like:
$response = $this->post('/candidate' . '/' . $user->id, [
'work_experiences' => [
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
]
]);
So your data should go under a work_experiences key such that $request->has('work_experiences') returns true and executes the WorkExperience::create() statement.
Currently your endpoint only allows for a single "work experience" to be created. Seeing that you've named it work_experiences I assume you'd want to pass in an array/collection of "work experiences" - but that won't work with the current implementation; you'll have to loop over them instead - something like this:
if ($request->has('work_experiences')) {
foreach ($request->input('work_experiences') as $experience) {
WorkExperience::create([
'user_id' => $request->user()->id,
'position' => $experience['position'],
'company' => $experience['company'],
'start_date' => $experience['start_date'],
'end_date' => $experience['end_date'],
]);
}
}
And then your test should look something like this:
$response = $this->post('/candidate' . '/' . $user->id, [
'work_experiences' => [
[
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
],
// more "work experiences"
]
]);
In the given scenario
request()->validate([
'type' => 'required',
'category' => 'required'
]);
and Again
request()->validate([
'name' => 'required',
'gender' => 'required
]);
Is it possible to get some sort of centralized or complied error that encompasses both the validations?
Then you should use Validator facade to handle this kind of cases.
for ex.
$validator = Validator::make($request->only('type', 'category), [
'type' => 'required',
'category' => 'required'
]);
$validator2 = Validator::make($request->only('name', 'gender'), [
'name' => 'required',
'gender' => 'required'
]);
if ($validator->fails() || $validator2->fails()) {
// return merge $validator->errors() and $validator2->errors();
}
I am using Laravel-5.8 as backend for an application. I have written all the Api for the endpoints.
Laravel: ApiController
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
//'email' => 'required|email|unique:users|max:255',
'phone' => 'required|max:14',
'business_name' => 'required',
'truck_type' => 'required',
'truck_required' => 'required',
'quote_origin' => 'required',
'quote_destination' => 'required',
'commodity' => 'required',
// 'weight' => 'required',
'loading_date' => 'required'
]);
$clientquote = new ClientQuote([
'first_name' => $request->first_name,
'last_name'=> $request->last_name,
'email' => $request->email
'phone' => $request->phone,
'business_name' => $request->business_name,
'address' => $request->address,
'comment' => $request->comment,
'truck_type' => $request->truck_type,
'truck_required' => $request->truck_required,
'quote_origin' => $request->quote_origin,
'quote_destination' => $request->quote_destination,
'commodity' => $request->commodity,
'loading_date' => $request->loading_date
]);
$clientquote->save();
$mainData = array();
$mainData['to'] = $clientquote->toArray()['email'];
$mainData['from'] = "support#tsllimited.com";
$mainData['subject'] = "Client Quote";
$mainData['content'] = "Your Quote have been successfully received. You will hear from us shortly through the provided email. Thank you!";
$this->mailSend($mainData);
return response()->json([
'message' => 'Quote Successfully Sent!'
], 201);
}
public function indexClientQuote(Request $request) {
return response()->json(ClientQuote::get());
}
When I tested the Request on the POSTMAN, I got the error shown below:
I don't know why it's expecting ']'.
What could have caused the error?
you missed ',' after email
'email' => $request->email
'phone' => $request->phone,
To
$clientquote = new ClientQuote([
'first_name' => $request->first_name,
'last_name'=> $request->last_name,
'email' => $request->email, //here you missed comma
'phone' => $request->phone,
'business_name' => $request->business_name,
'address' => $request->address,
'comment' => $request->comment,
'truck_type' => $request->truck_type,
'truck_required' => $request->truck_required,
'quote_origin' => $request->quote_origin,
'quote_destination' => $request->quote_destination,
'commodity' => $request->commodity,
'loading_date' => $request->loading_date
]);
I have this method to store new user
public function register(CreateUserRequest $request){
$file = $request->file('idcard');
$fileName = rand(0, 99999).$file->getClientOriginalName();
if($request->hasFile('idcard') && $request->file('idcard')->isValid()){
$request->file('idcard')->move("images/idcard/", $fileName);
}
User::create([
'role_id' => 1,
'email' => $request->email,
'password' => $request->password,
'full_name' => $request->full_name,
'address' => $request->address,
'phone' => $request->phone,
'family_name' => $request->family_name,
'family_address' => $request->family_address,
'family_phone' => $request->family_phone,
'idcard' => $fileName,
'status' => 'unconfirmed',
'balance' => 0,
]);
Session::flash('success', 'Please check your email to activate your account.');
return redirect('/register');
}
And I have this unit test
public function testNewUserRegistration()
{
$this->visit('/register')
->type('This is full name', 'full_name')
->type('JL. Mulyorejo 226 D', 'address')
->type('085788884877', 'phone')
->type('Rizky Sugiarto', 'family_name')
->type('JL. Kandangan', 'family_address')
->type('085766669999', 'family_phone')
->attach('/var/www/html/autodealer/images/pics/banner_car.jpg', 'idcard')
->type('mail#yahoo.co.id', 'email')
->type('123456789', 'password')
->type('123456789', 'password_confirmation')
->press('Submit')
->seeInDatabase('users', ['email' => 'mail#yahoo.co.id', 'role_id' => 1, 'balance' => 0, 'status' => 'unconfirmed'])
->seePageIs('/register')
->see('Please check your email to activate your account.');
}
If I test my function via browser, it successfuly input database and move file.
But when I test via PHPUnit it pass the test, successfully input database but the image doesn't move.
Is there something wrong with my attach() or something else wrong?
Thanks, any help appreciated.