code_web – CodeWeb https://codeweb.wall-spot.com Code the web easy Sun, 07 Jul 2024 06:59:32 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://i0.wp.com/codeweb.wall-spot.com/wp-content/uploads/2022/06/code-web.png?fit=32%2C32&ssl=1 code_web – CodeWeb https://codeweb.wall-spot.com 32 32 214557421 Check If ImageMagick and Ghostscript are installed on the Server https://codeweb.wall-spot.com/check-if-imagemagick-and-ghostscript-are-installed-on-the-server/ Sun, 07 Jul 2024 06:35:52 +0000 https://codeweb.wall-spot.com/?p=135 Create a PHP file. Let’s say it is in the main directory, so I go with index.php and paste the below code.

Open the website and it will show you a text message if there is software suit installed or not.

<?php
// Function to check if a command exists on the server
function isCommandAvailable($command) {
    $whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'which';
    $process = proc_open(
        "$whereIsCommand $command",
        [
            1 => ['pipe', 'w'], // stdout is a pipe that the child will write to
            2 => ['pipe', 'w'], // stderr is a pipe that the child will write to
        ],
        $pipes
    );
    if ($process !== false) {
        $stdout = stream_get_contents($pipes[1]);
        $stderr = stream_get_contents($pipes[2]);
        fclose($pipes[1]);
        fclose($pipes[2]);
        $returnCode = proc_close($process);
        return $returnCode === 0;
    }
    return false;
}

// Check if Ghostscript is installed
$ghostscriptInstalled = isCommandAvailable('gs');

// Check if ImageMagick is installed
$imagemagickInstalled = isCommandAvailable('convert');

// Output results
echo 'Ghostscript is ' . ($ghostscriptInstalled ? 'installed' : 'not installed') . ".<br>";
echo 'ImageMagick is ' . ($imagemagickInstalled ? 'installed' : 'not installed') . ".<br>";
?>

Just run the PHP file and it will show you if your server has ImageMagick and Ghostscript.

