laravel save pdf no such file or directory - laravel

So I want to save a pdf file to a directory on my local server but it keeps saying that the directory does not exist.
So first of all where would you store PDF files that are not accessible to by externals (so not in the public folder).
So this is my code. The download works perfectly.
public function generatePDF()
{
$this->mailorder = Session::get('order');
$this->cart = Session::get('cart');
$data = [
'id' => $this->mailorder->id,
'client' => $this->mailorder->Contact,
'country' => $this->mailorder->country,
'city' => $this->mailorder->city,
'street' => $this->mailorder->street,
'postal' => $this->mailorder->postal,
'phone' => $this->mailorder->phone,
'email' => $this->mailorder->email,
'dateIn' => $this->mailorder->dateIn,
'dateOut' => $this->mailorder->dateOut,
'subtotal' => $this->mailorder->subtotal,
'tax' => $this->mailorder->tax,
'total' => $this->mailorder->total,
'cart' => $this->mailorder->cart,
'delivery' => $this->mailorder->delivery,
];
$path = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
$pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save('storage/app/public/'.$path.'.pdf');
;
return $pdf->download(''.$path.'.pdf');
}

First of all, you should check if the directory exists with File facade. If it does not exist, you must make the directory.
if(!File::exists($directory_path)) {
File::makeDirectory($directory_path);
}
If the error still occurs, you must force it to make the directory:
if(!File::exists($directory_path)) {
File::makeDirectory($directory_path, $mode = 0755, true, true);
}
After that, you can save the file in that directory.
Second, if you don't want to save the file in the public directory. you must save it in storage.By simply call storage_path($file_path). this way laravel saves the file under storage/app/public directory.
after that, you can get the URL of the file according to this answer.

I figured it out thank you for your answer.
This is my code:
public function generatePDF()
{
$this->mailorder = Session::get('order');
$this->cart = Session::get('cart');
$data = [
'id' => $this->mailorder->id,
'client' => $this->mailorder->Contact,
'country' => $this->mailorder->country,
'city' => $this->mailorder->city,
'street' => $this->mailorder->street,
'postal' => $this->mailorder->postal,
'phone' => $this->mailorder->phone,
'email' => $this->mailorder->email,
'dateIn' => $this->mailorder->dateIn,
'dateOut' => $this->mailorder->dateOut,
'subtotal' => $this->mailorder->subtotal,
'tax' => $this->mailorder->tax,
'total' => $this->mailorder->total,
'cart' => $this->mailorder->cart,
'delivery' => $this->mailorder->delivery,
];
$filename = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
$path = storage_path('pdf/orders');
if(!File::exists($path)) {
File::makeDirectory($path, $mode = 0755, true, true);
}
else {}
$pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save(''.$path.'/'.$filename.'.pdf');
;
return $pdf->download(''.$filename.'.pdf');
}

Related

Updating a document in laravel firestore

Hi I've been trying out things in updating aa document in firestore from laravel 9. But it doesn't seem to work at all.
What I have tried so far:
$product = app('firebase.firestore')
->database()
->collection('Products')
->document('15da5077cf8940f797db')
->update([
'path' => 'status',
'value' => 'archive'
]);
and
$status = "archive";
$currentData = json_decode(json_encode([
'status' => $status,
]));
$newData = [
'status' => $status,
];
$postData = app('firebase.firestore')
->database()
->collection('Products')
->document('15da5077cf8940f797db');
$postData->update([[
'path' => 'Products',
'value' => FieldValue::arrayRemove([$currentData])
]]);
$postData->set([
'Products' => FieldValue::arrayUnion($newData)
], ['merge' => true]);
and, javascript
const app = initizalizeApp(firebaseConfig);
const db = getFirestore(app);
btnArchive.addEventListener('click', (e) =>{
archiveDoc(doc(db, "Products", "15da5077cf8940f797db"), {
status: archive,
})
alert('Product archived!')
});
I want to update the status of a product from active to archive. But this doesn't work at all. Also is there another way to edit a document without specifying it?
this is how my collection looks like, each document has its own product details:

Site refresh time is long in laravel?

Hello and do not be tired!!
My admin panel pages are hard to load...It takes 15 seconds for the pages to load!!
The problem is with my routes!!!
Because when I define the path as follows, the pages load quickly :
Route::get('users/index', function () {
return view('admin.index');
});
But the route I have defined is as follows :
Route::group(['middleware' => ['auth' , 'InfoFolder' , 'verified' , 'Roles'] , 'prefix' => 'users/'] , function(){
Route::get('{url}', [UrlController::class , 'urlpanel'])->name('users_url');
});
And the control I have defined :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UrlController extends Controller
{
//
public function urlpanel($url ){
$admin = "admin";
$pages = "admin.pages";
$charts = "admin.pages.charts";
$examples = "admin.pages.examples";
$forms = "admin.pages.forms";
$mailbox = "admin.pages.mailbox";
$tables = "admin.pages.tables";
$UI = "admin.pages.UI";
$link_panel = [
'index' => "$admin.index",
'list_users' => "$admin.list_users",
'index3' => "$admin.index3",
'calendar' => "$pages.calendar",
'widgets' => "$pages.widgets",
'chartjs' => "$charts.chartjs",
'flot' => "$charts.flot",
'inline' => "$charts.inline",
'404' => "$examples.404",
'500' => "$examples.500",
'blank' => "$examples.blank",
'invoice-print' => "$examples.invoice-print",
'invoice' => "$examples.invoice",
'lockscreen' => "$examples.lockscreen",
'login' => "$examples.login",
'profile' => "$examples.profile",
'register' => "$examples.register",
'product_add' => "$forms.product_add",
'editors' => "$forms.editors",
'general' => "$forms.general",
'compose' => "$mailbox.compose",
'mailbox' => "$mailbox.mailbox",
'read-mail' => "$mailbox.read-mail",
'data' => "$tables.data",
'simple' => "$tables.simple",
'buttons' => "$UI.buttons",
'general' => "$UI.general",
'icons' => "$UI.icons",
'sliders' => "$UI.sliders",
];
if(!in_array($link_panel[$url] , $link_panel)){
return abort(404);
}
return view($link_panel[$url]);
}
}
How can I increase the loading speed of my website?
The project has been uploaded to Localhost
:)
Try to check the middlewares you are using. Maybe in one of the middlewares there is some time consuming action.

