by Devin Yang
(This article was automatically translated.)

Published - 3 years ago ( Updated - 3 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

Facebook test user

Is there anyone who uses OAuth for website user login like me? There is a "test user" on the Facebook developer page. It can be used to test whether the function of Facebook is normal, Because when the website moved to Google’s GCE, for some reason, I actually posted one more in the $fillable array of Laravel’s User model~, Normally the program works fine, but when a new user logs in, an error is shown to you. I keep hearing people say that I can't log in to this website to leave a message. I want to say that I am very normal. @@ After using the test user test today, ha, I found that the login function on my website has been broken for a long time.

d-laravel, docker, laravel, docker-compose

D-Laravel v1.0.0 release change description

In order to allow the container to be used more flexibly, D-Laravel has released version v1.0.0, which is not backward compatible. https://github.com/DevinY/dlaravel/releases/tag/v1.0.0 If you are using before v1.0.0, you need to modify the .env file of the Laravel project, change DB_HOST=127.0.0.1 to DB_HOST=db If you have a custom docker-compose-custom.yml file....more

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.