欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Laravel基礎(chǔ)知識有哪些

這篇文章主要介紹“Laravel基礎(chǔ)知識有哪些”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Laravel基礎(chǔ)知識有哪些”文章能幫助大家解決問題。

站在用戶的角度思考問題,與客戶深入溝通,找到上蔡網(wǎng)站設(shè)計與上蔡網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋上蔡地區(qū)。

一、安裝laravle

1、安裝composer

2、執(zhí)行命令:

composer create-project laravel/laravel 項目文件夾名 --prefer-dist

二、目錄簡介

  • app:應(yīng)用程序的核心代碼

  • bootstrap:一個引導(dǎo)框架的app.php文件,一個cache目錄(包含路由及緩存文件),框架啟動文件,一般情況不動。

  • config:所有配置文件

  • database:其中migrations目錄可以生成數(shù)據(jù)表。

  • public:入口文件存放地,以及靜態(tài)資源(和tp類似)

  • resources

  • routes:應(yīng)用的所有路由定義

  • tests:可用來單元測試

  • vendor:所有composer依賴包

三、路由初識

1、常見的幾種請求

  • Route::get(                             u                         r                         l                         ,                            url,                url,callback);

  • Route::post(                             u                         r                         l                         ,                            url,                url,callback);

  • Route::put(                             u                         r                         l                         ,                            url,                url,callback);

  • Route::delete(                             u                         r                         l                         ,                            url,                url,callback);

2、匹配指定的請求方式

Route::match(['get','post'],'/',function(){});

3、配置任意請求方式

Route::any('/home', function () {
    });

4、給路由加必填參數(shù)

Route::get('/home/{id}', function ($id) {
    echo 'id為:'.$id;});

5、給路由增加可選參數(shù)

Route::get('/home/{id?}', function ($id = '') {
    echo 'id為:'.$id;});

6、通過?形式傳遞get參數(shù)

Route::get('/home', function () {
    echo 'id為:'.$_GET['id'];});

7、給路由增加別名

Route::any('/home/index', function () {
    echo '測試';})->name('hh');

8、設(shè)置路由群組

例如有如下路由:

  • /admin/login

  • /admin/index

  • /admin/logout

  • /admin/add

如果一個一個添加是比較麻煩的,他們有一個共同的區(qū)別,都是有/admin/前綴,可設(shè)置一個路由群組進行添加:

Route::group(['prefix'=>'admin'], function () {
    Route::get('test1', function () {
        echo 'test1';
    });
    Route::get('test2', function () {
        echo 'test2';
    });});

此時就可通過/admin/test1來進行訪問了。

9、路由配置控制器

控制器可以建一個前臺和一個后臺:

命令行創(chuàng)建路由:

php artisan make:controller Admin/IndexController

基本路由建立:

Route::get('test/index','TestController@index');

分目錄路由建立:

Route::get('/admin/index/index','Admin\IndexController@index');

四、laravel驗證器

引入:use Illuminate\Support\Facades\Validator

$param = $request->all();$rule = [
    'name'=>'required|max:2',];$message = [
    'required'  => ':attribute不能為空',
    'max' => ':attribute長度最大為2'];$replace = [
    'name' => '姓名',];$validator = Validator::make($param, $rule, $message,$replace);if ($validator->fails()){
    return response()->json(['status'=>0,'msg'=>$validator->errors()->first()]);}

五、控制器獲取用戶輸入的值

在控制器中如果要使用一個類,例如use Illuminate\Http\Request,就可以簡寫為use Request
但是需要在config目錄下的app.php配置文件中加入:

'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Arr' => Illuminate\Support\Arr::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,

        'Request' => Illuminate\Support\Facades\Request::class,

    ],

1、獲取用戶單個輸入值:

Input::get('id')

2、獲取用戶輸入的所有值:

Input::all()

打印出來的是數(shù)組

關(guān)于dd(dump+die)

3、獲取用戶輸入指定的值:

