Laravel - Send mail and pass multi array data - php

The end result I'm aiming for (if it's possible):
To iterate thru all $users and their $properties, grab the property address and then iterate thru $complaints (complaint belong to property) and create <table> for each property with it's complaints, it is possible that a property will not have complaints so need to check for that too.
I did try this code and managed to put all the $data in an array but first I can not iterate it in the email view and second if i'll have 1000 properties and each property will have 30 complaints... you get the idea... no good...
$users = User::all()->toArray();
foreach ($users as $user)
{
$report = [];
$properties = Property::where('user_id', $user['id'])->get()->toArray();
array_set($report, 'user', $user);
foreach ($properties as $property)
{
array_set($report, 'properties', $property);
$complaints = Complaint::where('property_id', $property['id'])->where('status', 'ACT')->get()->toArray();
foreach ($complaints as $complaint)
{
array_set($report, 'complaints', $complaint);
}
}
Mail::queue('emails.reports.weekly', $report, function($message) {
$message->to($report['user']['email'])->subject('Weekly Report');
});
}

Related

Larvel Update Data from Array with foreachloop

Hello There I need Help To Update on Vehicle Wies on Specific Dates.I'm Attaching Form image and Here is my Controller code.
public function add_vehicle_expense(Request $request)
{
$veh = array('veh_reg_num' => $request->veh_reg_num);
foreach ($veh as $data) {
print_r($request->date[$data]);
dd();
$veh = Sale_report::where('date',$request->date[$data])->where('veh_reg_num', $request->veh_reg_num[$data])->update(['diesel_qty'=> $request->qty[$data], 'diesel_rate'=> $request->rate[$data], 'diesel_amount'=> $request->total_amount[$data], 'other_expense'=> $request->other_exp[$data]]);
}
if ($veh) {
return redirect()->back()->with('Data Store Successfully');
}
//$veh = Sale_report::where('date',$request->date)->where('veh_reg_num', $request->veh_reg_num)->update(['diesel_qty'=> $request->qty, 'diesel_rate'=> $request->rate, 'diesel_amount'=> $request->total_amount, 'other_expense'=> $request->other_exp]);
/* foreach ($request->date as $reg_num => $key ) {
$s = Sale_report::where('date',$request->date[$reg_num])->where('veh_reg_num',$request->veh_reg_num[$reg_num])->first();
$s->diesel_qty = $request->qty[$reg_num];
$s->diesel_rate = $request->rate[$reg_num];
$s->diesel_amount = $request->total_amount[$reg_num];
$s->other_expense = $request->other_exp[$reg_num];
$s->update();
} */
}
I have try to Update Data by matching Each Vehicle Number with Date Some times it Show ErrorException Attempt to read property on int,ErrorException Undefined array key "data"
I have Found The Solution of this Problem.
I've use implode(',' , $request->veh_reg_num) Then I use $datas = explode() function then I use foreach() With exploded Vairable as foreach($datas as $data => $value){
match the each row that with $data array then I Update the values to data base }

Store data in a variable from a nested foreach loop from eloquent query statement

I want to pass data from laravel controller to view. the problem i am having is that the data being passed is coming from a database table and its being stored in a variable using nested foreach loop but the variable is being overritten with each loop.
Here is the Code.
public function generateContract(Request $request){
foreach($request->ids as $id){
$contract = explode(",", $id);
$this->techId[] = $contract[0];
$this->meetingId[] = $contract[1];
$this->contractId[] = $contract[2];
}
$names = contract::whereIn('technician_id', $this->techId)->whereIn('meeting_id', $this->meetingId )->get();
foreach($names as $name){
$names_arr[] = $name->firstName;
}
$unique_names = array_unique($names_arr);
foreach($unique_names as $unique_name){
$functions[] = Contract::where('firstName', $unique_name)->whereIn('meeting_id', $this->meetingId)->whereIn('id', $this->contractId)->get();
}
return view('contract.generated_contract', compact('unique_names', 'functions'));
}
My question is how can i pass the $functions variable to view without it being overwritten. I have tried using array but the array is also being overritten with each loop.

Laravel Foreach Controller vs View

