[
"bob" => $books
"maria" => $otherBooks
]
to
[
[
name => "bob"
data => $books
],
[
name => "maria"
data => $otherBooks
]
]
You can utilize map, map takes $item and $key there is your input arrays key value, as the argument for the closure. If you map those to expected array structure this should work.
$result = collect([
'bob' => $books
'maria' => $otherBooks
])->map(function ($item, $key) {
return [
'name' => $key,
'data' => $item,
]
});
You can do this easily with collections.
collect([
"bob" => $books
"maria" => $otherBooks
])->map(function ($books, $name) {
return [
'name' => $name,
'data' => $books
];
});
Related
public function test_show_role_should_return_actions()
{
$this->actingAs($this->user, 'api');
$this->withHeaders(['Accept' => 'application/json',])
->get(route('roles.show', ['role' => $this->role->id]))
->assertJsonStructure([
"data" => [
"name",
"actions" => [
"name",
"code"
]
]
]);
}
when I run the test,I have this error:
Tests\Feature\Role\RoleTest::test_show_role_should_return_actions
Failed asserting that an array has the key 'name'.
Even when I remove the "name",I get an error for the "actions" key.
public function test_show_role_should_return_actions()
{
$this->actingAs($this->user, 'api');
$this->getJson(route('roles.show', ['role' => $this->role->id]))
->assertJsonStructure([
'data' => [
'*' => [
'name',
'actions' => [
'*' => [
'name',
'code'
]
]
]
]
]);
}
I have a lot of data and each record have difference value. I try to loop insert/update data but it take time. Is it have better way to improve my code?
I don't want to delete all data before update.
$data = [
[ 'id' => 1, 'name' => 'A' ],
[ 'id' => 2, 'name' => 'B' ],
[ 'id' => 3, 'name' => 'C' ],
[ 'id' => 4, 'name' => 'D' ],
.
.
.
[ 'id' => 10000, 'name' => 'ZYX' ],
]
foreach ($data as $item) {
DB::table('my_table')->updateOrInsert(['id' => $item->id], $item);
}
Thank.
You can use upsert in Laravel 8.x or use this package: https://github.com/staudenmeir/laravel-upsert to use INSERT ON DUPLICATE
$data = [
[ 'id' => 1, 'name' => 'A' ],
[ 'id' => 2, 'name' => 'B' ],
[ 'id' => 3, 'name' => 'C' ],
[ 'id' => 4, 'name' => 'D' ],
.
.
.
[ 'id' => 10000, 'name' => 'ZYX' ],
];
DB::table('my_table')->upsert($data, 'id');
use laravel upsert method here
for example you want to update if exists or insert record if not exists
$data = [
[
'id' => 1
'name' => 'J.K. Rowling',
'author' => 'Harry Potter',
'quantity' => 15
],
[
'id' => 2
'name' => 'Cal Newport',
'author' => 'Deep Work',
'quantity' => 20
]
];
DB::table('books')->upsert($data, ['id'], ['quantity']);
1.The first argument consists of the values to insert or update.
2.The second argument lists the column(s) that uniquely identify records (id in our case) within the associated table.
3.The third and final argument is an array of columns that should be updated if a matching record already exists in the database. We want the quantity to be updated in our case.
for more detail read official docs upsert
How to validate that fruits from a given $list are in the allowed list ?
$attributes [
"fruits" => ['nullable', 'string', 'in:apple,banana,orange'],
];
$list = [
"fruits" => [
"apple",
"orange"
]
];
$validator = Illuminate\Support\Facades\Validator::make($list, $attributes);
you can validate each item in this array individually:
$attributes = [
"fruits.*" => ['nullable', 'string', Rule::in(['apple','banana','orange'])],
];
I trying to return access the attributes inside a array of object and it is giving this error Exception: Property [id] does not exist on this collection instance.
Here is what I have tried:
protected function formatted($classroom)
{
return [
'courses' => [
'id' => $classroom->courses->id,
'name' => $classroom->courses->name,
'slug' => $classroom->courses->slug,
'coursteachers' => [
'id' => '$classroom->courses->coursteachers->id',
'email' => '$classroom->courses->coursteachers->email',
'uid' => '$classroom->courses->coursteachers->uid',
]
],
];
}
And here is the actual data:
"courses": [
{
"id": 1,
"name": "Analytics",
"slug": "analytics",
"status_id": 1,
"deleted_at": null,
"pivot": {
"classroom_id": 2,
"courses_id": 1
},
"coursteachers": [
{
"id": 3,
"uid": "S0120-46890",
"email": "teacher#vschool.com",
"user_type": "Teacher",
}]
}]
You need to iterate through courses and also courseteachers since they both represent an array but rather an object
protected function formatted($classroom)
{
$result = [];
foreach($classroom->courses as $course) {
$result[] = [
'courses' => [
'id' => $classroom->courses->id,
'name' => $classroom->courses->name,
'slug' => $classroom->courses->slug,
'coursteachers' => $this->getCourseTeachers($cours)
],
];
}
return $result;
}
private function getClassroomTeachers($classroom) {
$result = [];
foreach($classroom->courses as $cours)
{
foreach ($cours->coursteachers as $key => $teacher) {
$result[] = [
// 'coursteachers'=> [
'id' => $teacher->id,
'email' => $teacher->email,
'uid' => $teacher->uid,
'last_name' => $teacher->profile->last_name,
'first_name' => $teacher->profile->first_name,
'middle_name' => $teacher->profile->middle_name,
// ],
];
}
}
return $result;
}
Since courses is an array, you should pick an object using the proper index.
Foe example: try $classroom->courses[0]->id instead of $classroom->courses->id. Here, '0' is the index.
Also it is better if you check if the index exists before you do it. For an example.
'id' : isset($classroom->courses[$index]) ? $classroom->courses[$index]->id : ''
Edit:
In case of a Eloquent collection you should use $classroom->courses->get($index)->id instead of array like retrieval.
In my datatables, the search function works only on the name column. The name column ist different that the others. It means, the name column gives the value of the database back, while the others gives a value back with the function add column
In the column array there are the following values:
ยดยดยด
protected function getColumns()
{
return [
'status',
'name' => [
'title' => 'Name',
'orderable' => true,
'searchable' => true
],
'location' => [
'title' => 'Standort',
],
'department' => [
'title' => 'Abteilung',
'orderable' => true,
'searchable' => true
],
'division' => [
'title' => 'Bereich',
'orderable' => true,
'searchable' => true
],
'leader' => [
'title' => 'Verantwortlicher',
'orderable' => true,
'searchable' => true
],
'start_date' => [
'title' => 'Startdatum',
'searchable'=> true,
],
'end_date' => [
'title' => 'Enddatum',
'searchable'=> true
]
];
}
```
Why it doesn't search on all columns? What i have to do?
Try to search by using this code.
return Datatables::of($tasks)
->filter(function ($query) use ($request) {
$title = $request->title;
if (isset($title) && !empty($title)) {
$query->where('title', 'like', '%'.$title.'%');
}
})->make(true);
OR
return Datatables::of($tasks)
->filterColumn('title', function($query, $value) {
$query->whereRaw("title like ?", ["%{$value}%"]);
})->make(true);