corrupted file after upload in laravel public disk - laravel

I'm using this code to upload some file to storage in laravel:
$username=Auth::user()->name;
$patz=Storage::url("users/".$username."_".Auth::user()->id);
$now= new \Carbon\Carbon();
$fileName = 'ddt_'.$username."_".$now->getTimestamp().'.'.$request->ddt_file->getClientOriginalExtension();
$pathComplete=$request->ddt_file->storeAs($patz,$fileName);
$ddt_file_url= new Ddt_file_url(); //salvare ddt e poi prendere id
$ddt_file_url->fileUrl = $pathComplete;
$ddt_file_url->fileName = $fileName;
$ddt_file_url->company()->associate(Auth::user()->id);
$ddt_file_url->save();
The file is present in the folder but the file is corrupted and when I try do dowload it this not works.
This is the code that im using for the download too:
#if($ddtR->ddt_file_url_id)
<a href="{{asset(Storage::url($ddt_urls->where('id',$ddtR->ddt_file_url_id)->first()->fileUrl))}}" download="{{$ddt_urls->where('id',$ddtR->ddt_file_url_id)->first()->fileName}}">
<button type="button" class="btn btn-default">
Download ddt file <span class="fa icon-icona_download-01">
</span>
</button>
</a>
#else
<p>-</p>
#endif
I ran several dd() over $patz and $pathComplete to me both looks correct!

this is how I solve it... I dont know if its the best and cleaner solution:

Related

How to set up a beautiful url in laravel 8?

Now my url looks like this
localhost/3
But I want the path to look like this
localhost/some-text
To do this, I have a special url field in the Ad model, where the desired address is located. But going through it, I get 404
I have a route
Route::get('/{url}', [App\Http\Controllers\IndexController::class, 'show'])->name('show');
Method
public function show($url) {
$ad = Ad::where('url', $url)->first();
return view('ad', compact('ad'));
}
And btn
<a href="{{ route('show', ['url' => $ad->url]) }}">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
</a>
To create better links just use slug
I use laravel slugable

Thymeleaf th:onclick event - calling confirm function

I am new to Thymeleaf. Recently I stumbled in the following situation. Here is a piece of my Thymeleaf html page:
<!-- an delete button link -->
<a th:href="#{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:onclick="if(!(confirm('Are you sure you want to delete this employee ?') )) return false" >
Delete
</a>
This code works fine as intended. However I want to add employee name as part of the confirmation. Here is the code:
<!-- an delete button link -->
<a th:href="#{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:onclick="if(!(confirm('Are you sure you want to delete this employee ' + '\'+${tempEmployee.firstName}+\'' +'?' ) )) return false" >
Delete
</a>
Unfortunately the result is:
Are you sure you want to delete this employee
'+${tempEmployee.firstName}+'.
Looks like Thymeleaf does not recognize ${tempEmployee.firstName}. It has no problem with it in th:href tag but does not like it in th:onclick.
I would appreciate if somebody can turn me into the right direction.
Not sure exactly what the problem is (though it may be related to onclick vs th:onclick. Regardless, I think a format more like this will work (with some added benefits like no JavaScript injection).
<!-- an delete button link -->
<a
th:href="#{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:data-confirm-delete="|Are you sure you want to delete this employee ${tempEmployee.firstName}?|"
onclick="if (!confirm(this.getAttribute('data-confirm-delete'))) return false"
>
Delete
</a>
(Notice I'm using onlick and not th:onclick.
Instead of this line in above code ---onclick="if (!confirm(this.getAttribute('data-confirm-delete'))) return false">
You can write as:
onclick="return confirm(this.getAttribute('data-confirm-delete'))"

Laravel WhereIn Doesn't Accept Array Value

I have this on my blade file:
{{ Form::open(['route' => 'my_route_name']) }}
<button type="submit" class="btn btn-sm btn-success">
<i class="fa fa-file-excel-o" aria-hidden="true"></i> Download
</button>
{{ Form::hidden('my_ids', $my_ids) }}
{{ Form::close() }}
Checking on the chrome's developer mode, the value of my hidden textbox named my_ids is:
[1,2,3,4,5,6]
Upon clicking the Download button, it goes on my controller:
$results= Model::whereIn('id', $request->my_ids)->get();
This is where I am getting an error.
DD-ing dd($request->my_ids) on my controller gives me "[1,2,3,4,5,6]".
However, if I just put the values directly on the eloquent query like below, it would work.
$results= Model::whereIn('id', [1,2,3,4,5,6])->get();
Am I missing something here?
Your dd shows that $request->my_ids is a string, therefore you must parse it before you use it as array.
Try
$results= Model::whereIn('id', json_decode($request->my_ids))->get();

unable to retrieve images from storage folder - path from database

I'm using Laravel 5.7 and I wish to upload images and place the path into database, it's easy and I did it, all is cool. now trying to retrieve the image, the path is correct and I tried to change the settings of the folders and the storage, I tried to find every single solution out there for 7 hours and still i can't find the issue, please advise.
I tried to change the storage from local to public and tried to use different methods to recall the image but all failed.
<div class="card">
<img class="card-img-top" src="{{ asset('storage/'.$post->background_image) }}">
<img class="card-img-top" src="/storage//{{$post['first_image']}}" alt="Card image cap">
<div class="card-body">
<h4 class="card-title">{{$post['post_name']}}</h4>
<p class="card-text">{{$post['description']}}</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Item 1</li>
<li class="list-group-item">Item 2</li>
<li class="list-group-item">Item 3</li>
</ul>
</div>
//as for saving the file i tried the below options:
$path1 = $request->file('background_image')->store('background_image');
if ($validatedImages) {
$fileNamewithExt= $request->file('background_image')
->getClientOriginalName();
$filename= pathinfo($fileNamewithExt, PATHINFO_FILENAME);
$extension = $request ->file('background_image')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path1 = $request->file('background_image')
->storeAs('/background_image',$fileNameToStore);
}
I expect the image to load instead of appearing unavailable, the path loads from the database, but for some reason the image doesn't load.

Laravel included template is not parsing / running foreach loop

I have included a couple of files.
my home.blade.php has the following:
#include('includes.header')
my header.blade.php in includes folder has the following
#include('elements.pendingTasksLi', array('tasks'=>$tasks))
and my elements/pendingTasksLi.php file has the following
#foreach ($tasks as $task)
<li>
<a class="todo-actions" href="javascript:void(0)">
<i class="fa fa-square-o"></i>
<span class="desc" style="opacity: 1; text-decoration: none;">{{{$task\['title'\]}}}</span>
<span class="label label-danger" style="opacity: 1;"> {{{$task\['deadLineTime'\]}}}</span>
</a>
</li>
#endforeach
This code in the pendingTakstsLi file is never executed as a loop but only as a simple text.
This is what the output looks like...
#foreach ($tasks as $task)
{{{$task['title']}}} {{{$task['deadLineTime']}}}
#endforeach
I am not sure what to do... Please advise...
Check a screen here
http://i.stack.imgur.com/8emZk.png
Thanks
Jyot
You mentioned that your "pendingTasksLi" view ends in ".php" and not ".blade.php" which would prevent it from being parsed as a blade view.
I had the same problem and as mentioned above you forgot the .blade before the .php
If you want to use expressions like this: {{ $variable}} and want them to be parsed you always have to add .blade in the name pefore the .php extension of the file.

Resources