我与laravel合作。 我创建了一个名为CommonUtilities的库类,在该类中,我从多个表中获取数据,但有时在从getPostList方法获取数据时,会返回一些像null这样的数据,例如status。
这是我的代码
namespace post\Libraries;
use DB;
use App\Quotation;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use post\Libraries\CommonUtilities;
class CommonUtilities extends Model
{
//
public function getTrainerDetails($t_code = "")
{
if (!empty($t_code) && $t_code != "") {
return DB::table('registered_trainers')
->where('registered_trainers.t_code', $t_code)
->leftjoin('trainer_experience', 'registered_trainers.t_code', '=', 'trainer_experience.trainer_code')
->leftjoin('qualification', 'registered_trainers.t_code', '=', 'qualification.t_code')
->first();
} else {
return DB::table('registered_trainers')
->leftjoin('trainer_experience', 'registered_trainers.t_code', '=', 'trainer_experience.trainer_code')
->leftjoin('qualification', 'registered_trainers.t_code', '=', 'qualification.t_code')
->get();
}
}
public function getPostList($tid = "")
{
if (!empty($tid) && $tid != "") {
return DB::table('posts')->where('t_id', $tid)
->leftjoin('registered_trainers', 'posts.t_id', '=', 'registered_trainers.tid')
->orderBy('posts.created_at', 'desc')
->get();
} else {
return DB::table('posts')
->leftjoin('registered_trainers', 'posts.t_id', '=', 'registered_trainers.tid')
->orderBy('posts.created_at', 'desc')
->get();
}
}
这里我正在调用getPostList函数以获取所有帖子详细信息
namespace post\Http\Controllers\admin;
use DB;
use Illuminate\Database\Eloquent\Model;
use post\Libraries\CommonUtilities;
use Illuminate\Http\Request;
use post\Http\Controllers\Controller;
class AdminController extends Controller
{
//
public function index()
{
$commClassObj = new CommonUtilities();
$postList = $commClassObj->getPostList();
return view('pages.dashboard',['PostList' => $postList]);
}
}
,输出为
Collection {#211 ▼
#items: array:5 [▼
0 => {#220 ▼
+"pid": 7
+"t_id": 7
+"post_title": "asassda"
+"post_desc": "asfdsgdsgvx"
+"file": "xfgdgd.jpg"
+"status": null
+"created_at": "2018-11-14 02:23:00"
+"updated_at": "2018-11-14 02:23:00"
+"deleted_at": null
+"ipaddress": "::1"
+"tid": 7
+"t_code": "SM4125"
+"first_name": "test"
+"last_name": "testing"
+"email": "test@example.com"
+"phone1": "9876543210"
+"phone2": "9876543211"
+"address": "Lorem ipsum dolor sit amet."
+"city": "Lorem"
+"state": "ipsum"
+"zip": "100001"
+"password": "asdf"
+"email_verified": null
+"verified_by": null
+"remember_token": "yes"
+"browser": "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
+"deactivated_at": null
}
1 => {#213 ▶}
2 => {#216 ▶}
3 => {#212 ▶}
4 => {#215 ▶}
]
}