gpt4 book ai didi

php - 在 eloquent 中创建不适用于 php

转载 作者:行者123 更新时间:2023-11-29 11:15:43 24 4
gpt4 key购买 nike

我在 PHP 中创建了一个 MVC,并尝试使用 Eloquent 来处理数据库。但我陷入了它的开头,即;在“创建自身”中。

我的代码:

home.php:-

class Home extends Controller
{

public function create($username='',$email='')
{
User::create([
'username'=>$username,
'email'=>$email
]);
}
}

?>

Controller .php

<?php

class Controller
{
public function model($model)
{
require_once '../app/models/'. $model .'.php' ;
return new $model();
}
public function view($view,$data=[])
{
require_once '../app/views/'. $view.'.php';
}
}

?>

用户.php

 <?php

use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent
{
public $name;
protected $fillable=['username','email'];
}

?>

应用程序.php

  <?php

class App
{
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
// by default we are considering /public/controller/methodname
//as soon as the directory is written on the address bar the first arg is controller , 2nd is method and the
// others are parameters
public function __construct()
{
$url = $this->parseUrl();
if(file_exists('../app/controllers/'. $url[0].'.php'))
{
$this->controller= $url[0];
unset($url[0]); //remove fromm array
} //this if was to check whether the entered controller exists or not, if exists controller is set to the controllers name
//or home is the default controller
require_once '../app/controllers/'. $this->controller.'.php';
$this->controller= new $this->controller;// create object instance of that controller
if(isset($url[1]))
{
if(method_exists($this->controller, $url[1]))
{
$this->method = $url[1];
unset($url[1]);
}
}

$this->params= $url ? array_values($url) : [];
call_user_func_array([$this->controller,$this->method], $this->params);
}
public function parseUrl()
{
if(isset($_GET['url'])){
return $url = explode('/',filter_var(rtrim($_GET['url'],'/'),FILTER_SANITIZE_URL));
}
}
}

?>

在 Mysql 中的用户表中,每次点击 url 后,只有created_at 和updated_at 会更新。

enter image description here

网址格式:公共(public)/ Controller /方法/参数1/参数2

网址:公共(public)/home/create/Johnny/johnny16@live.com

最佳答案

请检查mysql表中的数据类型。它应该与您在创建方法数组中使用的数据兼容。即它应该是 varchar(length)。

关于php - 在 eloquent 中创建不适用于 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39808379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com