by Devin Yang
(This article was automatically translated.)

Published - 4 years ago ( Updated - 4 years ago )

Laravel 8 has undergone a major revision, and my website has also been revised accordingly. The "notes" and "articles" on my website were originally separated into two blocks.
This time I integrated him together. Anyway, there are not many articles, and the combined content seems to be more. :p

My definition of an article is that the content is more original.
The notes are recorded casually for future reference. The notes may be original or copied from the Internet.

This upgrade will be carried out in two days. The first day is to step on landmines to upgrade the background, from 5.7.28 to 8.8.0,
Since Laravel is full of clear errors, it is not difficult to modify. Only when changing the Routes in the background, read it while modifying.
People who have upgraded like me should understand what I'm talking about.
use App\Http\Controllers\UserController;

Route::get('/user', [UserController::class, 'index']);

Fortunately, my Vim marco has reached the point of proficiency, and it will be done in about three minutes. Press the blank to automatically rewrite,
I can't help but admire Vim's macro function again, which is really easy to use.

On another day, a major revision of the front-end layout was carried out, but everything was kept simple.

This article is just chatting, sharing how I changed it, not teaching how to upgrade Laravel.

How to upgrade Laravel, you can see my old article.
https://www.ccc.tc/article/how-to-update -ow-backend

This upgrade, because it is a large version, so my strategy is to install a new one directly, and then import my background into composer.json.

But in fact, you can try to adjust the version number in composer.json to upgrade. You can take a look at Laravel's Upgrade Guide.
https://laravel.com/docs/8.x/upgrade

Speaking of the Laravel version number, just a while ago a friend asked me how to get the Laravel version number, I will post it here by the way..
$laravel_version=app()::VERSION;
The following ow_source is my background, and it can be used by loading it through psr4.
 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "Ow\\Model\\": "app/ow_source/seo-model/",
            "Ow\\": "app/ow_source/seo-class/"
        }
    },
Of course, the RouteServiceProvider needs to add my own background-specific routing
public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                -> middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::middleware('owadmin')
                ->namespace($this->namespace)
                ->group(base_path('app/ow_source/ow_routes.php'));

            Route::middleware('owapi')
                ->namespace($this->namespace)
                ->group(base_path('app/ow_source/ow_routes.php'));
        });
}

The above is far away, back to this article, the front-end code part, here I changed the block number (b_ids) to array,
Originally only displaying a single content can now directly display the content of multiple blocks. It is very easy to change Laravel somehow.
public function articles($bymonth){
$b_ids=[1,2]; //Block of the default home page
$queryStr = "";
if(!preg_match('/\d{4}-(\d?\d$)/u', $bymonth, $matches)){
return abort(404);
};
$bymonth.="-01"; //Add number 1
$queryStr.=sprintf(" AND c_dt BETWEEN '%s' AND DATE_ADD('%s', INTERVAL 1 MONTH)", $bymonth,$bymonth);
$contents = Content::ofBlock($b_ids)
-> whereRaw('1 = 1'.$queryStr)
->orderBy('c_id','desc')->get();
$archives = $this->archives($b_ids); //archive data
//return $archives;
return view("$this->frontend.pages.index",[
'contents'=>$contents,
'archives' => $archives
]);
}

Originally used where, but changed to whereIn, so it can be used @@, as you can see in the Model below, I changed it to whereIn.

In addition, in the content Model below, why do I need to customize the table and primaryKey? It’s definitely not that I’m too busy or looks more powerful.
It's because my backend existed before Laravel, so all the data table fields have already been defined,
This is the case of customizing $table and $primaryKey. Otherwise, basically, I will use Laravel's rules to call IDs, and then Table is plural.
 
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
class Content extends Model
{
protected $table='content';
protected $primaryKey='c_id';

public function scopeOfBlock($query, $b_ids)
{
    return $query->whereIn('b_id', $b_ids)->where('is_publish',1);
}


Another thing I probably learned from this revision is that the information I spit out is the same, so I shouldn’t change the version and even the Controller, right?
So, I pulled out the template folder and changed the dynamics. If I want to adjust different templates in the future, just change the frontend. (Start planning to build a multi-version front desk)
return view("$this->frontend.pages.index",[
    'contents'=>$contents,
    'archives' => $archives
]);

Here is my modified diff

After simplifying everything, it seems that the speed of the website has improved significantly, with a PageSpeed ​​Insights score of 98.


 

Tags: laravel

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


laravel

Laravel thought organization, from Model to DatabaseSeeder.

This article will introduce the relevant instruction use cases step by step: Model=>Factory=>Seeder=>DatabaseSeeder Step by step, let us examine all the processes from the Model to the DatabaseSeeder.

wordpress, d-laravel, docker, docker-compose

How to install Wordpress with D-Laravel

An 8-minute teaching video, introducing the installation of Wordpress on D-Laravel. D-Laravel is a docker-compose Laravel framework development environment, but it does not mean that it cannot be used to install other things. This video introduces the use of D-Laravel to install non-Laravel PHP projects, You can learn the usage of related instructions on D-Laravel: ./create --host usage, docker-compose up -d and ./console mysql usage, The database connection setting method after D-Laravel v1.0.0.

Laravel linux

My mobile computer, install Laravel 8 on Sony xperia 5 II

This is my first Android phone. It's no problem to try to install Laravel. Basically, I installed vim and mariadb. After executing sshd, you can log in through 8022, it is completely a mobile computer: p. The software I use is Termux...