Laravel: Send email to multiple user - laravel

I want to send email in laravel. 2 user get different email content. So, here is my code.
TransactionController.php
use App\Mail\MailFruitCustomer;
use App\Mail\MailFruitSeller;
$data = array(
'fruitid' => 'F01',
'fruitname' => 'Banana',
'customercode' => 'B2345',
'sellercode' => 'S9546'
);
Mail::to(customer1#mail.com)->send(new MailFruitCustomer($data));
Mail::to(seller1#mail.com)->send(new MailFruitSeller($data));
MailFruitCustomer.php (In Mail folder)
public function __construct($data){
$this->data = $data;
}
public function build(){
$result = $this->from('admin#mail.com')
->subject('Customer New Transaction')
->markdown('emails/customerreceipt')
->with('data', $this->data);
return $result;
}
MailFruitSeller.php (In Mail folder)
public function __construct($data){
$this->data = $data;
}
public function build(){
$result = $this->from('admin#mail.com')
->subject('Seller New Transaction')
->markdown('emails/sellerreceipt')
->with('data', $this->data);
return $result;
}
customerreceipt.blade.php
#component('mail::message')
Customer Receipt Detail
Fruit ID: {{ $data['fruitid'] }}
Fruit Name: {{ $data['fruitname'] }}
Customer Code: {{ $data['customercode'] }}
Thank you.<br>
#endcomponent
sellerreceipt.blade.php
#component('mail::message')
Seller Receipt Detail
Fruit ID: {{ $data['fruitid'] }}
Fruit Name: {{ $data['fruitname'] }}
Seller Code: {{ $data['sellercode'] }}
Thank you.
#endcomponent
It is working and both user get email with different content. But the process take longer time. Is there any method of mail in laravel that i could apply?
Any help will be grateful. Thank you.

You are searching for Queues: https://laravel.com/docs/7.x/queues
You create a job that sends the mail and queue the job. Laravel can send the response faster and the Mail is sent asynchronous.
Additional: https://laravel.com/docs/7.x/mail#queueing-mail

I continue to learn Queues and create job that sends mail.
I got another problem and fix it with help from others. Thank you.
Here is the link https://stackoverflow.com/a/63370095/12022919.

Related

Laravel: Access collection in view

I would like to display users that have sent or got message/s.
User Model:
public function messages_sender()
{
return $this->hasMany('App\Message', 'sender_id');
}
public function messages_recipient()
{
return $this->hasMany('App\Message', 'recipient_id');
}
MessageController
public function index()
{
$users = User::with('messages_recipient', 'messages_sender')->paginate(5);
//return $users;
return view('message.index',compact('users'));
}
Messages View:
{{ $user->messages_recipient['recipient_id'] }}
Result is an error in view "Undefined index: recipient_id" or when i try to access any of them (sender_id, recipient_id). I understand that not all users have messages, as i see in JSON format, so i used:
#if($user->messages_recipient->has('recipient_id'))
{{ $user->messages_recipient['recipient_id'] }}
#endif
and than error did not show.
JSON: https://pastebin.com/NFSWMp7r
As I am new to Laravel i could use some help. In similar example it worked when I needed to get users and name of their role (admin, user).
You can access the Users form the App\Message Model instead of the other way round.
Message Model:
public function senders()
{
return $this->belongsTo('App\Users', 'sender_id');
}
Message Controller
public function index()
{
$senders = Message::senders()->paginate(5);
return view('message.index',compact('senders'));
}
Dont blame me but I am not sure, if the sender_id is just right there :-P
Unfortunately, if you want unique records this will not work.
Your using the message_recipient as an array and if a key doesn't exist on an array PHP will always throw an error. The hasMany relationship returns a Collection of Message objects so you would need to use it as an object with:
#foreach($user->messages_sender as $message)
{{ $message->id }}
#endforeach
and
#foreach($user->messages_recipient as $message)
{{ $message->id }}
#endforeach
or if you just wanted all of the message ids you could do $user->messsages_recipient->pluck('id') and that would return all of the ids of the Message objects.

laravel 5 show is returning empty query and not showing any data

i am new to laravel and i work on eloquent i want to send some data to view and show them like laravel documentation says so in my controller i wrote
public function show(PhoneBook $phoneBook)
{
return view('admin.phonebook.show',compact('phoneBook',$phoneBook));
}
and in the phonebook show view i wrote
<strong>id</strong> {{ $phoneBook->id }}<br>
<strong>calldate</strong> {{ $phoneBook->calldate }}<br>
<strong>description</strong> {{ $phoneBook->description }}
any idea why i receive empty query with no result with no errors and just the titles with no result in front of them i thank you if you can help me with this
Controller:
public function show($id)
{
$phonebook = Phonebook::find($id);
return view('admin.phonebook.show',compact('phonebook'));
}
Blade:
<strong>id</strong> {{ $phonebook->id }}<br>
<strong>calldate</strong> {{ $phonebook->calldate }}<br>
<strong>description</strong> {{ $phonebook->description }}
Try this:-
return view('admin.phonebook.show', compact('phoneBook'));
You can try 2 ways below to send data from Controller to View:
return view('admin.phonebook.show', compact('phoneBook'));
OR
return view('admin.phonebook.show')->with('phoneBook', $phoneBook);

How can I handle Blocked Users in laravel

I need a help . Can u guide me How can i Show a message to blocked user that his account has been blocked . i m just rendering him to post page . but i want to show him some message that your account has been blocked or somthing like we do in validation messages . Please guide briefly .
public function login(Request $request)
{
$username= $request->username;
$user = User::where('username',$username)->first();
// return $user;
// return $user->id;
if($user != null){
$active = Activation::whereUserId($user->id)->first();
if($active->completed==0){
return redirect('/posts');
}
You need to use session. One way to do that:
return redirect('posts')->with('error', 'Your account is blocked');
And in a view after redirection:
#if (session('error'))
{{ session('error') }}
#endif
If you don't want to redirect the user, just pass a variable into the view:
return view('login.form', ['notActivated' => $active->completed === 0])
And in the view:
#if ($notActivated)
Your account is blocked
#endif

Laravel5.2 delete doesn't work

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,
What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

Laravel 4 route NOT passing in a username

Hi, I have been trying to solve this issue for hours how, researched Stack Overflow and many other websites, but no luck.
//-------Route
Route::get('/profile/{firstName}', array(
'as' => 'userProfile',
'uses' => 'ProfileController#user'
));
//--------Controller
class ProfileController extends BaseController {
public function user($firstName) {
$user = User::where('firstName', '=', $firstName);
if($user->count()) {
//First record return from the query
$user = $user->first();
return View::make('profile.user')
->with('user', $user);
}
return "sorry, 404";
//return App::abort(404);
}
}
Finally link:
<li> My Profile </li>
------------------------------
*If I put the username into the URL manually, it works, however when I clicked on the link I get localhost:8000/profile/{firstName} with "sorry, 404"
Thank you for the help!!!
Your route userProfile requires the parameter firstName so you have to pass that in when generating the url:
{{ URL::route('userProfile', Auth::user()->firstName) }}
(How you get the firstName in the view doesn't matter I just used Auth::user since I thought this might be your use case)

Resources