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 teaching, blade

Tags on my Laravel blade

Using Laravel's Blade Templates is quite easy to use, and I'm used to it after a long time, but one thing is that when a lot of blades are created, it seems that it is not easy to find myself. Here is my latest original method. If it is the same Pure coincidence.

laravel,woops

Laravel 5.5 Woops is back, let's check it out.

Woops is a framework for PHP error handling. It was preloaded in Laravel 4, but it was removed in Laravel 5.0, and now 5.5 is back.... Some features of Whoops include: - Flexible, stack-based error handling - No dependencies are required to use the current stand-alone library - Simple API for handling exceptions, tracking frames and their data -Include a stunning error page within your web application - Includes the ability to open referenced files directly in editors and IDEs - Includes handlers for different response formats (JSON, XML, SOAP) -Easy to extend and integrate with existing libraries - Clean, well structured and proven generation

laravel,blade

Laravel 5.5 make:model's -a parameter and Blade version chunk function introduction

Do you not know what the make:model -a parameter is? This video will briefly introduce the actual operation process. And use the chunk function in the Blade set to end.