Adding 'Read More' link in Laravel 4 - laravel-4

I have trying to excerpt some text and add Read More link. So far I was able to cut the text and add Read More but now I can to make few lines between text and read more link.
Here is how I truncated the text
{{ str_limit($product['description_small'], $limit = 250, $end = ' Read More') }}
How can I put this $end = ' Read More' few lines under the text and to make it link?
Laravel 4 + blade template

The best thing you can do is create your own function to create the excerpt and add the Read More link.
First of all in your composer.json file, in the autolaod section add a files array:
{
"autoload": {
"files": [
"app/helpers.php"
]
}
}
Now in your app/ folder create the helpers.php file.
Then add in app\start\global.php: ( NB: this is required only for Laravel 4 )
// at the bottom of the file
require app_path().'/helpers.php';
Then:
composer dump-autoload
In this way Laravel will load the file for you.
Now in this file you can create your onw function to manage the excerpt:
<?php
if( ! function_exists('createExcerptAndLink')){
function createExcerptAndLink($text, $limit, $url, $readMoreText = 'Read More') {
$end = "<br><br>$readMoreText";
return str_limit($text, $limit, $end);
}
}
Now in every view you can use your new helper:
{{ createExcerptAndLink($product['description_small'], 250, "http://www.google.it", 'Read more here.') }}

Related

I want to Download multiple images as a zip file using laravel 7

I am a beginner at laravel and I want to downloads multiple images as a zip file using laravel but I do not have an idea how can I do that please help me thanks.
InboxController
public function dowloads($id)
{
$clientfiles = Inbox::where('id', $id)->first();
dd($clientfiles->file);
// "["phpIgRq3Q.jpg","phpe6b34M.jpg","phpnPGN2M.png","php8CQh5P.jpg"]"
$files =config('yourstitchart.file_url');
// $files = "http://localhost/yourstitchart.com/web/public/uploads/images/"
}
HTML view
<a href="{{ route('download.inbox',$digitizingInbox->id) }}" class="download
btn btn-warning">Download
</a>
Route
Route::get('downloads/{id}/files','DigitizingInboxController#dowloads')->name('download.inbox');
You can also use Chumper/Zipper package to make zip files. It is used by many developers. check here
You can try it like:
$zipper = Zipper::make(public_path('/documents/deals.zip'));
$clientfiles = Inbox::where('id', $id)->first();
foreach ($clientfiles->file as $file) {
$zipper->add(public_path($file)); // update it by your path
}
$zipper->close();
return response()
->download(
public_path('/temporary_files/' . "deals.zip"),
"deals.zip",
["Content-Type" => "application/zip"]
);

laravel blade include files with relative path

In laravel blade system when we want to include a partial blade file we have to write the full path every time for each file. and when we rename a folder then we will have to check every #include of files inside it. sometimes it would be really easy to include with relative paths. is there any way to do that?
for example we have a blade file in this path :
resources/views/desktop/modules/home/home.blade.php
and I need to include a blade file that is near that file :
#include('desktop.modules.home.slide')
with relative path it would be something like this :
#include('.slide')
is there any way to do this?
if someone still interest with relative path to current view file, put this code in the boot method of AppServiceProvider.php or any provider you wish
Blade::directive('relativeInclude', function ($args) {
$args = Blade::stripParentheses($args);
$viewBasePath = Blade::getPath();
foreach ($this->app['config']['view.paths'] as $path) {
if (substr($viewBasePath,0,strlen($path)) === $path) {
$viewBasePath = substr($viewBasePath,strlen($path));
break;
}
}
$viewBasePath = dirname(trim($viewBasePath,'\/'));
$args = substr_replace($args, $viewBasePath.'.', 1, 0);
return "<?php echo \$__env->make({$args}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
});
and then use
#relativeInclude('partials.content', $data)
to include the content.blade.php from the sibling directory called partials
good luck for everyone
you need to create custom blade directive for that, the native include directive doesn't work like that.
read this page to learn how to create custom blade directive :
https://scotch.io/tutorials/all-about-writing-custom-blade-directives
\Blade::directive('include2', function ($path_relative) {
$view_file_root = ''; // you need to find this path with help of php functions, try some of them.
$full_path = $view_file_root . path_relative;
return view::make($full_path)->render();
});
then in blade file you can use relative path to include view files :
#include2('.slide')
I tried to tell you the idea. try and test yourself.
There’s now a package doing both relative and absolute includes (lfukumori/laravel-blade-include-relative) working with #include, #includeIf, #includeWhen, #each and #includeFirst directives. I just pulled it in a project, it works well.
A sleek option, in case you want to organise view files in sub-folders:
public function ...(Request $request) {
$blade_path = "folder.subfolder.subsubfolder.";
$data = (object)array(
".." => "..",
".." => $..,
"blade_path" => $blade_path,
);
return view($data->blade_path . 'view_file_name', compact('data'));
}
Then in the view blade (or wherever else you want to include):
#include($blade_path . 'another_view_file_name')

