假设我有这些关系
class Invitation
{
public function invitedPeople()
{
return $this->belongsTo('App\Person', 'personID');
}
}
class Person
{
public function apartments()
{
return $this->belongsToMany('App\Apartment', 'apartment_person', 'personID', 'apartmentID');
}
}
class Apartment
{
public function city()
{
return $this->belongsTo('App\City', 'cityID');
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,使用Laravel eager loading时我们可以拥有多少个嵌套级别?我已经尝试过这个查询并且它无法正常工作,有人可以建议解决这个问题吗?
return Invitation::with(['invitedPeople', 'invitedPeople.apartments', 'invitedPeople.apartments.city'])
Run Code Online (Sandbox Code Playgroud) 说我有这些模型:
用户模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'User';
protected $fillable =
[
'username', 'favoriteColor'
];
public function flights()
{
return $this->hasMany('App\Flight');
}
}
Run Code Online (Sandbox Code Playgroud)
飞行模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'Flight';
protected $fillable =
[
'flightName',
];
}
Run Code Online (Sandbox Code Playgroud)
每当我这样做时,我都会尝试用急切的加载做一些事情:
$usersWithFlights = …Run Code Online (Sandbox Code Playgroud) 我的问题更多的是一般的疑惑.我有两个使用Laravel创建的命令,我们称之为A和B.
这些命令中的每一个都使用 - > dailyAt($ par)方法进行调度.但$ par参数来自查询.
我的意思是这样的:
protected function schedule(Schedule $schedule)
{
$schedulerTime_commandA = App\Model\CommandsTime::where('id', 1)->first()->time;
$schedulerTime_commandB = App\Model\CommandsTime::where('id', 2)->first()->time;
$schedule->command('A')
->dailyAt($schedulerTime_commandA);
$schedule->command('B')
->dailyAt($schedulerTime_commandB);
}
Run Code Online (Sandbox Code Playgroud)
这是因为超级用户想要安排这些命令运行的时间.我的问题是:Laravel如何知道App\Console\Kernel.php文件中的此调度方法已被更改?
注意:我有以下cron条目,因为Laravel在文档上讨论它.
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
Run Code Online (Sandbox Code Playgroud) 想象一下以下模型:
var Office =
{
id: 1,
name: "My Office",
branches:
[
{
adddress: "Some street, that avenue",
isPrincipal: true,
},
{
adddress: "Another address",
isPrincipal: false,
},
]
}
Run Code Online (Sandbox Code Playgroud)
我想删除一个分支,但我们不能让用户从办公室删除主要分支。所以这是我的功能:
remove: function(body)
{
return new Promise(function(resolve, reject)
{
return Office.findByIdAndUpdate(1, { $pull: {'branches': {_id: body.branch.id}}}, { new: true })
.then(function(updatedOffice){
resolve(updatedOffice)
})
.catch(function(error){
reject(error);
});
})
}
Run Code Online (Sandbox Code Playgroud)
我在这里有一些疑问:
前端部分
参数的发送方式如下:
Laravel 请求
class CarCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
//TODO: Define authorization logic, possibly a middleware
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'car.name' => 'present|required'
];
}
}
Run Code Online (Sandbox Code Playgroud)
真正的问题
请求类始终验证为 false。我检查了验证数组部分,但看起来这可以发送如下参数:
car[name]=Spidey Mobile
Run Code Online (Sandbox Code Playgroud)
但是,我需要使用 JSON.stringify() 发送字符串化的数据。
有解决方法吗?看起来点符号不起作用,因为这是一个 JSON …