by Devin Yang
(This article was automatically translated.)

Published - 5 years ago ( Updated - 5 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,docker

How to customize Laravel pagination

Recently, I have been free, and I want to adjust the arrows on the upper and lower pages of the website. If you don’t know how to customize Laravel’s pagination, You can take a look at a short three-minute introduction on how I customize Laravel's pagination.

laravel

Customize your own helper in laravel

In the Laravel framework, a considerable number of PHP functions (php functions), called helpers, are included. https://laravel.com/docs/5.6/helpers So how do we customize our own helper in Laravel? It's actually quite simple.. Just add files in autoload in composer.json.

dlaravel

Use docker in docker to build a D-Laravel test environment.

D-Laravel is an extremely easy-to-use and extremely flexible Laravel development environment. As long as you are a Mac user, even if you don’t know Docker, you can use it to create Laravel projects and develop them. Due to the newly added .env function When it comes to functions, those who are in a hurry push, but there is no complete test, and a bunch of new bugs are created. Therefore, this time, a new dlaravel_test, a bash testing tool, is added to run the test through docker in docker. Make sure that every release of D-Laravel can be a stable version.