How to delete files or an image in laravel 5.4 - laravel-5.4

How to delete files in laravel 5.4
I tried this
\Storage::delete($path);
and this
\File::delete($path);
and it told me Method delete does not exist

I think your Files and Images are in Folder and Database. So , you need to Delete Files and Images from Folder and Database. I think you have already created a Delete button in View file and you have passed the id. Then try this..
after namespace App\Http\Controllers; , write this below codes.
use DB;
use File;
In your Controller file code like this.
I think your Files and Images store in public folder. That why I use \File::
function delete($id)
{
$query = DB::table('academic')->where('id',$id);
$files_to_delete = $query->pluck('Db_ImageorFile_Column_Name')->toArray(); //keeping the result in a php
$query->delete(); // For delete from database
\File::delete($files_to_delete); // For delete from folder
return redirect('Your_Page_Name');
}
If you have further Questions , use these Links -
) How To Delete Files Or Images From Folder And Database In Laravel?
) How to delete file from public folder in laravel 5.1
) https://laracasts.com/discuss/channels/laravel/how-to-delete-a-file-or-a-photo-from-the-folder-when-deleting-it-from-database?page=1

Related

Fetching mysql data is not working in laravel after importing CSV file manually

I have a model TestDirectory in laravel and test-directory table in mysql. After i manually importing a CSV file in to the mysql table via phpmyadmin, calling TestDirectory::all() is not fetching any data. But previously it was working fine.
Any direction please.
Add to your model:
protected $table = 'your_table';
You should try fillable property on model
protected $fillable = [
'colume_name',
'colume_name2'
];

Load an image with a relative path using mysql command line

I'm using Flyway to start MYSQL script at the start of my spring project but i'm currently having a problem because i want to load an image into my database using a relative path because my project will be on another server and i want to load the file from Flyway.
The problem is that i can't use the LOAD_FILE function from mysql since it only uses a absolute path.
UPDATE table
SET image = LOAD_FILE('/path/to/folder/image.jpg')
WHERE id=1;
I'm currently looking for another way to do it, if you have any advice i would greatly appreciate it
As advised by Jones, i used a Java Based Migration by creating a folder db/migration in my Java folder.
Inside this folder i have a migration with :
public void migrate(final Context context) throws Exception {
Connection con = context.getConnection();
InputStream image = new ClassPathResource("image/image.jpg").getInputStream();
String requestUpdate = "UPDATE table SET image = ? WHERE id=?";
PreparedStatement preparedStatementUpdate = con.prepareStatement(requestUpdate);
preparedStatementUpdate.setBlob(1, image );
preparedStatementUpdate.setString(2, "1");
preparedStatementUpdate.execute();
}

Db Schema for custom file navigation with laravel and mysql

The problem
Hi, i want to have a web app on which the user could navigate through a system of directory and subdirectory and download / see the files. ( similar to a file manager but they have some important limitation for this project limitation )
I want to create a panel in which i can navigate ( through post request ) to the next folder or download the file and upload file.
There are two categories of user :
user A ( can view user A and B files )
user B ( can view only view user B files )
Possible solution
What if i create a table in such way
-- --- --
path -> string -> primary key ( this path is equals to the path in the file system )
isDirectory -> boolean
canViewByUserB -> boolean
-- --- --
The path field is unique so i cannot have two file with the same name in the same folder.
So, when the panel is create, i search with a post request for a path that doesn't containt " / ", ( maybe with a LIKE statement ?) so it's in the root folder.
If this query retrieve a:
directory, if the user click on it, i do a post request and search for a path that start with "nameOfThisDirectory/someString" but it must not contain onother / ( in this way i retrive the directory child )
a file, get request to download the file with the specify path
EDIT
To traverse the file system through the db, here the query
select * from file where path REGEXP '^([^/]*[/]){2}[^/]*$';
the '2' indicate two /
what do you think of this solution?
With this setup i can easily do some things, such as
create a total download number of a file
number of visualization on a file
easily navigate through the file system
apply policies on files ( user a and user b )
Thanks, waiting for advice

Create a folder if it doesn't exist

Create a folder if it doesn't exist in Magento, I don't want to use mkdir(). Is there any way to do this ? May be core_file_uploader can work out, but its throwing error.
Magento has Varien_Io_File class for quite many filesystem works.
So instead of using PHP's vanilla mkdir() function, you can use the Magento's way:
$io = new Varien_Io_File();
$io->mkdir(your_path_to_dir);
Or you can also check if the directory does not exist right before creating it
$io = new Varien_Io_File();
if (!$io->fileExists(your_path_to_dir, false)) {
$io->mkdir(your_path_to_dir);
}

Laravel seed database from existing database

i have now new structure of my database, but i need to import the old data in the new format. For that reason i want to use the Laravel seeder, but i need somehow to connect to the old database and make select queries and to tell the seeder how to put the data in the new database.
Is that possible ?
Try:
Examples:
php artisan iseed my_table
php artisan iseed my_table,another_table
Visit: https://github.com/orangehill/iseed
Configure your laravel app to use two mysql connections (How to use multiple database in Laravel), one for the new database, the other for the old one.
I'll fake it like old and new.
In your seeds read from the old database and write into the new.
$old_user = DB::connection('old')->table('users')->get();
foreach ($old_users as $user) {
DB::connection('new')->table('users')->insert([
'name' => $user->name,
'email' => $user->email,
'password' => $user->password,
'old_id' -> $user->id
// ...
]);
}
Make sure to add messages while seeding like $this->command->info('Users table seeded'); or even a progress bar (you can access command line methods) to know at which point of the import you are.
Download package from
Git repo : https://github.com/orangehill/iseed
then update below file src/Orangehill/Iseed/IseedCommand.php
Add below code at line number 75
// update package script
if($this->argument('tables') === null){
$tables = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
}
and update getArguments method in same file with below code
array('tables', InputArgument::OPTIONAL, 'comma separated string of table names'),
and then run php artisan iseed so it will get all the tables from your existing db and start creating seeders for all tables