嗨,我创建了一个模型,它是:
<?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 = [];
}
?>控制器是这样的:
<?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类对象返回所需的值,我需要更改什么?
发布于 2018-07-18 14:02:03
在此之前,我建议您在编写PHP代码时遵循一些标准。
Class names in UpperCamelCase
Functions in lowerCamelCase
Variables in lowerCamelCase
In databases, lower_snake_case (usually singular names)BillingController
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.
}
}
}引线模型
<?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 = [];
}
?>https://stackoverflow.com/questions/51403451
复制相似问题