Email failures in Laravel - laravel

I am trying to send mail using below code
Mail::send(new ContactUs($request));
if(Mail::failures()){
return response()->json(['result' => 1]);
}
else {
return response()->json(['result' => 0]);
}
But I am not getting any response from Mail::failures() section.

Instead of using Mail::failures() you can use !empty(Mail::failures()) or count(Mail::failures()) > 0.
Mail::failures() Function returns an array of Email addresses which are failed.
array failures()
Get the array of failed recipients.
Return Value
array
You can read it here: https://laravel.com/api/5.1/Illuminate/Mail/Mailer.html#method_failures

Related

Laravel validation couldn't store value after validate and give error 500

I have a form that using ajax for update data client. In that form there is an input file. Everything is going fine except for updating the file. File is sent, it changed on storage too, but it gives error on validation and didn't change data on database.
Here is the code on the controller :
public function update(Request $request, Client $client)
{
$validatedData = Validator::make($request->all(), [
'name' => 'required|max:255',
'logo'=> 'image|file|max:100',
'level' => 'required|max:1'
]);
$validatedData['user_id'] = auth()->user()->id;
if ($validatedData->fails()){
return response()->json($validatedData->errors());
} else {
if($request->file('logo')){
if($request->oldLogo){
Storage::delete($request->oldLogo);
}
$validatedData['logo'] = $request->file('logo')->store('logo-clients');
}
$validateFix = $validatedData->validate();
Client::where('id', $client->id)->update($validateFix);
return response()->json([
'success' => 'Success!'
]);
}
}
It gives error on line :
$validatedData['logo'] = $request->file('logo')->store('logo-clients');
With message :
"Cannot use object of type Illuminate\Validation\Validator as array"
I use the same code that works on another case, the difference is the other not using ajax or I didn't use Validator::make on file input. I guess it's just wrong syntax but I don't really know where and what it is.
To retrieve the validated input of a Validator, use the validated() function like so:
$validated = $validator->validated();
Docs:
https://laravel.com/docs/9.x/validation#manually-creating-validators
https://laravel.com/api/9.x/Illuminate/Contracts/Validation/Validator.html
$validatedData is an object of type Illuminate\Validation\Validator.
I would say the error is earlier there as well as this line should give an error also:
$validatedData['user_id'] = auth()->user()->id;
As ericmp said, you first need to retrieve the validateddata to an array and then work with it.

Return Laravel options() as array when no optional parameter has been provided

Laravel has the super handy optional() helper.
I would like to combine it with a custom Model attribute like this:
// this method is on the User model
public function getDataAttribute()
{
// this data comes from another service
$data = [
'one' => 1,
'two' => 2,
];
return optional($data);
}
So I can use it like this:
$user->data->one // 1
$user->data->two // 2
$user->data->three // null
However, I am also trying to return the entire array by doing:
dump($user->data); // this should dump the internal $data array
But this will return an instance of Illuminate\Support\Optional with a value property.
Illuminate\Support\Optional {#1416 ▼
#value: {#2410 ▼
+"one": 1
+"two": 2
}
}
Is it possible to return the original $data array if no "sub"parameter (= a child attribute of $user->data) is given? Or is there a possibility to detect a child parameter in the getDataAttribute()?
I hope it's clear what I am trying to achieve.
What you're asking for cannot be achieved.
My suggestion would be to keep things simple and define a getter method and pass the key of the array you want and from there return your data respectively, e.g.:
public function getData($key = null) {
$data = [
'one' => 1,
'two' => 2,
];
if (!$key) {
return $data;
}
return $data[$key] ?? null;
}
Notice also how this method is no longer an attribute, this is because AFAIR, you cannot pass args to attribute methods.
Reading Material
Null coalescing operator
Thanks to lagbox for pushing me in the right direction. I have solved this by using the following macro:
Illuminate\Support\Optional::macro('toArray', function()
{
return (array) $this->value;
});
This way I can access all data by using:
$user->data->toArray();

using bcc function gives error :Address in mailbox given [$users] does not comply with RFC 2822, 3.6.2

i have to send the bulk emails using bcc but it gives error,i also read somewhere bcc function takes 2 parameters but this also didn't work.var_dump($users) it giving correct output
public function welcomeEmails()
{
$users = User::select('email')->whereIn('id',[5,6,7]->get()->toArray();
Mail::send('emails.welcome_email', [], function($message) use ($users)
{
$message->to(abc#gmail.com)
->bcc('$users')
->subject('Welcome to the jobsee');
});
Session::flash('success', 'Your message was sent!');
return redirect()->back();
}
This error is probably due to an invalid email address. This error is usually returned by the server and has nothing to do with Laravel.

Laravel: How to pass a collection to view?

The $result variable displays the json-string correctly. And when I try to send the result to the view, I get an error.
$accreditations = Accreditation::with(['country'])->get();
$result = AccreditationResource::collection($accreditations);
//return $result;
//{"data":[{"id":5,"user_id":1,"login":"Admin","country_id":"4","country":{"id":4,"name":"Austria"}}]}
return View::make('accred_table', $result->login);
//Error 500: Property [login] does not exist on this collection instance.
Help me figure this out
$result is an array. So, there is no login as the error said. If you return only one record from AccreditationResource, you can use
return view('accred_table', ['login' => $result[0]['login']]);
In blade, you will have $login.

Laravel: Trying to push url strings to array in database

I have a field in my AppSetup table called reference_images that is added as follows in the migration: $table->json('reference_images')->nullable(); and casted as an array in the AppSetup model. I have a method that adds image urls to the database. If it's for the reference_image field, I am trying to push it to the array but I keep getting an "Array to String conversion" error in Laravel.
public function addImagesToAppSetup($imageType=null, $image=null)
{
$appSetup = AppSetup::where('store', '=', id())->first();
if ($image) {
if ($imageType == "reference_images") {
$originalArray = $appSetup->$imageType;
$originalArray[] = $image;
$appSetup->$imageType = array_values(array_unique($originalArray));
} else {
$appSetup->$imageType = $image;
}
}
$appSetup->save();
return response()->json(['status' => 1, 'data' => $appSetup]);
}
Since reference_images is of type json, doing array_values(array_unique($originalArray)); is still an Array. You will have to convert it to json.
e.g. using collect()->json() docs
$appSetup->$imageType = collect($originalArray)->unique()->values()->toJson();

Resources