【发布时间】:2017-01-13 17:30:05
【问题描述】:
我的控制器是这样的:
public function create()
{
$data = array(
'email' => 'chelsea@gmail.com'
);
$data = Order::where('email', $data['email'])->first();
dd($data);
}
我的模型顺序是这样的:
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
class Order extends Model
{
use SoftDeletes, Notifiable;
public $table = 'orders';
protected $dates = ['deleted_at'];
public $fillable = [
'number',
'user_id',
'store_id',
'total_amount',
'total_product',
'service_amount',
'status',
'checkout_at'
];
protected $casts = [
'number' => 'string',
'user_id' => 'integer',
'store_id' => 'integer',
'total_amount' => 'integer',
'total_product' => 'integer',
'service_amount' => 'integer',
'status' => 'integer',
'checkout_at' => 'date'
];
public static $rules = [
'user_id' => 'required',
'store_id' => 'required'
];
public function user()
{
return $this->belongsTo(User::class,'user_id','id');
}
public function store()
{
return $this->belongsTo(Store::class,'store_id','id');
}
}
表用户有字段 id、姓名、电子邮件等
table store 有字段 id、name、email 等
我想从表用户和表存储中获取用户名和存储名。然后,我想将它存储在变量$data
我该怎么做?
更新
我的模型用户是这样的:
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
public $table = 'users';
protected $dates = ['deleted_at'];
public $fillable = [
'name',
'email',
'password',
'full_name',
'birth_date',,
'mobile_number',
];
protected $casts = [
'name' => 'string',
'email' => 'string',
'password' => 'string',
'full_name' => 'string',
'birth_date' => 'date',,
'mobile_number' => 'string',
];
public static $rules = [
'name' => 'required',
'email' => 'required'
];
}
我的模型店是这样的:
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Store extends Model
{
use SoftDeletes;
public $table = 'stores';
protected $dates = ['deleted_at'];
public $fillable = [
'user_id',
'domain',
'name',
'email'
];
protected $casts = [
'user_id' => 'integer',
'domain' => 'string',
'name' => 'string',
'email' => 'string'
];
public static $rules = [
'user_id' => 'required',
'domain' => 'required',
'name' => 'required'
];
public function user()
{
return $this->belongsTo(User::class,'user_id','id');
}
}
【问题讨论】:
-
@AddWeb Solution Pvt Ltd,我更新了我的问题
-
(离题):不要让
password在你的用户模型中填写。我对此感到不安全 =) -
@mosestoh:检查中...!
-
请先在
Store模型和User模型中定义订单模型关系 -
我认为answer 可能会对您有所帮助。
标签: php laravel eloquent laravel-5.3