Input::only(['id','name']  //只接收id,其余不接受

4、獲取用戶輸入指定值之外的值:

Input::except(['name']    //不接收name,其余都接收

5、判斷某個值是否存在

Input::has('name')    //存在返回true  不存在返回false  其中0返回true

六、視圖的創(chuàng)建與使用

1、視圖的創(chuàng)建

視圖也可分目錄管理:


控制器語法:

return view('home/test');

也可寫為:

return view('home.test');

2、變量映射

控制器中:

return view('home/test',['day'=>time()]);

視圖中:

{{$day}}

其中控制器中變量映射有三種:

  • view(模板文件,數(shù)組)

  • view(模板文件)->with(數(shù)組)

  • view(模板文件)->with(數(shù)組)->with(數(shù)組)

了解一下compact數(shù)組。

3、視圖渲染

3.1 foreach的使用

控制器中:

public function index(){

        $arr = [
            0 => [
                'name' => 'tom',
                'age' => '12',
            ],
            1 => [
                'name' => 'bby',
                'age' => '13',
            ]
        ];
        return view('home/test',['data'=>$arr]);
    }

視圖中:

@foreach($data as $k=>$v)
    鍵:{{$k}}
    值:{{$v['name']}}    <br/>@endforeach

3.2 if的使用

@if(1==2)
    是的
@else
    不是的
@endif

4、視圖之間的引用

@include('welcome')

七、模型的創(chuàng)建與使用

1、創(chuàng)建模型的命令

php artisan make:model Model/Admin/Member

此時,就會在app目錄內(nèi)創(chuàng)建:

2、模型基本設(shè)置

<?phpnamespace App\Model\Admin;use Illuminate\Database\Eloquent\Model;class Member extends Model{
    //定義表名
    protected $table = 'student';
    //定義主鍵
    protected $primaryKey = 'id';
    //定義禁止操作時間
    public $timestamps = false;
    //設(shè)置允許寫入的字段
    protected $fillable = ['id','sname'];}

3、模型數(shù)據(jù)添加

方式一:

	 $model = new Member();
	 $model->sname = '勒布朗';
	 $res = $model->save();
	 dd($res);

方式二:

     $model = new Member();
     $res = $model->create($request->all());
     dd($res);

4、模型的表連接

//查詢客戶與銷售顧問的客資列表$data = Custinfo::select(['custinfo.*', 'customers.name'])
    ->join('customers', 'customers.id', '=', 'custinfo.cust_id')
    ->where($where)
    ->get()
    ->toArray();

5、簡單模型關(guān)聯(lián)一對一

<?phpnamespace App\Model\Admin;use Illuminate\Database\Eloquent\Model;class Phone extends Model{
    //定義表名
    protected $table = 'phone';

    //定義主鍵
    protected $primaryKey = 'id';

    //定義禁止操作時間
    public $timestamps = false;

    //設(shè)置允許寫入的字段
    protected $fillable = ['id','uid','phone'];}
<?phpnamespace App\Model\Admin;use Illuminate\Database\Eloquent\Model;class Member extends Model{
    //定義表名
    protected $table = 'student';

    //定義主鍵
    protected $primaryKey = 'id';

    //定義禁止操作時間
    public $timestamps = false;

    //設(shè)置允許寫入的字段
    protected $fillable = ['id','sname'];

    /**
     * 獲取與用戶關(guān)聯(lián)的電話號碼記錄。
     */
    public function getPhone()
    {
        return $this->hasOne('App\Model\Admin\Phone', 'uid', 'id');
    }}
    //對象轉(zhuǎn)數(shù)組
    public function Arr($obj)
    {
        return json_decode(json_encode($obj), true);
    }


    public function index(){
        $infoObj = Member::with('getPhone')->get();
        $infoArr = $this->Arr($infoObj);
        print_r($infoArr);
    }

八、日志

1、自定義日志目錄

config目錄下的logging.php中的channels配置:

 'custom' => [
     'driver' => 'single',
     'path' => storage_path('logs/1laravel.log'),
     'level' => 'debug',
 ]

控制器中:

$message = ['joytom','rocker'];Log::channel('custom')->info($message);

九、遷移文件

建立一個遷移文件:php artisan make:migration create_shcool_table

會在database\migrations下創(chuàng)建一個文件:
Laravel基礎(chǔ)知識有哪些
在up方法中增加如下代碼:

<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateShcoolTable extends Migration{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('shcool', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('school_name','20')->notNull()->unique();

            $table->tinyInteger('status')->default(1);

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('shcool');
    }}

寫好SQL文件以后,執(zhí)行:php artisan migrate
將會生成數(shù)據(jù)表,其中操作日志將記錄在這個表中:

php artisan migrate:rollback:回滾最后一次的遷移操作, 刪除(回滾)之后會刪除遷移記錄,并且數(shù)據(jù)表也會刪除,但是遷移文件依舊存在,方便后期繼續(xù)遷移(創(chuàng)建數(shù)據(jù)表)。

關(guān)于“Laravel基礎(chǔ)知識有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

網(wǎng)站欄目:Laravel基礎(chǔ)知識有哪些
瀏覽路徑:http://chinadenli.net/article38/gshepp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化定制網(wǎng)站品牌網(wǎng)站建設(shè)微信小程序微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)