首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >雄辩的第一种方法返回带有模型变量的集合

雄辩的第一种方法返回带有模型变量的集合
EN

Stack Overflow用户
提问于 2018-07-18 13:36:40
回答 1查看 1.1K关注 0票数 0

嗨,我创建了一个模型,它是:

代码语言:javascript
复制
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Support\Facades\Schema;

class leads extends Model
{
use SoftDeletes;
protected $connection = 'mysql';
protected $dates = ['deleted_at'];
protected $table = 'tableName_name';
public $timestamps = false;
protected $guarded = [];
}
?>

控制器是这样的:

代码语言:javascript
复制
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\leads;
use DB;

class billingController extends Controller
{
    public $lead;
    public function __construct(leads $lead) {
        $this->Date = date("Y-m-d H:i:s");
        $this->date = date("Y-m-d");
        $this->lead = $lead;
    }

    public function GetInfo($user) {
        $GetUserInfo = $this->lead->select('*')->where('ID', $user)->first();
        if(empty($GetUserInfo)) return false;
        else return $GetUserInfo->userInfo;
    }
}

如果我想要userInfo

当我回显GetInfo时,它返回一个带花括号的字符串,而不是像laravel文档中提到的对象,它首先应该返回一个对象(std类),但是如果我返回一个对象( print_r(GetInfo) ),则会得到如下内容:

App\Leads对象(连接:受保护的=> mysql表:受保护的=> tableName_name dates:受保护的=> Array ( => deleted_at )时间戳=>保护:受保护的=> Array ()主键:受保护的=> ID键类型:受保护的=> int增量=> 1 with:protected => Array ()with => Array ()perPage:受保护的=> 15存在=> 1 wasRecentlyCreated =>属性:protected => 19 29 20 29.

我做错什么了吗?它应该作为std类对象返回所需的值,我需要更改什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-18 14:02:03

在此之前,我建议您在编写PHP代码时遵循一些标准。

conventions/PHP

代码语言:javascript
复制
Class names in UpperCamelCase
Functions in lowerCamelCase
Variables in lowerCamelCase

In databases, lower_snake_case (usually singular names)

BillingController

代码语言:javascript
复制
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Leads;
use DB;

class BillingController extends Controller
{
    public $lead;
    public function __construct(Leads $lead) {
        $this->Date = date("Y-m-d H:i:s");
        $this->date = date("Y-m-d");
        $this->lead = $lead;
    }

    public function getInfo($user) {
        //Since this is an Eloquent model, you can fetch by id using Find or even FindOrFail
        $getUserInfo = $this->lead->find($user);
        if(empty($getUserInfo)) {
            return false;
        ) 
        else {
            //I assume this is an attribute and not eager loading, otherwise you need to load the relationship
            //with either with() function or load('relationship')
            return $getUserInfo->userInfo;
            //This will the attribute as a string.
            //If you plan on retrieving the object as an array,
            return $getUserInfo->toArray();
            //If you plan on receiving in any other format on the other side, you just change the return value.
        }
    }
}

引线模型

代码语言:javascript
复制
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Support\Facades\Schema;

class Leads extends Model
{
    use SoftDeletes;
     //protected $connection = 'mysql'; Why do you need this? unless you got different tables, theres no need, it uses what's in the env file
     protected $dates = ['deleted_at'];
     protected $table = 'tableName_name';
     public $timestamps = false;
     protected $guarded = [];
}


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

https://stackoverflow.com/questions/51403451

复制
相关文章

相似问题

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