products
--- id
--- name
product_attributes
--- id
--- name
product_attribute_values
--- id
--- attribute_id
--- product_id
--- value
class Product extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function attributes(): BelongsToMany
{
return $this->belongsToMany(Attribute::class, 'product_attribute_value')->withPivot(['value']);
}
}
class Attribute extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function getValuesAttribute($value){
return collect(explode(',', $value));
}
}
$a1 = \App\Models\Attribute::create([
'name' => 'Attr1',
]);
$a2 = \App\Models\Attribute::create([
'name' => 'Attr2',
]);
$a3 = \App\Models\Attribute::create([
'name' => 'Multy Attr3',
]);
$p1 = \App\Models\Product::create([
'name' => 'Product1',
]);
$p1->attributes()->attach($a1->id, [
'value' => '2012',
]);
$p1->attributes()->attach($a2->id, [
'value' => 'Джинса',
]);
$p1->attributes()->attach($a3->id, [
'value' => 'Синий',
]);
$p1->attributes()->attach($a3->id, [
'value' => 'Черный',
]);
$p1->attributes()->attach($a3->id, [
'value' => 'Зеленый',
]);
$p2 = \App\Models\Product::create([
'name' => 'Product2',
]);
$p2->attributes()->attach($a1->id, [
'value' => '2013',
]);
$p2->attributes()->attach($a2->id, [
'value' => 'Лен',
]);
$p2->attributes()->attach($a3->id, [
'value' => 'Красный',
]);
$p2->attributes()->attach($a3->id, [
'value' => 'Серобурый',
]);
$p3 = \App\Models\Product::create([
'name' => 'Product3',
]);
$p3->attributes()->attach($a1->id, [
'value' => '2015',
]);
$p3->attributes()->attach($a2->id, [
'value' => 'Лен',
]);
$p3->attributes()->attach($a3->id, [
'value' => 'Синий',
]);
$p3->attributes()->attach($a3->id, [
'value' => 'Желтый',
]);
public function attributes_with_grouped_values(): HasMany{
$instance = $this->newRelatedInstance(Attribute::class);
$query = $instance->newQuery()->join('product_attribute_value', 'product_attribute_value.attribute_id', '=', 'attributes.id')
->groupBy('attributes.id', 'attributes.name', 'product_attribute_value.product_id')
->select('attributes.id', 'attributes.name', 'product_id', DB::raw('string_agg(product_attribute_value.value, \',\') as values'));
// в случае с мускул string_agg(product_attribute_value.value, \',\') заменить на group_concact
return $this->newHasMany(
$query, $this, 'product_attribute_value.product_id', 'id'
);
}
public function getValuesAttribute($value){
return collect(explode(',', $value));
}
$products = Product::with('attributes_with_grouped_values')->get();
foreach ($products as $product){
echo $product->name.PHP_EOL;
foreach ($product->attributes_with_grouped_values as $attribute){
dump(json_encode([$attribute->name => $attribute->values->all()]));
}
}
Product1
^ "{"Attr1":["2012"]}"
^ "{"Attr2":["\u0414\u0436\u0438\u043d\u0441\u0430"]}"
^ "{"Multy Attr3":["\u0421\u0438\u043d\u0438\u0439","\u0417\u0435\u043b\u0435\u043d\u044b\u0439","\u0427\u0435\u0440\u043d\u044b\u0439"]}"
Product2
^ "{"Attr1":["2013"]}"
^ "{"Attr2":["\u041b\u0435\u043d"]}"
^ "{"Multy Attr3":["\u041a\u0440\u0430\u0441\u043d\u044b\u0439","\u0421\u0435\u0440\u043e\u0431\u0443\u0440\u044b\u0439"]}"
Product3
^ "{"Attr1":["2015"]}"
^ "{"Attr2":["\u041b\u0435\u043d"]}"
^ "{"Multy Attr3":["\u0416\u0435\u043b\u0442\u044b\u0439","\u0421\u0438\u043d\u0438\u0439"]}"
protected $messages = [
'nameOrganization.required' => 'Заполните поле :attribute. :input - не подходит'
];
public function toArray($request)
{
return [
'name' => $this->name,
'email' => $this->email,
'balance' => $this->resource->hasVerifiedEmail()?100:0,
];
}
$students = Students::all()->where('class','=',$i);
$students = Students::where('class','=',$i)->get();
if (isset($i)) {
$students = Students::all()->where('class','=',$i);
} else {
$students = Students::all();
}
$students = Students::when($i, function($builder, $i){ return $builder->where('class', '=', $i); } )->get();
try {
$dbconnect = DB::connection()->getPDO();
$dbname = DB::connection()->getDatabaseName();
} catch(Exception $e) {
return redirect('/exception');
}
class AutoMark extends Model
{
use HasFactory;
public function models()
{
return $this->hasMany(AutoModel::class, 'mark_index', 'index');
}
public function modifications()
{
return $this->hasManyThrough(AutoModification::class, AutoModel::class);
}
}
$user = Auth::user();
DB::table('book')
->select('book.*')
->where([
['book.country', '=', $country->id],
['book.city', '=', 0],
['book.approve', '=', 1],
])
->when($user, function($query, $user)
{
$query->leftJoin('favorite', function($query) use($user){
$query->whereColumn('book.id', 'favorite.radio_id')->where('favorite.user_id', '=', $user->id);
})->addSelect('favorite.id as favorite_checked');
})
->paginate(50);
->where('status', '=' , 'active')
->whereIn('uid', [2,6])
->get();
->where('status', '=' , 'active')
->where(function($builder){
$builder->where('uid', '=', '2')
->orWhere('uid', '=', '6');
})
->get();
$countries = Country::whereHas('cities', function($builder){
return $builder->where('population', '>', 100000);
})
->with(['cities' => function($builder){
return $builder->where('population', '>', 100000)->orderBy('population', 'asc');
}])
->get();
$items = User::when($request->get('login'), function($query, $login){ $query->where('login', $login) })
->when($request->get('date_start'), function($query, $date_start){ $query->where('date_start', '>=', $date_start) })
->when($request->get('date_end'), function($query, $date_end){ $query->where('date_end', '<=', $date_end) })
->get();
$items = Trip::when($request->get('day_date'), function($query, $date){ $query->where('date', $date); })
->when($request->get('city_departure'), function($query, $departure_id){ $query->where('departure_id', $departure_id); })
->when($request->get('city_arrival'), function($query, $landing_id){ $query->where('landing_id', $landing_id); })
->get();
return view('search')->with(['mass' => $items]);
$result = '';
foreach ($comments as $comment){
$result .= view('comment')->with('comment', $comment)->render();
}
То есть напрямую изменить одно поле в массиве входящих данных?
Illuminate\Http\Request::macro(
'mergeGently', function($array){
// где делаем магию аля проверки и прочее.
});
Как его туда прокинуть?
public function register()
{
$this->renderable(function (ValidationException $exception) {
return redirect($exception->redirectTo ?? url()->previous())
->withInput(Arr::except($exception->validator->getData(), $this->dontFlash))
->withErrors($exception->errors(), $exception->errorBag);
});
}
Post::where('status', 1)
->join('post_tag', 'posts.id', '=', 'post_tag.post_id')
->join('tags', 'tags.id', '=', 'post_tag.tag_id')
->select('posts.*', DB::raw('group_concat(tags.name) as tags_name'))
->groupBy('posts.id', 'posts.name', 'posts.content')->get();
Post::where('status', 1)
->select('posts.*', DB::raw('select group_concat(tags.name) as tags_name from post_tag join tags on tags.id=post_tag.tag_id where post_tag.post_id = posts.id'))->get();
Schema::table()....
$table->string('uuid')->default(DB::raw('UUID()'));
});
должно сработать.