Access my files from the public directory - laravel

With the upload_multiple field / column, I need to open file with the existing link.
However when I open the link I get a 404.
This is how I do things :
// setupCreateOperation
CRUD::addField([
'name' => 'documents',
'label' => 'Documents',
'type' => 'upload_multiple',
'upload' => true,
]);
// setupShowOperation
CRUD::addColumn([
'name' => 'documents',
'label' => 'Documents',
'type' => 'upload_multiple',
]);
// Model
public function setDocumentsAttribute($value)
{
$attribute_name = "documents";
$disk = "public";
$destination_path = "documents";
$this->uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path);
}
So the documents are in storage > app > public > documents.
What appears in the form (show or edit) : http://localhost/documents/65004a2cdbd104406b025d14c6519ce9.pdf (404)
And I already exectuted php artisan storage:link
If you have any idea, that would be nice...
Thank you!

If you can show your route:
http://localhost/documents/65004a2cdbd104406b025d14c6519ce9.pdf (404)
when you work in localhost with Laravel, you need proporcionate complete url.
Your url to access this resource sould be:
assets('storage/documents/'.65004a2cdbd104406b025d14c6519ce9.pdf)
with this example, try generate link, should can show your documents.
Your problem it´s you need access to storage path.
Sorry for my bad english. I hope help you

Related

How to start using Slug URLs without breaking old in Yii2

i want to rewrite my URLs from
/view?id=100
to
/view/100-article-title
But the site already has several thousand search pages. As far as I know, such a change can be bad for SEO. Is there a way to keep the old URLs working when switching to the new routing.
*I am creating links as follows:
Url::to(['view', 'id' => $item->id])
Is it possible to create links further through ID?
you can create getLink() function on your model and use it on. Then, when function runs you can check id if id <= 100 then return Url::to(['view', 'id' => $item->id]) else return Url::to(['view', 'id' => $item->id, 'slug' => $this->slug])
And add route with slug on your main.php
Look at my example.
First config urlManager in config.php in 'app/config' folder. In my case look like:
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'home/index',
'<slugm>-<id:\d+>-' => 'home/view',
],
],
.
...
],
and create link as fallow:
Url::to(['/home/view', 'id' => $model->home_id, 'slugm' =>$model->title_sr_latn])
and finaly url look like:
https://primer.izrada-sajta.rs/usluge-izrade-sajtova-1-

Backpack Laravel Admin Incorrectly Redirects after Editing Model

I have a CrudController created for a model using the Backpack Laravel Admin Library.
When I update the model it redirects me incorrectly to a 404 page with the message No query results for model [App\Models\Group].
It is redirecting me to the incorrect URL from what I can tell.
admin/group/261/ instead of admin/group/261/edit
The model also does not update.
I have the "Save and Edit" option set on the green save button. If I try to change this I get the same error, but it doesn't update.
I'm able to save every other model correctly.
The update method in the CrudController is just the following. I've removed all the extra code.
public function update(){
$response = $this->traitUpdate();
return $response;
}
Figured this out. It was because I was referencing the Primary Key -> 'id' in the fields within the Group Crud Controller.
$this->crud->addField([
'name' => 'id',
'type' => 'text',
'attributes' => ['disabled' => 'disabled'],
]);
u can use id, u need delete attribute 'disabled' like this:
[
'name' => 'id',
'label' => 'ID',
'attributes' => [
'readonly' => 'readonly',
],
],

Wrong path when downloading in Backpack CRUD view

