Wednesday, November 25, 2020

Laravel

https://osol-tutorials.blogspot.com/2020/11/laravel.html

URL:

http://localhost/pjt/laravel/php7/example/server.php

or

http://localhost/pjt/laravel/php7/example/public


path: D:\projects\laravel\php7_with5.4\example


http://sreelp/pjt/MyContributions/osolcontributions/quickfileexlorer/


Good tutorial at :https://www.webtrickshome.com/category/laravel

check info about various paths there


for laravel 7 

https://laravel.com/docs/7.x/installation

https://www.webhostface.com/kb/knowledgebase/install-laravel-windows/


COMPOSER COMMANDLINE ARGUMENTS:https://getcomposer.org/doc/03-cli.md


        composer create-project --prefer-dist laravel/laravel example


             --ignore-platform-reqs

             if only old version is installed

        php key:generate      (do it if it is not run as last step in laravel installation)

            

            

            php artisan key:generate

            Application key [base64:ATRDLHorCFpqaLr83arYBau718w6zXv/m0UU1e1Jwno=] set successfully.

            (REF: https://laravel.com/docs/4.2)


        Get Version:

            cd /D D:\projects\laravel\php7\example

            php artisan --version

                    Laravel Framework 5.4.36


    When using multiple versions of php , care full , check before you install

       ECHO %PATH%

            C:\wamp64\bin\php\php5.6.31;


       or php -v

            PHP 7.4.9 (cli) (built: Aug  4 2020 11:52:41) ( ZTS Visual C++ 2017 x64 )


            This PC >>right click -> properties >> Advanced system settings >> Environment variables


            Under "system variables" select "path" click edit

                change

                    C:\wamp64\bin\php\php5.6.31

                    TO

                    C:\wamp64_4_php7\bin\php\php7.4.9



            after that if you install

                Installing laravel/laravel (v8.4.3)



front end

        edit APP_URL=http://localhost in .env

        files are in public folder

        server.php in root folder may used as starting point

        copy .htaccess in pubic_html folder to root folder 




        create folder named "frontend" in public folder, create css, js, images in it

            save index.html in  resources/views  as frontend/index.blade.php

            in routes/web.php      change return view('welcome');      to 'frontend.index'


            change href, src and action to {{url('public_html/frontend/,,,')}} eg:<a href="{{ url('/login') }}">


            in forms , add 2 hidden input fields 

                1. _token value="{{csrf_token()}}     (cross site request forgery)

                2. tbl value= "{{encrypt('tbl_name')}}


                these fields are checked in controller c



backend

        edit .env and set the following appropriately after creating a database

        DB_DATABASE=homestead

        DB_USERNAME=homestead

        DB_PASSWORD=secret


        in command line


                Model

                    php artisan make:model table_name


                        creates  2 classes 

                                class in app/Providers/table_name.php, set table name and primary key in it

                                class in database\migrations folder check sample 2014_10_12_000000_create_users_table.php

                            add table create commands in 'up' method


                        run in command line

                    php artisan migrate

                            will create table in database as we set in 'up' method supra


                Controller

                    php artisan make:controller crudController.php

                        creates crudController.php in app/Http/Controllers/Auth folder


                    copy index.php to resources/views as backend/index.blade.php

                        change href, src and action supra, in forms add 2 hidden field supra

                        

                            REF:https://www.webtrickshome.com/laravel/laravel-blades

                        start with 

                        @extends('backend.app') 


                        @section('content')

                            html contents here

                        @end

                     in routes/web.php  add


                        Route::get('adminpanel', function () {

                            return view('backend.index');

                        });


                        or if controller is to be used

                        Route::get('addpost', 'ControllerName@editpost');

                        or

                        Route::post('editpost/{id}', 'ControllerName@editpost');


                        with a corresponding method in the controller

                            public function editpost($id)

                            {

                                $data = DB::table('tbl_posts')->where('pid',"=",$id)->first();

                                $cats = DB::table('tbl_cats')->get();

                                return view('backend.index',array("data" =>$data,"cats"=>$cats)); 

                            } 

                            

                            in that


                            in view file access data as

                            @foreach($cats as $cat)

                             <option  value="{{$cat->field_name}}"

                            @endforeach



                    for trimming

                        {!! str_limit($data->details,50) !!}

                    for pagination 

                        $cats = DB::table('tbl_cats')->get()->paginate(10);


                        access pagination links as

                            {{$cats->links()}}

                    for date 

                        {{date('D,m-d-Y')}}



                    

            CSS Framework

                Laravel also comes pre-configured for Bootstrap 3 and 4 - Which you can opt-out of course. 

                But you will need to redesign some defined layouts, such as the Pagination for example, 

                which comes with classes for Bootstrap.                    


                To do that you can do:


                php artisan vendor:publish --tag=laravel-pagination


                This command will create all the views for the pagination in your views folder.


                You can also see this and more about the pagination if you visit: https://laravel.com/docs/5.6/pagination 




         Misc

            https://www.webtrickshome.com/laravel/laravel-functioning


            https://dev.to/kingsconsult/laravel-8-auth-registration-and-login-32jl

            better one

            https://therichpost.com/laravel-8-login-registration-working-tutorial/

            composer require laravel/ui

                    ......

                    Package manifest generated successfully.

                    72 packages you are using are looking for funding.

                    Use the `composer fund` command to find out more!

            php artisan ui vue --auth

                Vue scaffolding installed successfully.

                Please run "npm install && npm run dev" to compile your fresh scaffolding.

                Authentication scaffolding generated successfully.


            npm install


            npm run dev


            php artisan serve



                    D:\projects\laravel\php7_with5.4\example>php artisan serve

                    Laravel development server started: <http://127.0.0.1:8000>

                    [Fri Nov 27 13:13:37 2020] PHP 7.4.9 Development Server (http://127.0.0.1:8000) started

                

                with diiferent port 

                php artisan serve --port=8082


                You can stop Laravel Development server anytime by pressing ctrl+c key from keyboard.


                check the working on browser



Errors

php artisan migrate


    In D:\projects\laravel\php7_with5.4\example\vendor\laravel\framework\src\Illuminate\Database\Connection.php

        Access denied for user 'homestead'@'localhost' (using password: YES) (SQL: select * from inf

        ormation_schema.tables where table_schema = homestead and table_name = migrations)




        

        In D:\projects\laravel\php7_with5.4\example\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php line 68:


            SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)


                   

            

   SOLUTION:         

            simplest possible step is to close command line window ,open another one and excute this


                   

            IF IT DOESNT WORK   

                check .env and set appropriately


                DB_CONNECTION=mysql

                DB_HOST=localhost

                DB_PORT=3306

                DB_DATABASE=laravel_auth_test

                DB_USERNAME=root

                DB_PASSWORD=''

                   

                    Well it uses config\database.php but it references env() which is explained here: http://laravel.com/docs/5.1/installation#environment-configuration


                    You need to run these two commands

                            

                            php artisan config:clear

                            php artisan cache:clear

                            php artisan config:cache

                            php artisan view:clear



same command 

another error

php artisan migrate

Migration table created successfully.

Migrating: 2014_10_12_000000_create_users_table


   Illuminate\Database\QueryException


  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))


  at D:\projects\laravel\php7\example\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671

    667▕         // If an exception occurs when attempting to run a query, we'll format the error

    668▕         // message to include the bindings with SQL, which will make this exception a

    669▕         // lot more helpful to the developer instead of just the database's errors.

    670▕         catch (Exception $e) {

  ➜ 671▕             throw new QueryException(

    672▕                 $query, $this->prepareBindings($bindings), $e

    673▕             );

    674▕         }

    675▕


  1   D:\projects\laravel\php7\example\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464

      PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes")


  2   D:\projects\laravel\php7\example\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464

      PDOStatement::execute()




SOLUTION :     

         ADD BELOW CODE INSIDE YOUR APP/PROVIDERS/APPSERVICEPROVIDER.PHP FILE AND THIS CODE WILL RESOLVE YOUR ERROR WHICH WILL COME ON MIGRATE TIME:

         drop tables created also


         ...

            use Illuminate\Support\Facades\Schema;

            ...

            class AppServiceProvider extends ServiceProvider

            {

                

                ...

                public function boot()

                {

                    ...

                    Schema::defaultStringLength(191);

                }

            }



404 error for http://localhost/pjt/laravel/php7/example/public/login            


        add bellow line in .htaccess

    RewriteBase /pjt/laravel/php7/example/public/