PHP筆記

前言因緣際會下還是開始學習php了。經歷了風風雨雨終於在今年暑假要去加拿大留學了,php會是第二年的其中一門必修課程,加上最近前端也真的蠻心累,也許有一門精進的後端語言,日後轉職會有更寬廣的道路,對自己說加油!

一、註冊 API

首先創建auth 的controlller

php artisan make:controller API/AuthController

在laravel 中 使用Eloquent ORM 來控制資料庫

代碼如下:

java 复制代码
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends BaseController
{
    public function register(Request $requst)
    {
          $user = User::create([
              'username' => $requst->input('username'),
              'email' => $requst->input('email'),
              'password' => Hash::make($requst->input('password')),
          ]);
          $token = $user->createToken('user_token');
          return $this->response([
              'user' => $user,
              'token' => $token
          ]);
        }
    }

}

現在來一步步拆解上方的代碼

首先引入model User (laravel 預設Models 資料夾下有User.php) 使用該model的方法創建一個新的資
User::create

二、使用步骤

1.引入库

代码如下(示例):

php 复制代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context

2.读入数据

代码如下(示例):

php 复制代码
data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

该处使用的url网络请求的数据。

補充:

Eloquent ORM的基本使用方法

notice: PK. = primary key

// 拿User這個model舉例

// 查詢PK = 1的資料

User::find(1);

// 查詢多筆資料 By PK欄位

User::find([1, 2, 3]);

// Where條件式,第一個參數為欄位名稱,第二個參數為值

User::where('name', 'Yee');

// Where也可以這樣用

User::where('age', '>', '18');

// Where還能這樣用 ((我最愛這方法XD
q u e r y = [ [ ′ n a m e ′ , ′ = ′ , ′ Y e e ′ ] , [ ′ a g e ′ , ′ > ′ , ′ 1 8 ′ ] ] U s e r : : w h e r e ( query = [['name', '=', 'Yee'], ['age', '>', '18']] User::where( query=[[′name′,′=′,′Yee′],[′age′,′>′,′18′]]User::where(query);

// 當然也有where NULL

User::whereNull('name')->get();

User::whereNotNull('name')->get();

// Insert 的方法一

$user = new User;

$user->name = 'Yee';

...

$user->save();

// Insert 的方法二

$attributes = [['name'=>'Yee', 'email'=>'yee@gmail.com']];
u s e r = U s e r : : c r e a t e ( user = User::create( user=User::create(attributes);

// 兩個新增的方法都會讓 $user 擁有剛剛新增得值

// Update 的方法一

$user = User::find(1);

$user->name = 'Yee2';

$user->save();

// Update 的方法二

// where條件請參考上面的方法
u s e r = U s e r : : w h e r e ( user = User::where( user=User::where(query)->update($attributes);

// Delete 的方法,

User::find(1)->delete();

相关推荐
并不喜欢吃鱼21 小时前
从零开始C++----四.vector的使用与底层实现
开发语言·c++
anzhxu21 小时前
防火墙安全策略(基本配置)
服务器·php·apache
沐雪轻挽萤21 小时前
17. C++17新特性-并行算法 (Parallel Algorithms)
java·开发语言·c++
墨澜逸客21 小时前
华胥祭坛志---文/墨澜逸客
开发语言·深度学习·学习·百度·php·学习方法·新浪微博
覆东流21 小时前
第3天:Python print深入与格式化输出
开发语言·后端·python
加号31 天前
C# 基于MD5实现密码加密功能,附源码
开发语言·c#·密码加密
耿雨飞1 天前
Python 后端开发技术博客专栏 | 第 05 篇 Python 数据模型与标准库精选 -- 写出 Pythonic 的代码
开发语言·python
执笔画流年呀1 天前
计算机是如何⼯作的
linux·开发语言·python
weixin_520649871 天前
C#闭包知识点详解
开发语言·c#
东北甜妹1 天前
Redis Cluster 操作命令
java·开发语言