I realized something I can't figure out by myself.
I get a different result when I loop through first in a Controller and pass then the result to my view. Versus loop straight in my view.
For instance:
I have this in my Controller:
public function index()
{
$subscribers = Subscriber::where('user_id', Auth::user()->id)->orderBy('created_at','asc')->get();
foreach ($subscribers as $key => $subscriber) {
$var = $subscriber->name;
}
return view('backend.newsletter.contacts.index')->withSubscribers($subscribers)
->withVar($var);
}
by using {{$var}} in my view I get only "John" as a result.
But when I use the foreach loop in my view instead of in the Controller:
#foreach($subscribers as $key => $subscriber)
{{$subscriber->name}}
#endforeach
I get two results, "John" and "Dan". This makes totaly sense as I have two entries in my DB.
So how does it come that I get two different results here ?
When you're doing this:
foreach ($subscribers as $key => $subscriber) {
$var = $subscriber->name;
}
With each iteration you overwrite $var so you always get the last value. For example, if you have ['Dan', 'John, 'Alice', 'Alan'], you'll get Alan.
Good practice is too pass data to a view and iterate it with #foreach.
Because only the last loop iteration is stored in $var.
You need to
$var = [];
foreach ($subscribers as $key => $subscriber) {
$var[] = $subscriber->name;
}
You are overriding $var in the loop.
Try this:
$var = [];
foreach ($subscribers as $key => $subscriber) {
$var[] = $subscriber->name; // Appends name to array
}

My Array inside a foreach loop is not accepting a variable in PHP & Laravel

I have a Category array that I'm looping through and counting the subcategories that have their Category ID as the ID of the current item in the loop. It is retrieving the county just fine, uynfportunately when adding to the array it adds the value of zero. When echoed out, the values are as they should be, its only when inserting into the array the value becomes a zero.
This is the code I'm working with in PHP. I am using the Laravel framework.
public function index()
{
$categories = $this->categories->get()->toArray();
$categories_array = array();
$stats = array();
// dd($categories);
foreach ($categories as $key => $value)
{
$subcats_number = sub_categories::whereCategory_id($value['id'])->count();
$stats = array_add($stats, 'subcategories', $subcats_number);
echo $subcats_number.'<br/>';
$listings = classifieds::whereCategory_id($value['id'])->count();
$stats = array_add($stats, 'listings', $listings);
$categories_array = array_add($categories_array, $value['name'], $stats);
}
dd($categories_array);
// return view('admin.categories', compact('categories_array'));
}
The result of my dd:
Dump Result
I am not sure that array_add() helper is appropriate for your function here. As the doc says "The array_add function adds a given key / value pair to the array if the given key doesn't already exist in the array", and I am not sure that this is the proper behavior you need.
I would rather use a simple array_push() here:
public function index()
{
$categories = $this->categories->get()->toArray();
$categories_array = array();
foreach ($categories as $key => $value)
{
$subcats_number = sub_categories::whereCategory_id($value['id'])->count();
$listings = classifieds::whereCategory_id($value['id'])->count();
$stats = array('subcategories' => $subcats_number,'listings' => $listings);
array_push($categories_array,$value['name'] => $stats);
}
dd($categories_array);
}
Otherwise, you should also declare the $stats array into your loop if you choose to keep the array_add() function...

How to map Zend_Db_Table_Row object to Zend_Form_Element_Select options (optial way)

How would You populate Zend_Form_Element_Select with options direct from Zend_Db_Table_Row?
For instance:
$select = new Zend_Form_Element_Select('user_id', array(
'required' => true
));
// fetching users for select
$userTable = new User_Model_DbTable_User();
$users = $userTable->fetchAll();
$select->addMultiOptions($users->toArray());
But this will not work to good. Let say I want to have object id as a option value and some object property as an select label.
I know I can run foreach thourgh the rowset and construct an array of options but maybe there is some kind of map function?
Any map function you create would be iterating the rowset so you might as well simply do that, eg
foreach ($users as $user) {
$select->addMultiOption($user->id, $user->someObjectProperty);
}
you also may consider tailoring a model method to return exactly the array you want use.
Perhaps something similar to:
public function fetchSelectList() {
$resultSet = $this->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry['id'] = $row->id;
$entry['name'] = $row->name;
$entries[] = $entry;
}
return $entries;
}

Categories