Remove unused Images/files from upload folder laravel

I have laravel5.4 application.I want to remove unused images/files from my upload folder which is not available in my database.
For example :
I have 50 images in my upload folder for user profile but some of the image not use for any user.i think he removed or update his image from frontend.
Yes i know we need to code to remove file when user update or remove profile picture at a time also delete from upload folder.but my app run from many time and i want to remove unused file using script not manually beacause i have lot's of files so it's hard to check and remove file manually.anyone can you please help me for create any function for remove file from folder.
Sorry for my bad English.
I use something like this in my AdminController to remove images by clicking on a button.
Maybe you need to change the path or extensions
public function deleteUnusedImages()
{
$file_types = [
'gif',
'jpg',
'jpeg',
'png'
];
$directory = public_path();
$files = File::allFiles($directory);
foreach ($files as $file)
{
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($ext, $file_types)) {
if(DB::table('users')->where('votes', '=', $file)->count())
continue; // continue if the picture is in use
echo 'removed' . basename($file)."<br />";
unlink($file); // delete if picture isn't in use
}
}
}

Laravel EXCEL and PDF export

I am new in Laravel and I am using Laravel 4.2
I like to export some data in PDF and excel.
Is there any way in Laravel?
Use FPDF to do what u need. You must create a pdf file from scratch and fill it in the way u want.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage(); // add page to PDF
$pdf->SetFont('Arial','B',16); // Choose a font and size
$pdf->Cell(40,10,'Hello World!'); // write anything to any line you want
$pdf->Output("your_name.pdf"); // Export the file and send in to browser
?>
And for Excel a simple way is to add PHPExcel to laravel. Add this line to your composer.json:
"require": {
"phpexcel/phpexcel": "dev-master"
}
then run a composer update. So use it like this:
$ea = new PHPExcel();
$ea->getProperties()
->setCreator('somebody')
->setTitle('PHPExcel Demo')
->setLastModifiedBy('soembody')
->setDescription('A demo to show how to use PHPExcel to manipulate an Excel file')
->setSubject('PHP Excel manipulation')
->setKeywords('excel php office phpexcel')
->setCategory('programming')
;
$ews = $ea->getSheet(0);
$ews->setTitle('Data');
$ews->setCellValue('a1', 'ID'); // Sets cell 'a1' to value 'ID
$ews->setCellValue('b1', 'Season');
Use maatwebsite to Create and import Excel, CSV and PDF files
Add this lines to your composer.json:
"require": {
"maatwebsite/excel": "~2.1.0"
}
After updating composer, add the ServiceProvider to the providers array in config/app.php
Maatwebsite\Excel\ExcelServiceProvider::class,
You can use the facade for shorter code. Add this to your aliasses:
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
To publish the config settings in Laravel 5 use:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
simple use of this package:
$users = User::select('id','name' ,'username')->get();
$Info = array();
array_push($Info, ['id','name' ,'username']);
foreach ($users as $user) {
array_push($Info, $user->toArray());
}
Excel::create('Users', function($excel) use ($Info) {
$excel->setTitle('Users');
$excel->setCreator('milad')->setCompany('Test');
$excel->setDescription('users file');
$excel->sheet('sheet1', function($sheet) use ($Info) {
$sheet->setRightToLeft(true);
$sheet->fromArray($Info, null, 'A1', false, false);
});
})->download('xls'); // or download('PDF')

How to create a folder inside a folder using laravel

In new in laravel.
I trying to create a folder inside a folder using laravel.I want to create a new folder inside the another folder .what is the html code for blade.php page.
you can use File::makeDirectory to create folder inside folder
$path = public_path().'/uploads/test';
File::makeDirectory($path,0777, true, true);
List All Directory for user john
main directory john_1
1.1 john has 3 sub directory abc,xyz and mno
get all directory of user john and pass it view from controller
$list = File::directories('/john_1');
return View::make('upload',array('list' => $list));
finally display it as selectbox
{{ Form::select('folder_name', $list, null, array('class' => 'form-control')) }}
list all image from user john
foreach (File::allFiles(public_path().'/john_1/img/') as $file) {
$filename = $file->getRelativePathName();
echo HTML::image('public/john_1/img/'.$filename, $filename);
}
This will attempt to create /uploads if it doesn't exist. Then
/uploads/test if it doesn't exist.If all created directories are
successfully created then true will be returned.

Resources