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

时间:2018-07-18 13:36:41

标签: laravel eloquent

嗨,我创建了一个模型:

<?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 [日期:受保护] =>数组([0] => Deleted_at)[时间戳记] => [受保护:受保护] =>数组()   [primaryKey:protected] => id [keyType:protected] => int [递增]   => 1 [with:protected] => Array()[withCount:protected] => Array()[perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] =>   [attributes:protected] =>数组([ID] => 19 [userInfo] => 29 ........

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

1 个答案:

答案 0 :(得分:0)

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

  

https://www.mediawiki.org/wiki/Manual:Coding_conventions/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 = [];
}


?>