“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

background

  1. The project was developed with Thinkphp5.1, but it was difficult to deal with the query Tp of various relational models.
  2. As hard-running programmers, we decided to develop Lavarel for our next project.
  3. Since nuggets are rewarded, take a look at some of gitee’s private study notes.

instructions

Laravel is a medium – and large-sized framework, with all kinds of functions built in, and the official Laravel document is confusing to me, which is not very friendly to beginners. So this series of articles is for PHP basics only. Want a quick start in development.

Prior to the start

Because all the problems have been sorted out in detail, there is only the function of managing the background to view the list of blogs and audit blogs, and it is very similar to the function before. So let’s just quickly sort this out.

start

  1. Registered routing

Route::resource(‘blog’, BlogController::class);

  1. Controller Creation

      

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Service\BlogService;

class BlogController extends Controller
{
    / / instantiate
    public function __construct()
    {
        parent::__construct();

        $this->BlogService = new BlogService();
    }
    // List data
    public function index()
    {
        $res = $this->BlogService->getList();

        return $this->success($res);
    }

    // Audit function
    public function update($id, Request $request)
    {
        $option = $request->input('option');
        if (in_array($option['approve']) {switch ($option) {
                case 'approve':
                    $status = $request->input('status');
                    if ($status= =1) {
                        $res = $this->BlogService->edit($id['status'= >$status]);

                        return $this->success($res);
                    }
                    return $this->fail('Modification failed');
                    break; }}return $this->fail(); }}Copy the code

The key analysis

  1. List data: Because we had extracted the Service layer earlier, and the method was very granular, it was very lively. So the index method can pull the list data directly.
  2. Audit function: According to the previous analysis, audit can be reduced to a large category of Update data, so we request Update method, just add the operation identifier to refine the specific operation