首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel9:如何让模型的find()工作?

Laravel9:如何让模型的find()工作?
EN

Stack Overflow用户
提问于 2022-06-16 06:30:55
回答 1查看 44关注 0票数 0

我有服务层。我的代码在Laravel 5.6中工作。现在我正在用Laravel 9重写,它不再起作用了。

UserController

代码语言:javascript
复制
public function getForm($id = null)
{
    ...
    $user = $this->userService->findIdOrNew($id);

UserService

代码语言:javascript
复制
class UserService
{
    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
        $this->modelName = "\App\Models\User";
        $this->model = new $this->modelName;
        $this->table = $this->model->getTable();
    }

    private function newModel()
    {
        return new $this->modelName;
    }

    public function findIdOrNew($id = null)
    {
        if(empty($id)){
            $row = new $this->modelName;
        }else{
            $row = $this->model->find($id);
            echo "<pre>", print_r($row, 1), "</pre>";exit;
        }
        return false;        
    }

逻辑:

如果没有用户id,那么新的emtpy用户模型,返回它。

如果提供了用户id,则获取用户模型,并找到用户id。

$this->model只执行__controller中的新操作。

我还有很多类似的功能。

代码语言:javascript
复制
public function updateById($data, $id)
{
    $row = $this->model->find($id);
    $row->email = $data['email'];
    $row->name = $data['name'];
    ...

public function getRows($data = array(), $debug = 0)
{
    $query = $this->model->query();

    if(!empty($data['filter_ids'])){
        $query->whereIn('id', $data['filter_ids']);
    }

    if(!empty($data['filter_name'])){
        $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name'])));

        $query->where(function ($query) use($words) {
            $query->orWhere(function ($query2) use($words) {
                foreach ($words as $word) {
                    $query2->where('name', 'like', "%{$word}%");
                }
            });
        });
    }

以这种方式,我不使用User::find(1),我不需要在函数中说明我想要哪个模型。只需在__contructor()中编写一次。

它在我的laravel 5.6中确实有效。现在它空回来了。没有错误信息。

代码语言:javascript
复制
public function findIdOrNew($id = null)
{
    ini_set("display_errors", "On");
    error_reporting(E_ALL ^E_NOTICE ^E_DEPRECATED);
    
    $row = $this->model->find($id);
    echo "<pre>", print_r($row, 1), "</pre>";exit;

我应该得到:

代码语言:javascript
复制
App\Models\User Object
(
    [fillable:protected] => Array
        (
            [0] => name
            [1] => email
            [2] => password
        )

    [hidden:protected] => Array
        (
            [0] => password
            [1] => remember_token
        )

    [connection:protected] => mysql
    [table:protected] => 
    [primaryKey:protected] => id
    [keyType:protected] => int
    [incrementing] => 1
    [with:protected] => Array
        (
        )

    [withCount:protected] => Array
        (
        )

    [perPage:protected] => 15
    [exists] => 1
    [wasRecentlyCreated] => 
    [attributes:protected] => Array
        (
            [id] => 50
            [rcode] => 1971836358
            [name] => ooxxooxx
    ...

现在只是一页白纸。我错过了什么?

我删除了所有日志,重新启动apache服务器,然后重新加载页面。

Apache/logs/laraocadmin9.test-access.log

127.0.0.1 - 16/Jun/2022:14:39:46 +0800 "GET /en/admin/system/user/user/49/编辑HTTP/1.1“200 11

127.0.0.1 - 16/Jun/2022:14:39:48 +0800 "GET /favicon.ico HTTP/1.1“200 -

Apache/logs/laraocadmin9.test-error.log

(空)

Apache/logs/access.log

(空)

Apache/logs/error.log

清华6月16日14:38:09.392250 2022 pid 40028:tid 388 AH00455: Apache/2.4.53 (Win64) mod_fcgid/2.3.10dev配置-恢复正常运行

清华6月16日14:38:09.393249 2022 pid 40028:AH00456 388 : Apache休息室VS16服务器建成日期:2022年3月16日11:26:15

清华6月16日14:38:09.406444 2022 pid 40028:AH00418 388 :父进程:创建子进程20972

清华6月16日14:38:10.096179 2022 pid 20972:tid 432 AH00354:子:启动64个工作线程。

Laravel存储\日志

只有一个文件名.gitnore

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-16 07:07:02

抱歉,还能用。我传递的用户标识是错误的。我有两个不同的项目。一个是

代码语言:javascript
复制
$this->getForm('edit', $id)
$this->getForm('create', $id)
function getForm($action, $id=null)

另一个是

代码语言:javascript
复制
$this->getForm($id)
$this->getForm($id)
function getForm($id=null)

我搞混了,把“编辑”传递给了getForm($id)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72641424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档