how to add table in phpword with laravel - laravel

i am trying to draw a table in phpword with laravel
code below is example of writing something in the doc file.
$wordTest = new \PhpOffice\PhpWord\PhpWord();
$newSection = $wordTest->addSection();
$desc1 = "The Portfolio details is a very useful feature of the web page. You can establish your archived details and the works to the entire web community. It was outlined to bring in extra clients, get you selected based on this details.";
$newSection->addText($desc1, array('name' => 'Tahoma', 'size' => 15, 'color' => 'red'));
$objectWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordTest, 'Word2007');
$objectWriter->save(storage_path('TestWordFile.docx'));
please give me an example of how i can draw table there

to draw a table i normally use this code
$table = array('borderColor'=>'black', 'borderSize'=> 1, 'cellMargin'=>50, 'valign'=>'center');
$phpWord->addTableStyle('table', $table);
$table = $section->addTable('table');
$table->addRow();
$table->addCell(1750)->addText(htmlspecialchars("#"),$bold);
$table->addCell(1750)->addText(htmlspecialchars("Attendance Date "),$bold);

Related

In Laravel, how do you bind parameters in DB raw query when you set model attribute?

For example, I have my codes.
$news = new News();
$news->title = 'hello world';
$new->user = $user_id,
$news->urlcc = DB::raw('crc32("'.$args['newsShortUrlInput'].'")');
$news->save();
$news->refresh();
Here with attribute $news->urlcc comes from user input after using mysql function crc32();
For the SQL injection issue, above codes not safe.
So, my question is how to bind the parameters in DB::raw() with Laravel model something like below.
$news->urlcc = DB::raw('crc32(:newsShortUrlInput)', ['newsShortUrlInput' => $args['newsShortUrlInput]);
Thanks,
I found one solution, not sure it is right or perfect solution.
In News model class to rewrite setAttribute, see below.
public function setUrlcrcAttribute($shortUrl)
{
$this->attributes['urlcrc'] = $this->getConnection()->select('select crc32(?) as urlcrc', [$shortUrl])[0]->urlcrc;
}
In your service class to create a new model like below.
$news = new News();
$news->title = 'hello world';
$new->user = $user_id,
$news->urlcrc = $args['newsShortUrlInput']; // Laravel model will try to build the real attribute urlcrc
$news->save();
$news->refresh();
It works to me, but not sure this is perfect solution or not.

Passing Stripe Payment Methods to Laravel View

I am trying to load previously used cards using the Stripe API using Laravel using this link from Stripe https://stripe.com/docs/api/payment_methods/list?lang=php
Here is the snippet of code from my controller:
$stripe = new \Stripe\StripeClient(
'sk_test_51GueZuLq4MEy
);
$customer_id = "cus_HhnBT9fpjxW3hn";
$paymentMethods = $stripe->paymentMethods->all([
'customer' => $customer_id,
'type' => 'card',
]);
$pm = ($paymentMethods->data);
return view('payment.details', $pm);
However when I am trying to pass the card data into my view I am unable to do so. The variable I am passing in views is:
{{ $pm }}
The error message I get is that my variable is not recognised. The data I am trying to access is $paymentmethods->-data->card->last4
Any help is always appreciated
$stripe->paymentMethods->all returns an object with a data attribute, where the data is an array of PaymentMethods, in this case, you'll likely want to access the first element of data.
Try updating to:
$pm = $paymentMethods->data[0];

should i put this code in the model or controller in laravel

ok so I am upping my Laravel and MVC skills..currently all this in the controller (i know its bad thats why I am here I am trying to do it better :D)
Receive Data from UI as post (Done)
I have to store a file (Done)
store that file ID and path in a Files Table (Done)
Create new record and use the above record ID using query builder(Done)
but should I keep this in the controller or does this belong in the model specificlaly getting the files and storing the record etc.
do i build it the long way in the controller and use save()?
or return from a new model for files and pass that through on return (this i feel is best way ).
FileModel (To be built)
saves files returns file ID from file table
Business Model saves buisness data
controllerfile:
use /app/fileModel
$fileStored = call file model and passes request->file
for storing
$businessFile = $fileStored;
Save()
I think I have answered my own question but I am just making sure.
I am known for coding in anti patterns so I am asking for a bit of guidance not code thank you
Also if anyone knows of a place to just discuss laravel stuff so not to put here I would love a a good conversation, some my questions may seem stupid, I just need a soundboard at times.
ok so this works and is my solution.
public function create(Request $request)
{
//get files from request
$Match1 = $request->file('Match1')->store('media/scores');
$Match2 = $request->file('Match2')->store('media/scores');
$Match3 = $request->file('Match3')->store('media/scores');
//build collection array to loop through saving them
$collection = collect([$Match1,$Match2,$Match3]);
// store return paths in to new array
$mediapaths = array();
//loop through collections saving details in to files table pushing to the paths array
foreach($collection as $MatchScore) {
$ScoresSaved = Files::create([
'user_id' => $request->userID,
'file_name' => $request->title,
'type' => 'png',
'category_id'=>3,
'path' => $MatchScore,
'is_public'=>0
]);
array_push($mediapaths, $ScoresSaved);
}
//Save the score with new media path to the model
$scores = new Scores;
$scores->tournament=22;
$scores->round='Finals';
$scores->homeArcade=2578;
$scores->homePlayer=2;
$scores->opponenet=2;
$scores->Match1=$mediapaths[0]['id'];
$scores->Match2=$mediapaths[1]['id'];
$scores->Match3=$mediapaths[2]['id'];
$scores->Match1Score=22;
$scores->Match2Score=44;
$scores->Match3Score=88;
$scores->comments='testing full flow';
$scores->winner=2;
$scores->referee=3;
$scores->confirmed=1;
$scores->dispute=0;
$scores->submitedBy=2;
$scores->save();
return response()->json([
'result' => 'Success'
]);
}

How to Fetch Latest youtube Video's in Laravel

I am using alaouy/Youtube package on my laravel 5.4 its fetching my all video's from my channel with that code
$videoList = Youtube::listChannelVideos('UC9PWIZ20pnEXgT1fT69bX8A', 50);
but its showing me old first i want to to fetch latest first what do i do
thank you
In git repository of that package we can se this:
public function listChannelVideos($channelId, $maxResults = 10, $order = null, $part = ['id', 'snippet'], $pageInfo = false)
{
$params = array(
'type' => 'video',
'channelId' => $channelId,
'part' => implode(', ', $part),
'maxResults' => $maxResults,
);
if (!empty($order)) {
$params['order'] = $order;
}
return $this->searchAdvanced($params, $pageInfo);
}
That function seems to accept a third parameter "order"
So maybe this work:
$videoList = Youtube::listChannelVideos('UC9PWIZ20pnEXgT1fT69bX8A', 50, "date");
I ran the snippet you pasted and the videos do not appear to be in an order at all - the top few are 2016, then 2017, then 2016 again.
You can fetch the latest first by sorting the list:
$videoList = Youtube::listChannelVideos('UC9PWIZ20pnEXgT1fT69bX8A', 50);
usort($videoList, function ($a, $b) {
return strtotime($a->snippet->publishedAt) < strtotime($b->snippet->publishedAt);
});
Once the list is sorted, $videoList[0] will contain your most recent video
I am just trying this package too and found that by using the search method you can loop through all of your videos and store the results in your database whilst capturing the publish date and other properties. From there you can use these records to easily sort by date or title etc and paginate through your videos. This also saves on multiple requests to the you tube api.
Anyone else taken that approach?

zf2 DB insert using custom query is not working

We are developing a website using ZF2. We alreday started the project. We installed ZF2 skelton application and integrated the album module. Everything seems working. Now we are trying to integrate multiple zend forms in a module. Forms eveyrything we made [ by modifying the albulm module elements) I am able to insert/update/delete etc data in to the default table that set on the module.php file [this is for form 1].
I have another form which requires to insert/delete/update to another table, I created new form class, Table class (on model) etc. When I tried to insert the data in to Db using custom query format, it throws error:
File:
/home/catholic/public_html/propertydosth/vendor/zendframework/zendframework/library/Zend/Db/Sql/Insert.php:298
Message:
The key adapter was not found in this objects column list.
It seems adaptor value is not getting something..
My Code for insert query:
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$insert = $sql->insert('dad_cms');
$newData = array(
'category_id' => $admin->category_id,
'cms_title' => $admin->cms_title,
'cmd_desc' => $admin->cmd_desc,
'seo_keywords' => $admin->seo_keywords,
'seo_titles' => $admin->seo_titles
);
$insert->values($newData);
$selectString = $sql->getSqlStringForSqlObject($insert);
$results = $insert->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
I am getting correct query value when I echoed $selectString .
Any one please suggest what is wrong in my code to get it done.
Finally Sam helped me on this issue:
So correct Answer become [this will be helpful somebody having similar issue. Even I spent lot of time to debug the same]:
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$insert = $sql->insert('dad_cms');
$newData = array(
'category_id' => $admin->category_id,
'cms_title' => $admin->cms_title,
'cmd_desc' => $admin->cmd_desc,
'seo_keywords' => $admin->seo_keywords,
'seo_titles' => $admin->seo_titles
);
$insert->values($newData);
$selectString = $sql->getSqlStringForSqlObject($insert);
$results = $adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);

Resources