Imagemagik and Ghostscript check using php
    ]]>
    135
    How To Call Social Link In The Loop https://codeweb.wall-spot.com/how-to-call-social-link-in-the-loop/ Wed, 10 Apr 2024 05:11:25 +0000 https://codeweb.wall-spot.com/?p=130 Calling similar tags or input fields in a loop. Let us skip the repeating input text box with a PHP loop in Laravel.

    Don’t just paste if you can use the loop

    Suppose you have a database table called “socialtables” and it has 5 column names followed by numbers to let the user to select which social media they want to save.

    Soc the database structure is like this:

    Social profile database
    socialtables
                 |social_icon_1|
                 |social_link_1|
                 |social_icon_2|
                 |social_link_2|
                 |social_icon_3|
                 |social_link_3|
                 |social_icon_4|
                 |social_link_4|
                 |social_icon_5|
                 |social_link_5|

    As you in the above example, socialtables database table has five social profiles to be flooded with social site links and icons. Only the numbers differ. So we have decided to call the numbers using a loop and keep the rest.

    Social site data we are going to save

    social_icon_1 = [image/icon]

    social_linnk_1 = https://fb.com/pagename

    How the user interface will look like?

    social input loop

    We will let the user select the social icons to choose from and then they can enter the site links. Let the admin/your client decide what icons to display.

    For this, we will need predefined data to be flooded. For this, you can use a database filled with data or you can use a Laravel controller.

    Use the array in the controller

    public function sitesettings()
        {
            $socialicons = [
                'facebook' => [
                    'icon' => '<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 8 19"><path fill-rule="evenodd" d="M6.135 3H8V0H6.135a4.147 4.147 0 0 0-4.142 4.142V6H0v3h2v9.938h3V9h2.021l.592-3H5V3.591A.6.6 0 0 1 5.592 3h.543Z" clip-rule="evenodd"/></svg>'
                ],
                'discord' => [
                    'icon' => '<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 21 16"><path d="M16.942 1.556a16.3 16.3 0 0 0-4.126-1.3 12.04 12.04 0 0 0-.529 1.1 15.175 15.175 0 0 0-4.573 0 11.585 11.585 0 0 0-.535-1.1 16.274 16.274 0 0 0-4.129 1.3A17.392 17.392 0 0 0 .182 13.218a15.785 15.785 0 0 0 4.963 2.521c.41-.564.773-1.16 1.084-1.785a10.63 10.63 0 0 1-1.706-.83c.143-.106.283-.217.418-.33a11.664 11.664 0 0 0 10.118 0c.137.113.277.224.418.33-.544.328-1.116.606-1.71.832a12.52 12.52 0 0 0 1.084 1.785 16.46 16.46 0 0 0 5.064-2.595 17.286 17.286 0 0 0-2.973-11.59ZM6.678 10.813a1.941 1.941 0 0 1-1.8-2.045 1.93 1.93 0 0 1 1.8-2.047 1.919 1.919 0 0 1 1.8 2.047 1.93 1.93 0 0 1-1.8 2.045Zm6.644 0a1.94 1.94 0 0 1-1.8-2.045 1.93 1.93 0 0 1 1.8-2.047 1.918 1.918 0 0 1 1.8 2.047 1.93 1.93 0 0 1-1.8 2.045Z"/></svg>'
                ],
                'twitter' => [
                    'icon' => '<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 17"><path fill-rule="evenodd" d="M20 1.892a8.178 8.178 0 0 1-2.355.635 4.074 4.074 0 0 0 1.8-2.235 8.344 8.344 0 0 1-2.605.98A4.13 4.13 0 0 0 13.85 0a4.068 4.068 0 0 0-4.1 4.038 4 4 0 0 0 .105.919A11.705 11.705 0 0 1 1.4.734a4.006 4.006 0 0 0 1.268 5.392 4.165 4.165 0 0 1-1.859-.5v.05A4.057 4.057 0 0 0 4.1 9.635a4.19 4.19 0 0 1-1.856.07 4.108 4.108 0 0 0 3.831 2.807A8.36 8.36 0 0 1 0 14.184 11.732 11.732 0 0 0 6.291 16 11.502 11.502 0 0 0 17.964 4.5c0-.177 0-.35-.012-.523A8.143 8.143 0 0 0 20 1.892Z" clip-rule="evenodd"/></svg>'
                ],
                'github' => [
                    'icon' => '<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 .333A9.911 9.911 0 0 0 6.866 19.65c.5.092.678-.215.678-.477 0-.237-.01-1.017-.014-1.845-2.757.6-3.338-1.169-3.338-1.169a2.627 2.627 0 0 0-1.1-1.451c-.9-.615.07-.6.07-.6a2.084 2.084 0 0 1 1.518 1.021 2.11 2.11 0 0 0 2.884.823c.044-.503.268-.973.63-1.325-2.2-.25-4.516-1.1-4.516-4.9A3.832 3.832 0 0 1 4.7 7.068a3.56 3.56 0 0 1 .095-2.623s.832-.266 2.726 1.016a9.409 9.409 0 0 1 4.962 0c1.89-1.282 2.717-1.016 2.717-1.016.366.83.402 1.768.1 2.623a3.827 3.827 0 0 1 1.02 2.659c0 3.807-2.319 4.644-4.525 4.889a2.366 2.366 0 0 1 .673 1.834c0 1.326-.012 2.394-.012 2.72 0 .263.18.572.681.475A9.911 9.911 0 0 0 10 .333Z" clip-rule="evenodd"/></svg>'
                ],
                'dribbble' => [
                    'icon' => '<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 0a10 10 0 1 0 10 10A10.009 10.009 0 0 0 10 0Zm6.613 4.614a8.523 8.523 0 0 1 1.93 5.32 20.094 20.094 0 0 0-5.949-.274c-.059-.149-.122-.292-.184-.441a23.879 23.879 0 0 0-.566-1.239 11.41 11.41 0 0 0 4.769-3.366ZM8 1.707a8.821 8.821 0 0 1 2-.238 8.5 8.5 0 0 1 5.664 2.152 9.608 9.608 0 0 1-4.476 3.087A45.758 45.758 0 0 0 8 1.707ZM1.642 8.262a8.57 8.57 0 0 1 4.73-5.981A53.998 53.998 0 0 1 9.54 7.222a32.078 32.078 0 0 1-7.9 1.04h.002Zm2.01 7.46a8.51 8.51 0 0 1-2.2-5.707v-.262a31.64 31.64 0 0 0 8.777-1.219c.243.477.477.964.692 1.449-.114.032-.227.067-.336.1a13.569 13.569 0 0 0-6.942 5.636l.009.003ZM10 18.556a8.508 8.508 0 0 1-5.243-1.8 11.717 11.717 0 0 1 6.7-5.332.509.509 0 0 1 .055-.02 35.65 35.65 0 0 1 1.819 6.476 8.476 8.476 0 0 1-3.331.676Zm4.772-1.462A37.232 37.232 0 0 0 13.113 11a12.513 12.513 0 0 1 5.321.364 8.56 8.56 0 0 1-3.66 5.73h-.002Z" clip-rule="evenodd"/></svg>'
                ],
                'youtube' => [
                    'icon' => '<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="bi bi-youtube w-4 h-4" viewBox="0 0 16 16"><path d="M8.051 1.999h.089c.822.003 4.987.033 6.11.335a2.01 2.01 0 0 1 1.415 1.42c.101.38.172.883.22 1.402l.01.104.022.26.008.104c.065.914.073 1.77.074 1.957v.075c-.001.194-.01 1.108-.082 2.06l-.008.105-.009.104c-.05.572-.124 1.14-.235 1.558a2.01 2.01 0 0 1-1.415 1.42c-1.16.312-5.569.334-6.18.335h-.142c-.309 0-1.587-.006-2.927-.052l-.17-.006-.087-.004-.171-.007-.171-.007c-1.11-.049-2.167-.128-2.654-.26a2.01 2.01 0 0 1-1.415-1.419c-.111-.417-.185-.986-.235-1.558L.09 9.82l-.008-.104A31 31 0 0 1 0 7.68v-.123c.002-.215.01-.958.064-1.778l.007-.103.003-.052.008-.104.022-.26.01-.104c.048-.519.119-1.023.22-1.402a2.01 2.01 0 0 1 1.415-1.42c.487-.13 1.544-.21 2.654-.26l.17-.007.172-.006.086-.003.171-.007A100 100 0 0 1 7.858 2zM6.4 5.209v4.818l4.157-2.408z"/></svg>'
                ],
                'instagram' => [
                    'icon' => '<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="bi bi-instagram w-4 h-4" viewBox="0 0 16 16"><path d="M8 0C5.829 0 5.556.01 4.703.048 3.85.088 3.269.222 2.76.42a3.9 3.9 0 0 0-1.417.923A3.9 3.9 0 0 0 .42 2.76C.222 3.268.087 3.85.048 4.7.01 5.555 0 5.827 0 8.001c0 2.172.01 2.444.048 3.297.04.852.174 1.433.372 1.942.205.526.478.972.923 1.417.444.445.89.719 1.416.923.51.198 1.09.333 1.942.372C5.555 15.99 5.827 16 8 16s2.444-.01 3.298-.048c.851-.04 1.434-.174 1.943-.372a3.9 3.9 0 0 0 1.416-.923c.445-.445.718-.891.923-1.417.197-.509.332-1.09.372-1.942C15.99 10.445 16 10.173 16 8s-.01-2.445-.048-3.299c-.04-.851-.175-1.433-.372-1.941a3.9 3.9 0 0 0-.923-1.417A3.9 3.9 0 0 0 13.24.42c-.51-.198-1.092-.333-1.943-.372C10.443.01 10.172 0 7.998 0zm-.717 1.442h.718c2.136 0 2.389.007 3.232.046.78.035 1.204.166 1.486.275.373.145.64.319.92.599s.453.546.598.92c.11.281.24.705.275 1.485.039.843.047 1.096.047 3.231s-.008 2.389-.047 3.232c-.035.78-.166 1.203-.275 1.485a2.5 2.5 0 0 1-.599.919c-.28.28-.546.453-.92.598-.28.11-.704.24-1.485.276-.843.038-1.096.047-3.232.047s-2.39-.009-3.233-.047c-.78-.036-1.203-.166-1.485-.276a2.5 2.5 0 0 1-.92-.598 2.5 2.5 0 0 1-.6-.92c-.109-.281-.24-.705-.275-1.485-.038-.843-.046-1.096-.046-3.233s.008-2.388.046-3.231c.036-.78.166-1.204.276-1.486.145-.373.319-.64.599-.92s.546-.453.92-.598c.282-.11.705-.24 1.485-.276.738-.034 1.024-.044 2.515-.045zm4.988 1.328a.96.96 0 1 0 0 1.92.96.96 0 0 0 0-1.92m-4.27 1.122a4.109 4.109 0 1 0 0 8.217 4.109 4.109 0 0 0 0-8.217m0 1.441a2.667 2.667 0 1 1 0 5.334 2.667 2.667 0 0 1 0-5.334"/></svg>'
                ],
            ];
            
            $socialtables = Socialtable::first();
            return view('admin.settings', compact('socialtables', 'socialicons'));
        }

    In the above <svg ...></svg> is svg icon we are using to choose from the same will appear in the footer area. You can use predefined images. For SVG icons visit Bootstrap icons or Heroicons.

    The structure is pretty simple: One variable has an array and each of them has one or two variables stored in it. So it in short looks like this:

    $socialicons = [
         'facebook' => [
                    'icon' => '<svg...></svg>'
                       ],
    ];

    Laravel blade file to 5 loops of the same input box

    Now the Laravel blade file is named settings and it is inside the admin folder, so it looks like admin.settings.

    Now place this code that will show five main loops with a dropdown selection and input text box.

    @for ($count = 1; $count <= 5; $count++)
                    <div class="space-y-5 space-x-4">
                        <select name="social_icon_{{$count}}" id="" class="dark:bg-gray-700 rounded-md">
                            @foreach ($socialicons as $platform => $data)
                                <option value='{!! $data['icon'] !!}' class="text-gray-400" {{ $data['icon'] == $admin->{'social_icon_'.$count} ? 'selected' : '' }}>{{ ucfirst($platform) }}</option>
                            @endforeach
                        </select>
                        <input type="text" name="social_link_{{$count}}" id="" value="{{ $admin->{'social_link_' . $count} }}" class="dark:bg-gray-700 rounded-md">
                    </div>
                @endfor

    You see, we have used one select tag and one input tag for five text fields.

    In the above code, we have used a double exclamation mark !! with single curly brackets {!! $var !!}. This means to run the code stored in the variable. Because we are using svg icons in HTML format that need to be executed.

    Main parts in the above code

    • For loop to count and store numbers in $count.
    • Calling social_icon_{{$count}} and social_link_{{$count}} respectively.
    • Foreach loop to get the social icons from the controller we stored in an array $socialicons. Here we can call platforms for the site name we defined and data to call the icons.
    ]]>
    130
    Livewire Error Fix: “Cannot assign null to property $name” https://codeweb.wall-spot.com/livewire-error-fix-cannot-assign-null-to-property-name/ Wed, 03 Apr 2024 05:49:26 +0000 https://codeweb.wall-spot.com/?p=126 Resolving Null Value Assignment in Livewire Components

    Troubleshooting “Cannot assign null to property $name”

    What is actually the error?

    Laravel 10 with the breeze and livewire project. When customizing the user profile area with adding extra fields like a unique username and about the field.

    Cannot assign null to property Livewire\Volt\Component@anonymous::$name of type string

    An error appears for the update-profile-information-form.blade.php file, in the mount function.

    public function mount(): void
        {
            $this->name = Auth::user()->name;
            $this->email = Auth::user()->email;
        }

    Above the name field is empty but we are using it. How do we make it nullable?

    Solution

    You just need to make the default value to show if there is a null field in the database.

    public function mount(): void
        {
            $this->name = Auth::user()->name ?? '';
            $this->email = Auth::user()->email ?? '';
        }

    Or make it more cleaner:

    public function mount(): void
        { 
            $user = Auth::user();
            $this->name = $user->name ?? '';
            $this->email = $user->email ?? '';
        }
    ]]>
    126
    Seeding Inside Migration File https://codeweb.wall-spot.com/seeding-inside-migration-file/ Sat, 27 Jan 2024 10:03:58 +0000 https://codeweb.wall-spot.com/?p=121 In Laravel, you can seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you.

    Here we are talking about the initial data seeding along with the migration file. So you will get the first data just after migrating the database file.

    public function up()
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    
        DB::table('table_name')->insert([
            'name' => 'John Doe',
            'short' => 'Short intro goes here...',
            'description' => 'All the details goes here...',
        ]);
    }
    

    In the above example, the up the method creates a table named table_name with columns idname, and timestamps. After creating the table, it inserts three rows of data into the table using the insert method of the DB facade.

    ]]>
    121
    How To Show Checkbox Value Checked https://codeweb.wall-spot.com/how-to-show-checkbox-value-checked/ Sat, 20 Jan 2024 06:56:04 +0000 https://codeweb.wall-spot.com/?p=117 In this post, we will work on checkbox status while updating the post. This example is very similar to other if-else statements.

    Your table column will be false by default. It will store 0 in the database if not checked.

    $table->boolean('visible')->default(false);

    For the entry form it will be something like this:

    <input type="checkbox" name="visible" value="1">

    Show checkbox checked

    Now the main part here is to show checked when updating if there is a true value store that is 1 and not to show checked if there is a false value that is 0.

    Example 1

    Simple Laravel checkbox selected with the ternary operator

    <input type="checkbox" name="visible" value="1" {{ $page->visible ? 'checked' : '' }}>

    Laravel checkbox selected with the ternary operator and compare boolean values.

    <input type="checkbox" name="visible" value="1" {{ $page->visible==1 ? 'checked' : '' }}>

    Another way to check the same thing is with the true or false values we have used above for boolean values.

    <input type="checkbox" name="visible" value="1" {{ $page->visible==true ? 'checked' : '' }}>

    Example 2

    There are other ways to do the same. You can always use the if-else statement to show if something is true or false. If-else statement will be messy and why not use the better few lines to complete the long path?

    We can use the same statement within the input tag. Select the checkbox with the @if blade directives.

    <input type="checkbox" name="visible" value="1" @if ($page->visible) checked @endif>

    The thing with a shorter version by @checked blade directives.

    <input type="checkbox" name="visible" value="1" @checked ($page->visible)>
    ]]>
    117
    Dynamic Page Reordering with Laravel Livewire and Up/Down Arrows https://codeweb.wall-spot.com/dynamic-page-reordering-with-laravel-livewire-and-up-down-arrows/ Thu, 18 Jan 2024 06:19:36 +0000 https://codeweb.wall-spot.com/?p=114 Dynamic Page Reordering with Laravel Livewire and Up/Down Arrows.

    Enhance the visual appeal and organization of your website by incorporating a neatly arranged list/page/menu in the footer area. You have the option to display it in alphabetical order or based on unique identifiers. Strategically placing vital links at the top can captivate user attention, while the rest gracefully follow below. This practice is particularly beneficial when working on expansive projects, as it ensures that key links are showcased prominently and arranged in a meaningful order, contributing to a more engaging and user-friendly experience.

    Reordering the list using Laravel Livewire

    Here we are not using any Laravel packages for changing the position of the list. Just the arrows to move one step up or down to rearrange the position.

    If you like to use the packages get any of them:

    For this example, we are using the page model. So you have to add the position column in the page table. $table->unsignedInteger() because we do not want the negative numbers to be stored.

    /**
         * Run the migrations.
         */
        public function up(): void
        {
            Schema::create('pages', function (Blueprint $table) {
                $table->id();
                $table->unsignedInteger('position')->nullable();
                $table->string('title');
                $table->text('description');
    
                $table->timestamps();
            });
        }

    Make Livewire component: php artisan make:livewire page-list

    Implement the logic:

    // app/Http/Livewire/PageList.php
    
    class PageList extends Component
    {
        public $pages = [];
    
        public function mount()
        {
            $this->pages = Page::orderBy('position')->get();
        }
    
        public function moveUp($pageId)
        {
            $page = Page::find($pageId);
            $previousPage = Page::where('position', '<', $page->position)->orderBy('position', 'desc')->first();
    
            if ($previousPage) {
                $tempPosition = $page->position;
                $page->position = $previousPage->position;
                $previousPage->position = $tempPosition;
                $page->save();
                $previousPage->save();
                $this->pages = Page::orderBy('position')->get();
            }
        }
    
        public function moveDown($pageId)
        {
            $page = Page::find($pageId);
            $previousPage = Page::where('position', '>', $page->position)->orderBy('position', 'asc')->first();
    
            if ($previousPage) {
                $tempPosition = $page->position;
                $page->position = $previousPage->position;
                $previousPage->position = $tempPosition;
                $page->save();
                $previousPage->save();
                $this->pages = Page::orderBy('position')->get();
            }
        }
    
        public function render()
        {
            return view('livewire.page-list');
        }
    }
    

    Create the blade view:

    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Position</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($pages as $page)
                <tr>
                    <td>{{ $page->title }}</td>
                    <td>{{ $page->position }}</td>
                    <td>
                        @if ($page->position > 1)
                          <button wire:click="moveUp({{ $page->id }})">↑</button>
                        @endif
                        @if ($page->position < $pages->max('position'))
                          <button wire:click="moveDown({{ $page->id }})">↓</button>
                        @endif
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>

    Include the component in your view:

    // In your main view or layout
    
    <div>
        @livewire('page-list')
    </div>
    
    ]]>
    114
    Creating Search Function https://codeweb.wall-spot.com/creating-search-function/ Wed, 20 Dec 2023 15:27:26 +0000 https://codeweb.wall-spot.com/?p=109 This is how you can use the search function used in the Laravel project.

    Search the post and show all the results that match its title or description.

    public function search(Request $request)
        {
            $this->validate($request, [
                'q' => 'required',
            ]);
            $posts = Post::where('title', 'like', '%' . $request->q . '%')->orWhere('description', 'like', '%' . $request->q . '%')->paginate(20);
        
            return view('pages.search', compact('posts'))->with('i', (request()->input('page', 1) - 1) * 20);
        }
    ]]>
    109
    How To Delete Old File While Uploading New https://codeweb.wall-spot.com/how-to-delete-old-file-while-uploading-new/ Fri, 15 Dec 2023 15:28:38 +0000 https://codeweb.wall-spot.com/?p=105 How to delete older file/image when you update the post?

    To make the folder clean and remove all unused file/image from the folder, delete old files.

    A Step-by-Step Guide to Updating Posts and Cleaning Up Images in Laravel

    Learn How to Update Your Posts and Delete Unused Images in Laravel.

    As your website grows and you create more and more blog posts, you might face some challenges in managing your content and images.

    For example, you might want to update your old posts with new information or images, but you don’t want to leave behind the old images that are no longer relevant or used. These unused images can take up valuable space in your server and affect your website performance and loading speed.

    Solution: Laravel provides us with a great solution to keep our blog folder clean and gives us the control to manage all the unnecessary files. When we update our old posts, we can check and delete the old files if we upload new ones.

    So, we will create a function where we will check if the post has an image already. Then the next actions will depend on it.

    • Check if the post has an image
    • If yes, then delete and upload a new one (if the user wants)
    • If no, then upload a new image (if the user wants)
    • The field could be left empty as per the user’s requirement

    Check if the file exists

    Let’s see the code to check if there is already a file available in the folder.

            if ($request->hasFile('image')) {
                $oldfile = public_path('images/post_img/') . $post->image;
                $filename = 'image' . '_' .time() . '.' . $request->file('image')->getClientOriginalExtension();
                if(File::exists($oldfile)){
                    File::delete($oldfile);
                }
    

    We store the file path and the file name in a variable called $oldfile. This will make it easy to use the long file path if we need it for heavy actions.

    Basically what the above code means: We check if the user is uploading a file. If yes, then we name it and then check if there is already a file exists. Here again, if the condition is yes, we will delete it and then update it with $post->update();

    • If the request has a file, change its name, and store it in a variable called $filename
    • If there is already a file (old file), remove it if(File::exists($oldfile)){File::delete($olderfile);}

    Function for deleting old image and updating with new image

    use File;
    
    public function update(Request $request, Post $post)
        {
            $validatedData = $this->validate($request, [
                'title' => 'required',
                'category_id' => 'required',
                'description' => 'required',
                'image' => 'sometimes|mimes:jpeg,jpg,gif,png'
            ]);
    
            if ($request->hasFile('image')) {
                $oldfile = public_path('images/post_img/') . $post->image;
                $filename = 'image' . '_' .time() . '.' . $request->file('image')->getClientOriginalExtension();
                if(File::exists($oldfile)){
                    File::delete($oldfile);
                }
    
                $request->file('image')->storeAs('post_img', $filename, 'public');
    
                $validatedData['image'] = $filename;
            }
    
    
        
            $post->update($validatedData);
    
            return redirect()->back()->withMessage('Your updated post is ready now!');
        }
    ]]>
    105
    Email Validation And Message In Laravel https://codeweb.wall-spot.com/email-validation-and-message-in-laravel/ Mon, 04 Sep 2023 06:47:26 +0000 https://codeweb.wall-spot.com/?p=102 //email validation and message //controller file public function store(Request $request) { $this->validation($request, ['email'=>'required|email']); } //Now on view page @if($error->any()) @foreach($error->all() as $error) <li>{{ $error }}</li> @endforeach @endif ]]> 102 Laravel Route To Open Pages https://codeweb.wall-spot.com/laravel-route-to-open-pages/ Mon, 04 Sep 2023 06:39:54 +0000 https://codeweb.wall-spot.com/?p=100 Laravel function to open pages is simple. A demo function you will find on web.php for welcome page.

    Simply you can copy paste the same to open any other pages.

            //Opens welcome blade page
    	Route::get('/', function () {
    	return view('welcome');
    	});
    	
    	//Opens pages/about page
    	Route::get('/about', function(){
    		return view('pages.about');
    	});
    	
    	//Opens custom pages
    	Route::get('/users/{id}', function($id){
    		return 'This is user '.$id;
    	});
    ]]>
    100