laravel send mail to - laravel

Good evening everyone, I have some issues, with send mail to:
I have a code in controller, and have data taken from database with joins, this $survey variable shows gives me emails of the survey which belongs to team.
But I need some how to send an email for all emails which I gets from $survey variable. When I put $survey variable inside $message->to($survey); it shows me Undefined variable
my code below , how can I use it. When I put in ->to(); normal one email adress it works, but I need to send same email to all team members
public function startSurvey(Request $req) {
$nameSurvey = $req->input('SurveySelectBox');
$startDate = $req->input('surveyStartDate');
$endDate = $req->input('surveyEndDate');
$data = array(
'updated_at' => Carbon::now(),
'started_at' => $startDate,
'ended_at' => $endDate
);
DB::table('survey')->where('surveyId','=',$nameSurvey)->update($data);
$survey = Survey::where('surveyId' , '=', $nameSurvey)
->join('team','team.teamId', '=', 'survey.teamId')
->join('teammembersall','teammembersall.TeamId', '=', 'team.TeamId')
->join('users','users.id', '=', 'teammembersall.UserId')
->select('users.email')
->get();
Mail::raw('You have new survey to answer: http://localhost:8000/profile', function ($message) {
$message->from('kristijanask#gmail.com', 'New Survey released');
$message->to($survey);
});
return redirect('surveyDrafts');
}

You need to use use ($survey) to use the variable within the Mail anonymous function:
Mail::raw('...text', function ($message) use ($survey) {
$message->from('kristijanask#gmail.com', 'New Survey released');
$message->to($survey);
});
You might also need to use ->pluck() and toArray() on the query result, to make the result an array:
$survey = Survey::where('surveyId' , '=', $nameSurvey)
// long query
->get()
->pluck('email')
->toArray();

Related

I can't make use of url with arguments in laravel

I am working with a rest API, and I cannot do the following query, it throws me an empty array and not the answer that I want to see:
http://localhost/project-test/public/api/contactos/buscar?name=eric&surname=almendras
controller:
public function buscar($name,$surname){
$contacto = Contacto::where('name', $name,'surname',$surname)->get();
return $contacto;
}
route: (api.php)
Route::get('contactos/buscar/{name}/{surname}','ContactosController#buscar');
You define name and surname as url params, but you include it in query string.
To access it you need to use Request class:
public function buscar(Request $request){
$name = $request->input('name');
$surname = $request->input('surname');
$contacto = Contacto::where([
'name' => $name,
'surname' => $surname
])->get();
return $contacto;
}
or change url to:
http://localhost/project-test/public/api/contactos/buscar/eric/almendras
You need to put the 2 conditions in a separate where statement:
$contacto = Contacto::where('name', $name)->where('surname',$surname)->get();
As Daniel says you need to get the parameters from the Request, you then have different options when querying the model.
You can put multiple where clauses in an array, as long as you want all the operators to be =, passed to a single where:
$contacto = Contacto::where([
'name' => $name,
'surname' => $surname
])->get();
or an array of arrays if you want to use different operators:
$contacto = Contacto::where([
['name', '=', $name],
['surname', '<>', $surname]
])->get();
or use multiple where functions
$contacto = Contacto::where('name', $name)
->where('surname', $surname)
->get();
or use Laravel's magic methods:
$contacto = Contacto::whereName($name)
->whereSurname($surname)
->get();

add new element in laravel collection for friendship in laravel

I am trying to display all users weather I send him friend request or not with friendship status for that. First of all I load all friend requests and received request in that manner.
$users2 = User::with(['sentRequests' => function ($query) {
...
}])->with(['recievedRequests' => function ($query) {
...
}])->where('users.id', Auth::id())->get();
But then I also want to load other users so for that I create some query like that
$sent = Friendship::where('user_id', Auth::id())
->pluck('friend_id');
$received = Friendship::where('friend_id', Auth::id())
->pluck('user_id');
$users3 = User::whereNotIn('id', $sent)
->whereNotIn('id', $received)
->where('id', '!=', Auth::id())
->get();
Now I want to append $users3 to $users2 but when I done like that
$users2->put('otherUsers',$users3);
It appended but not in the right manner , I want to append it in same manner as 'sentRequests' and 'recievedRequests' are.

Query returning every row null in laravel