i added an upload field in my CRUD Controller.
Upload works fine and file gets loaded in my /storage/private directory.
Here is filesystems.php file:
'private' => [
'driver' => 'local',
'root' => storage_path('private')
],
Here are my custom functions in the File.php Model:
public static function boot()
{
parent::boot();
static::deleting(function($file) {
\Storage::disk('private')->delete($file->file);
});
}
public function setFileAttribute($value)
{
$attribute_name = "file";
$disk = "private";
$destination_path = "";
// Cifratura del file
file_put_contents($value->getRealPath(), file_get_contents($value->getRealPath()));
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
And here is my FileCRUDController.php code:
$this->crud->addField(
[ // Upload
'name' => 'file',
'label' => 'File to upload',
'type' => 'upload',
'upload' => true,
'disk' => 'private'
]);
When i try to download the file, however, it tries to fetch it from http://localhost:8000/storage/myfile.png instead of http://localhost:8000/storage/private/myfile.png
What i'm doing wrong? Thank you very much.
I would also like to know if there is a way to hook a custom function instead downloading the file directly from the CRUD view. My files are encrypted and i need a controller that cares about decrypting before sending the files to the user.
Method url() is still not usable for the files are placed in subdirectories.
You may also use the storage_path function to generate a fully qualified path to a given file relative to the storage directory:
$app_path = storage_path('app');
$file_path = storage_path('app/file.txt');
In reference to Issue #13610
The following works for version 5.3:
'my-disk' => [
'driver' => 'local',
'root' => storage_path(),
'url' => '/storage'
],
\Storage::disk('my-disk')->url('private/myfile.png')
this should return "/storage/private/myfile.png"

backpack for laravel segment missing during upload

I created a CRUD (using backpack crud for laravel )for uploading certificates, when i upload the pdf I use
$this->crud->addField([
'name' => 'respaldo',
'label' => 'Respaldo',
'type' => 'upload',
'upload' => true,
'wrapperAttributes' => [
'class' => 'form-group col-md-6'
],
]);
when i want to see the link of the uploaded file, storage is missing from the route so i added it to a mutator ,see below
public function setRespaldoAttribute($value)
{
$attribute_name = "respaldo";
$disk = "public";
$destination_path = "uploads/respaldos";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
public function getRespaldoLink() {
return '<a href="'.asset('storage/'.$this->respaldo).'" target="_blank">
Descargar</a>';
}
Now when clicking on the link it's displayed on the listview, the problem is when i click on edit , "storage" segment is missing there so i get 404 instead. see image
I got the same issue and tried to amend the link before displaying it on browser for user to preview in Update or Show CRUD.
The issue is whatever the path is stored inside your database, it will pop up on browser without the head folder. As we know, we can amend the head folder with Laravel LIKE asset('storage/'.$file_path)
So, you need to update the path before storing into database.
Search and Update file HasUploadFields.php
Update this line with add-on 'storage/' Like this:
$this->attributes[$attribute_name] = 'storage/' . $file_path;
Do same for uploadMultipleFilesToDisk method
Now for all new file uploaded, it will add-on 'storage/' before stored file path

Cake PHP Upload plugin - How to store the thumbnail path in the database

I have installed Upload plugin for cakephp.
I've been trying to figure out how to store the thumbnail path or thumbnail name inside a field called image_thumb in my table called Portfolio. I want to store this the same way I can store the original image name. The dir option for the image is there but there is no such thing for thumbnail dir.
My model code where I used upload plugin is this:
public $actsAs = array(
'Upload.Upload' => array(
'image' => array(
'fields' => array(
'dir' => 'image'
),
'thumbnailSizes' => array(
'small' => '640x480',
),
'thumbnailName' => '{filename}_{size}_{geometry}',
'thumbnailPath' => '{ROOT}webroot{DS}img{DS}{model}{DS}Thumbnails{DS}'
)
)
);
Controller, the add function, where the plugin happens I assume (I haven't changed anything here though,
it is the baked code)
public function admin_add() {
$this->layout='admin';
if ($this->request->is('post')) {
$this->Portfolio->create();
if ($this->Portfolio->save($this->request->data)) {
$this->Session->setFlash(__('The portfolio has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The portfolio could not be saved. Please, try again.'));
}
}
}
When the image path is uploaded in database, the image itself and thumbnails are saved in the specified directories in my application. But I want to save the path of the thumbnails inside the database soe that I can use it in my view.
Would really appreciate the help as I'm so new to cakephp and I have to figure it out in a short amount of time.
Thanks.
You may make mistake in here it should be
'fields' => array(
'dir' => 'image_thumb' //table field name
),

Resources