输入为json的示例
{
"user":{
"name":"Thomas",
"age":101
},
"shoppingcart":{
"products":{
"p1":"someprod",
"p2":"someprod2"
},
"valuta":"eur",
"coupon":null,
"something":[
"bla1",
"bla2"
]
}
}
Run Code Online (Sandbox Code Playgroud)
预期产出
[
'user.name' => 'Thomas',
'user.age' => 101,
'shoppingcart.products.p1' => 'someprod',
...
'shoppingcart.something.1' => 'bla1'
]
Run Code Online (Sandbox Code Playgroud)
我写了这个函数,但它产生了错误的输出.接下来,我想将所述函数重写为宏,Collection但我无法绕过它.问题还在于当前函数需要全局变量来跟踪结果.
public function dotFlattenArray($array, $currentKeyArray = []) {
foreach ($array as $key => $value) {
$explodedKey = preg_split('/[^a-zA-Z]/', $key);
$currentKeyArray[] = end($explodedKey);
if (is_array($value)) {
$this->dotFlattenArray($value, $currentKeyArray);
} else {
$resultArray[implode('.', $currentKeyArray)] = $value;
array_pop($currentKeyArray);
}
}
$this->resultArray += $resultArray;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是双重的:1.有时函数没有给出正确的结果2.如何将这个递归函数重写为宏
Collection::macro('dotflatten', …Run Code Online (Sandbox Code Playgroud) class PageRelation extends Eloquent
{
public $incrementing = false;
public $timestamps = false;
protected $table = 'page_relation';
protected $casts = [
'parent' => 'int', // FK to page
'child' => 'int', // FK to page
'lpc' => 'int',
];
protected $fillable = [
'lpc',
];
public function children()
{
return $this->hasMany(Page::class, 'category_id', 'child');
}
public function parents()
{
return $this->hasMany(Page::class, 'category_id', 'parent');
}
public function siblings()
{
// ... return $this->hasMany(Page::class ...
// how do I define this relationship?
} …Run Code Online (Sandbox Code Playgroud) $rosters = EventRosters::where('event_id', $event_id)
->whereJsonContains('players', $user_id)
->whereNull('deleted_at')
->get();
Run Code Online (Sandbox Code Playgroud)
The eloquent query above seems to only work when there is a single item in the 'players' json array.
The data stored in the database looks as follows:
[1] vs ["1","2"]
Is there a reason the whereJsonContains is only working when it sees [1] in the db but not when it sees ["1","2"] ?
I am pretty new to Laravel and have been struggling with this one a bit.
我的 Laravel 应用程序中有多对多关系。模型Competition属于ToMany Location,反之亦然。
现在我正在尝试提供一种功能,可以将现有位置添加到竞赛中。
$competition = Competition::where('id','=', $request->input('contestId'))->firstOrFail();
$locations = $competition->locations;
$locationNames = [];
foreach ($locations as $location) {
$locationNames[] = $location->name;
}
if (!in_array($request->input('locationList'), $locationNames)) {
$locationId = Location::where('name','=', $request->input('locationList'))->firstOrFail()->id;
$competition->locations()->attach($locationId);
}
Run Code Online (Sandbox Code Playgroud)
我需要检查比赛是否已经有了位置,所以我将比赛数据存储在里面$competition。之后,如果找不到该位置,我会将该位置附加到竞赛中。
问题在于正在运行的查询数量;一种用于比赛数据,一种用于在比赛地点内未找到时检索其 ID 的位置,另一种用于在附加时存储数据。
这会返回错误:“超出了最大执行时间 60 秒”
这样做的正确方法是什么?最大限度地减少所需的查询量。
提前致谢
播种机:
class CouponTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$coupons = (array)factory(\App\Coupon::class,10)->make();
Log::info('Created coupons.. ', $coupons);
}
}
class CompanyTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$companies = (array)factory(\App\Company::class,10)->make();
Log::info('Company users.. ', $companies);
}
}
class CustomerTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$customers …Run Code Online (Sandbox Code Playgroud) 我已经用分页编写了这个查询
$items = Item::select('items.*', 'sub_category_name', 'category_name', 'sub_category_slug', 'category_slug')
->join('sub_categories AS sc', 'sc.sc_id', 'items.sub_category_id')
->join('categories AS c', 'c.category_id', 'sc.category_id')
->where('items.is_active', '=', 1)
->where('sc.is_active', '=', 1)
->where('c.is_active', '=', 1)
->where('sc.sc_id', '=', $sub_category_id)
->paginate(1);
Run Code Online (Sandbox Code Playgroud)
但它说
语法错误或访问冲突:1140如果没有GROUP BY子句,则GROUP列(MIN(),MAX(),COUNT(),...)的混合没有GROUP列是非法的
但是,当我添加->groupBy('item_id');它说
语法错误或访问冲突:1055'books.items.item_name'不在GROUP BY中
但是当我在groupBy子句中执行item_name时,它会将groupBy称为下一列.为什么?
我如何belongsToThrough通过中间国家表获得计划的货币?
这是我的数据库结构:
plan
id - integer
country_id - string // country.code
country
code - string
currency_id - string // currency.code
currency
code - string
Run Code Online (Sandbox Code Playgroud)
我需要currency()计划模型中的关系:
我试了几次都没有用。我想过这样的事情,但这只是返回空值:
class Plan extends \Illuminate\Database\Eloquent\Model
{
public function currency()
{
return $this->belongsTo(
Currency::class, // $related,
'currency_id', // $foreignKey = null,
'code', // $ownerKey = null,
'country' // $relation = null
);
}
Run Code Online (Sandbox Code Playgroud) 该示例基于 Laravel 的注册。
我在 register.blade.php 中添加了以下内容:
<div class="form-group row">
<label for="file" class="col-md-4 col-form-label text-md-right">{{ __('Files') }}</label>
<div class="col-md-6">
<input type="file" id="files" name="files[]" multiple>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
RegisterController 中的方法如下所示:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'files.*' => ['required', 'file'],
]);
dd($validator->errors());
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试上传 PDF 和 DOC 文件。:
MessageBag {#236 ?
#messages: array:2 [?
"files.0" => array:1 [?
0 => "The files.0 must be a file."
]
"files.1" => array:1 [?
0 => "The files.1 …Run Code Online (Sandbox Code Playgroud) 我有一张products桌子和一张orders桌子。它们之间有很多对很多的关系,而product_order表是中介。现在,我有两个产品ID,并且想要选择包含两个产品ID的订单(如果存在)。如何用Laravel雄辩地做到这一点?
class Product extends Model
{
// ...
public function orders()
{
return $this->belongsToMany('App\Order', 'product_order');
}
}
class Order extends Model
{
// ...
public function products()
{
return $this->belongsToMany('App\Products', 'product_order');
}
}
Run Code Online (Sandbox Code Playgroud) laravel paginator在where子句中不与LIKE一起使用
laravel / framework版本:v5.6.33
控制者
$search_qs = $request->input('search');
$query = Article::where("status", 2);
$query->where('title', 'LIKE', '%' . $search_qs . '%');
$articles = $query->paginate(3);
Run Code Online (Sandbox Code Playgroud)
视图
{{ $articles->links() }}
Run Code Online (Sandbox Code Playgroud)
数据库查询
select * from `articles` where `status` = '2' and `title` LIKE '%txt2search%' limit 3 offset 0
Run Code Online (Sandbox Code Playgroud)
!!! 好 !!!
但是,当我单击分页器的第2页时
select * from `articles` where `status` = '2' and `title` LIKE '%%' limit 3 offset 3
Run Code Online (Sandbox Code Playgroud)
绑定
0. 2 (`status` = ?)
1. %% (`title` LIKE ?)
Run Code Online (Sandbox Code Playgroud)
值应存储在session或flash_session中?但是未检索到LIKE值
laravel ×9
eloquent ×5
php ×5
laravel-5 ×2
arrays ×1
json ×1
laravel-5.6 ×1
laravel-5.7 ×1
mysql ×1
orm ×1
paginator ×1
recursion ×1
validation ×1