Commit file deletions with Laravel package GrahamCampbell/Laravel-GitHub

Using GrahamCampbell/Laravel-GitHub my Laravel app can commit files like this:
public function commitFiles(string $github_nickname, string $repo_name, string $branch, string $commit_message, array $files) {
$master_branch = $this->github_client->repo()->branches($github_nickname, $repo_name, $branch);
$commit_parent = $master_branch["commit"]["sha"];
$base_tree = $master_branch["commit"]["commit"]["tree"]["sha"];
$commit_tree = array();
foreach ($files as $file) {
$file_blob = [
"path" => $file["path"],
"mode" => "100644",
"type" => "file",
"content" => $file["content"],
];
array_push($commit_tree, $file_blob);
}
$new_commit_tree_response = $this->github_client->git()->trees()->create($github_nickname, $repo_name, [
"base_tree" => $base_tree,
"tree" => $commit_tree
]);
// TODO verify commit with GPG
$new_commit_response = $this->github_client->git()->commits()->create($github_nickname, $repo_name, [
"message" => $commit_message,
"parents" => [$commit_parent],
"tree" => $new_commit_tree_response["sha"],
]);
$this->github_client->git()->references()->update($github_nickname, $repo_name, "heads/".$branch, [
"sha" => $new_commit_response["sha"],
"force" => false,
]);
return true;
}
But how can I delete files? I have not found any applicable documentation and have a really hard time figuring this out.
Here is a way to remove one file:
public function deleteFile($github_nickname, $repo_name, $path, $commitMessage)
{
$committer = array('name' => 'SomeName', 'email' => 'some#email.com');
$oldFile = $this->github_client->api('repo')->contents()->show($github_nickname, $repo_name, $path, 'master');
$this->github_client->api('repo')->contents()->rm($github_nickname, $repo_name, $path, $commitMessage, $oldFile['sha'], 'master', $committer);
}

[cakePHP3]how to debug a failed save

This is my code , I have problems with making a simple save. The message is :( when i trying to save
$user = $this->Users->newEntity();
if($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if($this->Users->save($user))
{
$this->Flash->success(':)');
return $this->redirect(['controller' => 'Users', 'action' => 'index']);
}
else
{
$this->Flash->error(':(');
}
debug($this->request->data);
}
$this->set(compact('user'));
this is my table , I made a migration. All fields can be null
$table = $this->table('users');
$table->addColumn('first_name','string',array('limit'=>100))
->addColumn('last_name','string',array('limit'=>100))
->addColumn('email','string',array('limit'=>100))
->addColumn('password','string')
->addColumn('role','enum',array('values'=>'admin,user'))
->addColumn('active','boolean')
->addColumn('created','datetime')
->addColumn('modified','datetime')
->create();
this is my request data
[
'first_name' => 'wewe',
'last_name' => 'wewe',
'email' => 'wewe#wee.com',
'password' => 'wewewe',
'role' => 'admin',
'active' => '1'
]
I hope that you help me , I am very frustrated
EDIT: if i use print_r ($user->errors()); i get this...
Array ( [firts_name] => Array ( [_required] => This field is required ) )
Your error is in the name of your data, the request data is
'first_name' => 'wewe'"
your column name is "firts_name", its a typing error.

How to echo config item from config file for view in Codeigniter?

How to call config item from config file for view in codeigniter.
here is my config file
$config['user'] = array(
'email_validation' => 'email validation',///^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
'no_permission' => '/',
'admin_group' => 'admin',
'default_group' => 'default',
'public_group' => 'public',
'users' => 'aauth_users',
'groups' => 'aauth_groups',
'user_to_group' => 'aauth_user_to_group',
'perms' => 'aauth_perms',
'perm_to_group' => 'aauth_perm_to_group',
'perm_to_user' => 'aauth_perm_to_user',
'pms' => 'aauth_pms',
'system_variables' => 'aauth_system_variables',
'user_variables' => 'aauth_user_variables',
'remember' => ' +3 days',
'max' => 13,
'valid_chars' => array(' ', '\''),
'ddos_protection' => true,
'recaptcha_active' => false,
'recaptcha_login_attempts' => 4,
'recaptcha_siteKey' => '',
'recaptcha_secret' => '',
'max_login_attempt' => 10,
'verification' => false,
'email' => 'admin#admin.com',
'name' => 'Emre Akay'
);
Here is my load config
$this->config->load('user');
And I will view its item for view as below
$site_name = $this->config->item('email_validation');
But is don't show any thing
This is because your config array is two dimensional array. So, you can't access directly email_validation without getting user first. Moreover,
$this->config->load('user'); just means loading user.php from application/config/ directory. Doesn't mean loading user index from $config array. You can do it like that.
$userConfig = $this->config->item('user');
echo $userConfig["email_validation"];
Edit
Please make sure you config file is under application/config/ and loaded.
$this->config->load('user');
You can check which config is loaded by doing like this.
echo "<pre>";
print_r($this->config);
echo "</pre>";
Hope it will be useful for you.
if your php version>=5.4 you can use this
$site_name = $this->config->item('user')['email_validation']

Resources