批改狀態(tài):合格
老師批語:完全的不錯 , 圖文直觀
1、通過artisan和手動創(chuàng)建控制器,并通過設(shè)置路由訪問
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class Home extends Controller { public function index(){ echo '我是控制器,路由實現(xiàn)方法'; } } ?>
點擊 "運行實例" 按鈕查看在線實例
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('/home', function () { return ('home'); }); Route::get('/home','home@index'); ?>
點擊 "運行實例" 按鈕查看在線實例
2、通過artisan和手動創(chuàng)建模型,并通過配置數(shù)據(jù)庫實現(xiàn)從表中獲取數(shù)據(jù)
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $table = 'user'; protected $primaryKey = 'uid'; // 獲取表中所有記錄 public function abc(){ return $this->get()->toarray(); } } ?>
點擊 "運行實例" 按鈕查看在線實例
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Request; class Home extends Controller { public function index(User $user){ echo '<pre>'; $res = $user->abc(); foreach($res as $k => $val){ $res[$k] = (array)$val; } print_r($res); } } ?>
點擊 "運行實例" 按鈕查看在線實例
通過artisan創(chuàng)建模型
3、在控制器中引用模型,通過模型方法獲取數(shù)據(jù)庫中的數(shù)據(jù),并輸出
<?php namespace App\Http\Controllers; use App\Models\Users; use Illuminate\Http\Request; class Home extends Controller { public function index(Users $users){ echo '<pre>'; $res = $users->cc(); foreach($res as $k => $val){ $res[$k] = (array)$val; } $users = Users::find(1); print_r($users->name); } } ?>
點擊 "運行實例" 按鈕查看在線實例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號