Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to check whether form has file or not of it has I want to upload it to server and save path if not then ignore and run old dB saved path query
You can try in this way first validate your all inputs as per your requirement then check for the file in this way :
Am assuming logo file here from form filed :
if($request->hasFile('logo')){
// Get filename with the extension
$filenameWithExt = $request->file('logo')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('logo')->getClientOriginalExtension();
$fileNameToStore=$filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('logo')->move('webimg/', $fileNameToStore);
// Filename to store
$fileNameToStore= $path;
use the hasFile method on the request:
if ($request->hasFile('name_of_file_input_fiels')) {
//
}
https://laravel.com/docs/5.6/requests#retrieving-uploaded-files
if ($request->file('name_of_file_input_fields')) {
}
you can check also this way.
use laravel File validator
$rules = array(
'file' => 'required'
);
You can check documentation here
Here is an example: Tutorial with file upload validation
It's pretty simple.
$request->hasfile('your key');
Related
laravel 8 pagination using this style for links
www.website.com/huawei?page=3
how i can make it like this?
www.website.com/huawei/3
There is not specific way. so we need to play around the existing things.
But I had made it one of my application a long time before not tested much.
may be there is much better way also to achieve it.
so may it will work same for you. Please check below way to do it as pretty url.
Route :
Route::get('/articles/page/{page_number?}', function($page_number=1){
$per_page = 1;
Articles::resolveConnection()->getPaginator()->setCurrentPage($page_number);
$articles = Articles::orderBy('created_at', 'desc')->paginate($per_page);
return View::make('pages/articles')->with('articles', $articles);
});
View :
$links = $articles->links();
$patterns = array();
$patterns[] = '/'.$articles->getCurrentPage().'\?page=/';
$replacements = array();
$replacements[] = '';
echo preg_replace($patterns, $replacements, $links);
Best Solution I had found : Please use below package for laravel which help you.
https://packagist.org/packages/spatie/laravel-paginateroute
I have the following code to upload and store the file.
$user->update([
'display_profile' => request()->display_profile->storeAs('avatars', $name,'public')
]);
This stores the file in the display_profile as avatars/filename.jpg.
Since I have multiple versions of the files for displaying around the views I am using prefixes like follow
thumb_filename.jpg
small_filename.jpg
large_filename.jpg
I will need to do a lot of string replace to insert the prefix in place to show the right version of the images. Is there anyway I can save just the filename in the database insteat of the full path?
If not whats the best way to show my files in the view?
I use this approach mostly:
$file = $request->file('display_profile');
$name = $file->getClientOriginalName() . '.' . $file->extension();
$file->storeAs('public/avatars', $name);
$user->update([
'display_profile' => $name
]);
When you retrieve a file from the request, this will be converted as an UploadedFile object. You can easily retrieve the original name as follow
$file = $request->display_profile;
$user->update([
'display_profile' => $file->getClientOriginalName()
]);
For your second question I suggest you to create a relation with a Image class where you can store all the image conversion you require. But this can be a little tricky and may require time to be coded. Fortunately there are tons of libraries that can handle this for you. My favourite is Laravel medialibrary
I have a program that can print an individual PDF when on a students file. I'm doing this using niklasravnsborg/laravel-pdf package in Laravel 5.7. It's working great because I can just stream the pdf from a view into the browser then print from there.
Now I'm wanting to batch print the PDF's at the end of the day instead of one by one. How can I do this? There's no documentation for this on the package repository.
Several ways I've thought of doing this: one, save each PDF as an image file then try to print all files in that folder at the end of the day. If I do this, how would I print all files in that folder?
Next: does anyone know a way to maybe append a new PDF to a variable containing all the previously looped pdf's?
For example:
$finalpdf;
$students = Student::all();
foreach($students as $student){
$pdf = PDF::loadView('pdf.document', $student);
$finalpdf .+ $pdf; //i know this line doesn't work, but how to alter it?
return $pdf->save('document.pdf');
}
After a few days of playing around, hopefully this solution helps someone in the future. I used another package called "lara pdf merger". By writing my files to a save destination, adding those files to a merged file, then deleting the saved files after download, I was able to get my functionality. This is my controller code:
use PDF;
use Illuminate\Filesystem\Filesystem;
use LynX39\LaraPdfMerger\Facades\PdfMerger;
public function batchPrint(){
$pdfMerger = PDFMerger::init();
$i = 0;
$students = Student::whereDate('updated_at', Carbon::today('America/Los_Angeles'))->get();
foreach ($students as $student) {
$pdf = PDF::loadView('printTables', compact('student'));
$pdf->save('pdf/document'.$i.'.pdf');
$pdfMerger->addPDF('pdf/document'.$i.'.pdf');
$i++;
}
$pdfMerger->merge();
$pdfMerger->save("file_name.pdf", "browser");
$file = new Filesystem;
$file->cleanDirectory('pdf');
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to use Laravel isDirty() which helps me to get only modified values,
But there is a problem when I'm trying to use, it always return false
public function update(Request $request, $id) {
$client = Client::find($id);
dd($client->isDirty('name'));
}
It always return false.
It returns false because you haven't done anything to $client.
Thus, it is not "dirty" - it is "clean".
If you do something like $client->name = str_random(40); it'll become dirty.
To get only modified attributes, you need to use the getDirty() method. isDirty() only shows if there are any modified attributes:
$client = Client::find($id);
$client->name = 'Some New Name';
$modifiedAttributes = $client->getDirty();
If you want to check if any of properties were modified in the submitted form or not, you can do it like this:
if ($client->name === $request->name)
Does anyone know how you get get video lengths in laravel (preferably in the validator)?
I know its possible to check for video extension and the file size but I want to get the actual length of the video too.
Thank you.
Laravel doesn't support video length natively, but you can extend the Validation functionality to create your own rule. But you'll need to rely on a library to determine the video length:
// Rule
$rules = [
'video' => 'required|file|video_length:5000',
];
// Custom validation rule
Validator::extend('video_length', function( $attribute, $value, $parameters ) {
$length = 0; // check length using library
return $length <= $parameters[0];
});
You'll need to upload the video to your server. Check the length of the video using the library of your choice and return error if it exceeds your restrictions.
The ID3 library may be able to help.