How to pass data collection to mail? I don't know the proper term but set of data I guess. I want to display a list of users in my pdf. Every time I use foreach in blade, it fails to send the mail. Ex. I want to display the users John Doe, Jane Doe, Peter Parker from model Signature to my pdf mail. my reference for sending mail with pdf.
In my controller
public function sendEmail(Request $request)
{
$sig = Signature::all();
$users = StudentApplicant::whereIn("id", $request->ids)->get();
foreach ($users as $key => $user) {
$data = [
'studnum' => $user->users->stud_num,
'fname' => $user->users->first_name,
'mname' => $user->users->middle_name,
'lname' => $user->users->last_name,
'gwa' => $user->gwa,
'award' => $user->award_applied,
'award_name' => $user->award->name,
'sy' => $user->school_year
];
$details = ['email' => $user->users->email];
SendEmailJob::dispatch($details, $data, $data['studnum'], $sig);
}
StudentApplicant::whereIn("id", $request->ids)->update(['certificate_status' => 1]);
return response()->json(['success' => 'Send email successfully. Refresh the page']);
}
cert blade file
<table class="table2">
<tr>
#foreach ($sig->take(3) as $item)
<td width="21%">
<b>{{ $item->name }}</b><br>
{{ $item->position }}
</td>
#endforeach
</tr>
</table>
<table class="table2">
<tr>
#foreach ($sig as $item)
#if ($loop->last)
<td>
<b>{{ $item->name }}</b><br>
{{ $item->position }}
</td>
#endif
#endforeach
</tr>
</table>
Related
Hello i am doing a panel where a client can select one or multiple products and make an order. I have made the table connection for many to many.
This is the migration code for ordering table:
public function up()
{
Schema::create('orders', function (Blueprint $table)
{
$table->id();
$table->string('order_number')->nullable();
$table->unsignedBigInteger('client')->nullable();
$table->unsignedBigInteger('products')->nullable();
$table->string('amount')->nullable();
$table->string('description')->nullable();
$table->timestamps();
$table->foreign('client')->references('id')->on('clients')->onDelete('cascade');
$table->foreign('products')->references('id')->on('products')->onDelete('cascade');
});
}
This is the store method that i have made in the OrderController :
public function store(Request $request)
{
$request->validate([
'order_number' => 'required',
'client' => 'required',
'products' => 'required',
'amount' => 'required',
'description' => 'required',
]);
for($i = 0; $i < count($request->products); $i++)
{
$values[] = [
'order_number' => $request->order_number,
'client' => $request->client,
'products' => $request->products[$i],
'amount' => $request->amount[$i],
'description' => $request->description,
];
$amount = Product::findorFail($request->products[$i]);
$total_value = $request->amount[$i] + $amount->amount;
$amount->update(['amount'=>$total_value]);
}
Order::upsert($values,'order_number');
return redirect('/')->with('msg', 'Order Saved successfully!');
}
If i make dd i will have an array.
This is an image how the table order looks like after one user has made one order with many products selected:
Now i want to have a table where i can show the results from the database and it looks like this:
Is it possible to make it show only one row without duplicating 3 times?
This is the code of this view:
#extends('layouts.app')
#section('content')
<div class="wrapper-index">
<h1>Total Products</h1>
<table class="table table-striped">
<tr>
<th>Order Number</th>
<th>Client</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
#foreach($orders as $order)
<tr>
<td> {{$order->order_number}} </td>
<td> {{$order->client}} </td>
<td> {{$order->description}} </td>
<td>
<a href="{{route('orders.show',$order->id)}}">
<button>Edit</button>
</a>
</td>
<td>
<form action="{{route('orders.destroy',$order->id)}}" method="POST">
#csrf
#method('DELETE')
<button>Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
<p class="msg">{{session('msg')}}</p>
</div>
#endsection
Here is the solution,
It's simple..
You have many-to-many relationship between products and the orders tables.
You can get unique data by using groupBy() method.
group the products with order_id(order_number).
Get the order details by database like this way and return the data to your blade file,
public function index()
{
$orders = Order::with('client','products')->groupBy('order_number')->get();
return view('orders.index', compact('orders'));
}
Hope you will fix this issue...
I retrieved form input in three ways in codeigniter? I am unsure which one is correct. I have given this line
$this->load->view('userview',$data);
Is this correct? What is the right way to input the data from the form? When should I use an array?
I also want to know if the record was added successfully. After submitting the form, which function would I have to use and where would I put it?
view folder file name userview.php
userview.php
<form name="f1" action="" method="post"/>
<table width="500" border="1">
<tr>
<td>UserName</td>
<td>:</td>
<td><input type="text" name="username" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" value=""/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" value=""/></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit" value="Save"/></td>
</tr>
</table>
</form>
First one-created Array and stored into variable:
public function index()
{
$data = array();
if($this->input->post('submit') != NULL ){
$postData = $this->input->post();
$data['response'] = $postData;
}
$this->load->view('userview',$data);
}
Second one: retrieved input variable within array
public function index()
{
$data['response']=array('username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'));
$this->load->view('userview',$data);
}
Third one: created one method within index function
public function index()
{
$this->load->view('userview');
$this->getvalue();
}
public function getvalue()
{
if($this->input->post('submit')!==null)
{
$data['response']=array('username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'));
$this->load->view('viewuser',$data);
}
}
All are OK, Personally I use the following when I have multiple fields:
$UserDetails = $this->input->post(['username', 'email', 'password']);
This would return a key => value pair array that contains the 3 fields I need only.
In your first example, you might be returning extra fields that you don't need.
Your second example is very verbose for my taste but it's OK.
3rd example is also very verbose for my taste but it's OK.
I would use form validation instead of manually checking if the form is posted.
$data = [];
$form_validation = array(
['field' => 'username', 'label' => 'Username', 'rules' => 'trim|required'],
['field' => 'email', 'label' => 'Email', 'rules' => 'trim|required|email'],
['field' => 'password', 'label' => 'Password', 'rules' => 'trim|required'],
);
$this->form_validation->set_rules($form_validation);
if ( $this->form_validation->run() === false ) {
// set some error messages here
}else{
// get data here
$data['UserDetails'] = $this->input->post(['username', 'email', 'password']);
}
// pass data to view ??
$this->load->view('view', $data)
Here you go:
Form Validation: https://www.codeigniter.com/userguide3/libraries/form_validation.html
Input Class: https://www.codeigniter.com/userguide3/libraries/input.html
Form Helper: https://www.codeigniter.com/userguide3/helpers/form_helper.html
Good luck : )
I have a table in which I am adding rows dynamically using JS which works fine, I am able to add those text fields to db as well using for loop, now one of the row has to be an image uploader, by current code I am only able to save one image, the rest images from different rows doesnt save, please help. Thank You.
blade file:
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Tags</th>
<th>Image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="button" class="btn-link cursor-pointer" value="Add Menu Item" href="javascript:void(0);">
<input type="hidden" class="tbl_row" name="count_row[]" id="count_row[]" value="1"/>
</td>
<td>
{{ Form::text('item_name[]', null, ['id' => 'name', 'class' => 'form-control'] ) }}
</td>
<td>
{{ Form::text('price[]', null, ['id' => 'price', 'class' => 'form-control'] )}}
</td>
<td>
{{ Form::select('tags[]', $tags, null, ['class' => 'form-control']) }}
</td>
<td>
<input type="file" name="image[]">
</td>
<td>
{{ Form::textarea('description[]', null, ['id' => 'description', 'rows' => 2, 'class' => 'form-control']) }}
</td>
</tr>
</tbody>
</table>
Controller:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$i]) && is_file($attachment[$i])) {
$fileNameWithExt = $attachment[$i]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$i]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . $extention;
// Upload Image
$path = $attachment[$i]->storeAs('public/content-images/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => $fileNameToStore[$ln], **even the name is not saving properly in db**
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}
Thanks to azeĆ³s's comment managed to solve by changing the code to this, had to change $i to $ln and fileNameToStore[$ln] to fileNameToStore in order to save proper filename to db:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$ln]) && is_file($attachment[$ln])) {
$fileNameWithExt = $attachment[$ln]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$ln]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . '.' . $extention;
// Upload Image
$path = $attachment[$ln]->storeAs('public/menu/'.$category->id.'/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => 'menu/' . $category->id . '/' . $fileNameToStore,
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}
I am trying to create a Collection and pass it to blade. My PHP code look like this
$collection1 = collect(['name' => 'Alex', 'id' => '1']);
$collection2 = collect(['name' => 'John', 'id' => '2']);
$collection3 = collect(['name' => 'Andy', 'id' => '3']);
$people_col = new Collection();
$people_col->push($collection1);
$people_col->push($collection2);
$people_col->push($collection3);
return view('test',[
'people_col' => $people_col
]);
In my blade I loop through people_col to get properties of items:
#foreach ($people_col as $people)
<tr>
<td>{{ $people->name }}</td>
<td>{{ $people->id }}</td>
</tr>
#endforeach
However I got this error:
Property [name] does not exist on this collection instance
Any idea?
Thanks
You are creating a collection of collections where you should have created a collection of objects instead.
With your current implementation you should be able to get the values using array access method {{ $people['name'] }} or using get method on collection {{ $people->get('name') }}
If you created a collection of objects like below and return it instead of what you are doing now
$people_col = collect([
(object) ['name' => 'Alex', 'id' => '1'],
(object) ['name' => 'John', 'id' => '2'],
(object) ['name' => 'Andy', 'id' => '3']
]);
return view('test', [
'people_col' => $people_col
]);
Then in your view you should be able to access people object like you have done in your code.
#foreach ($people_col as $people)
<tr>
<td>{{ $people->name }}</td>
<td>{{ $people->id }}</td>
</tr>
#endforeach
Try to access to the property name this way: $people['name']
#foreach ($people_col as $people)
<tr>
<td>{{ $people['name'] }}</td>
<td>{{ $people['id'] }}</td>
</tr>
#endforeach
I'm trying to build some custom grid forms which looks like
form.blade.php
<table>
<tr>
<td>
{{ Form::text('name[]', null, array('placeholder' => 'name') }}
</td>
</tr>
<tr>
<td>
{{ Form::text('aerobic[]', null, array('placeholder' => 'aerobic') }}
</td>
</tr>
<tr>
<td>
{{ Form::text('core_test[]', null, array('placeholder' => 'core test') }}
</td>
</tr>
</table>
Validation rules
$validator = Validator::make(Input::all(), array(
'name[]' => 'required|alpha',
'aerobic[]' => 'required|alpha',
'core_test[]' => 'required|alpha'
));
How do I validate the inputs or what other good ways would be to build this?
I get the following errors when I submit the form
htmlentities() expects parameter 1 to be string, array given
(View: /Applications/XAMPP/xamppfiles/htdocs/hp/v1.1/app/views
/files/templates/apa/form.blade.php)
I've also tried this
$validator = Validator::make(Input::all(), array(
'name' => 'required|alpha',
'aerobic' => 'required|alpha',
'core_test' => 'required|alpha'
));
And I get this error from alpha validation rule
preg_match() expects parameter 2 to be string, array given
I've spend a lot of time trying to figure out how to solve this problem...Laravel does not expect a text input to be an array. so you either use plain html for your array inputs, or use custom macros to create your own form class(I assume you're using LaravelCollective).
Take off the [] in the name of each of your inputs. PHP is interpreting those inputs as arrays instead of strings.
Example:
{{ Form::text('name', null, array('placeholder' => 'Name')) }}