I'm trying to build a chat application using laravel echo and pusher, everything works but the data that returns to the databse is either null or the default value, here's the code
public function sendMessage(Request $request){
$conID = $request->conID;
$message1 = $request->message;
$user = Auth::user();
$fetch_userTo = DB::table('messages')
->where('conversation_id', $conID)
->where('user_to', '!=', Auth::user()->id)
->get();
$userTo = $fetch_userTo[0]->user_to;
$message = Message::create([
'user_from' => Auth::user()->id,
'user_to' => $userTo,
'conversation_id' => $conID,
'message' => $message1,
]);
if($message) {
$userMsg = DB::table('messages')
->join('users', 'users.id','messages.user_from')
->where('messages.conversation_id', $conID)->get();
broadcast(new MessagePosted($message))->toOthers();
return $userMsg;
}
}
NB: when i put insert() instead of create in the query the data goes through the database normally but there's an error in broadcasting
Have you tried to create a message like this? instead of using a model event?
$message = new Message;
$message->user_from = Auth::user()->id;
$message->$user_to = $userTo;
$message->conversation_id = $conID;
$message->message = $message1;
$message->save();
You have a lot more control this way, i.e
if($message->save()) { ... }
Or you could wrap the whole thing in a transaction?
Be sure your Message model allows the fields that you want to add in the $fillable array
Create method check fillable attributes into Laravel model. You have to write your all columns into fillable and then use create method.
Second solution is use Active Record technique. #Devin Greay answer is helpful to use Active record.
More information visit https://laravel.com/docs/5.6/eloquent#mass-assignment

Sending multiple emails at once using Laravel 4.2

I managed to make it work sending email to one user at a time but not to multiple users at one. Although i have tried many ways, below is a sample code using which I am trying to send multiple emails. 

if (isset($_POST['searchbutton'])) {
 $data = $query->where('blood_type', '=', "$search")
 ->Where('state', '=', "$state")->get();

 if (isset($POST['emailbutton'])) {
 foreach ($data as $row)
 Mail::send('emails.notify', array('name' => 'Name'), function($message) {

 $message->to($row->email, $row->name)->subject('Hello');
 });
 }
 }

Thank you.
Here are some suggestion of changes assuming that all the user will receive the same email, means you won't be customizing each email with user's name:
if (isset($_POST['searchbutton'])) {
$data = $query->where('blood_type', '=', "$search")
->Where('state', '=', "$state")->get();
$emails = $query->where('blood_type', '=', "$search")
->Where('state', '=', "$state")->lists('email');
if (isset($POST['emailbutton'])) {
Mail::send('emails.notify', array(), function($message) use ($emails) {
$message->to($emails)->subject('Hello');
});
}
}
The actual error here is that the variable $row is not in the scope of your closure. To correct this, you need to use the use keyword. i.e.
foreach ($data as $row){
Mail::send('emails.notify', array('name' => 'Name'),
function($message) use ($row) {
$message->to($row->email, $row->name)->subject('Hello');
}
);
}
You can read more about PHP closures here.

Laravel- filter result by several parameters

I want filter a table with several parameters. which I get from a form. it is the index function of the controller where i want to do these things. How can pass these form values to result, how to use laravel collection to apply filter to result. Sorry for a messy question and being silly.
Here is my controller code
$keyword = Input::get('keyword');
$mfs_id = Input::get('mfs_id');
$tps_id = Input::get('tps_id');
$date_start = Input::get('date_start');
$date_end = Input::get('date_end');
//values passed to view
$sales = Sale::paginate(10);
$total = Sale::sums('total_online_sale', 'discount', 'delivery_cost','sales_vat');
$mfs = Mf::lists('mfs_name', 'id');
$tps = Tp::lists('tps_name','id');
// load the view and pass the values
return View::make('sales.index')
->with('sales', $sales)
->with('total',$total)
->with('mfs',$mfs)
->with('tps',$tps);
In view file I am using a form to get $keyword, $mfs_id, $tps_id, $date_start, $date_end
view code
{{ Form::open(array('route' => array('sales.index', array('keyword' => $keyword, 'mfs_id' => $mfs_id, 'tps_id' => $tps_id, 'date_start' => $date_start, 'date_end' => $date_end)))) }}
http://laravel.com/docs/4.2/queries
$users = DB::table('users')->where('start', '>', $date_start)
->where('end', '<=', $date_end)
Or with model:
$users = Users::where('start', '>=', $date_start)
->where('end', '<=', $date_end)
Replace users with your model and add all the parameters in chainging them.

Resources