Delete key 'data' of collection Laravel - php

I need to remove the 'data' key from my collection in Laravel.
This worked for me, but it removed the other keys that I cared to keep, I just need to remove the 'data' key:
return $filteredValues ​​= $collection->values ​()->all(); // I remove other keys inside the objects.
My collection return:
$records = Item::where('tienda_id',$id)->where('item.nombre', 'like', "%" . $query . "%")->take(50)->get();
return $collection = new ItemCollection($records);
My ItemCollection.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Facades\Storage;
class ItemCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* #param \Illuminate\Http\Request $request
* #return mixed
*/
public function toArray($request)
{
return $this->collection->transform(function($row, $key) {
return [
'id' => $row->id,
'nombre' => $row->nombre,
'marca_id' => $row->marca_id,
'tienda_id' => $row->tienda_id,
'nombre_marca' => $row->marca->nombre_marca,
'unidad_id' => $row->unidad_id,
'nombre_unidad' => $row->unidad->nombre_unidad,
'tipo_cambio' => $row->tienda->tipocambio,
'categoria_id' => $row->categoria_id,
'stock' => $row->stock,
'moneda' => $row->moneda,
'codigos' => $row->codigos,
'stockminimo' => $row->stockminimo,
'stockmaximo' => $row->stockmaximo,
'impuesto_id' => $row->impuesto_id,
'primer_margen' => $row->primer_margen,
'segundo_margen' => $row->segundo_margen,
'precio' => $row->precio,
'notas' => $row->notas,
'imagen' => url('images/'.$row->imagen),
];
});
}
}
My JSON:
{
"data": [
{
"id": 27,
"nombre": "Nombre de prueba",
"marca_id": 2,
"tienda_id": 2,
"nombre_marca": "marca 2",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 100,
"moneda": "$",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 2,
"nombre": "66677kj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 5,
"nombre": "78877k"
}
}
],
"stockminimo": 1,
"stockmaximo": 100,
"impuesto_id": 1,
"primer_margen": "35.00",
"segundo_margen": "20.00",
"precio": "5.07",
"notas": "jkjkkjkkj",
"imagen": "http://maks.test/images/159692202782450637_121718839341269_2075616741920079872_o.jpg"
},
{
"id": 28,
"nombre": "Aleta de pollo",
"marca_id": 1,
"tienda_id": 2,
"nombre_marca": "marca1x",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 5,
"moneda": "S/",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 2,
"nombre": "jhjjkj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 5,
"nombre": "jkk"
}
}
],
"stockminimo": 6,
"stockmaximo": 88,
"impuesto_id": 1,
"primer_margen": "2.00",
"segundo_margen": "10.00",
"precio": "100.00",
"notas": "jkjkkjjk",
"imagen": "http://maks.test/images/159698573182341068_123146022531884_1345097685962588160_o.jpg"
}
]
}
I need it to look like this:
[
{
"id": 27,
"nombre": "Nombre de prueba",
"marca_id": 2,
"tienda_id": 2,
"nombre_marca": "marca 2",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 100,
"moneda": "$",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 2,
"nombre": "66677kj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 5,
"nombre": "78877k"
}
}
],
"stockminimo": 1,
"stockmaximo": 100,
"impuesto_id": 1,
"primer_margen": "35.00",
"segundo_margen": "20.00",
"precio": "5.07",
"notas": "jkjkkjkkj",
"imagen": "http://maks.test/images/159692202782450637_121718839341269_2075616741920079872_o.jpg"
},
{
"id": 28,
"nombre": "Aleta de pollo",
"marca_id": 1,
"tienda_id": 2,
"nombre_marca": "marca1x",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 5,
"moneda": "S/",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 2,
"nombre": "jhjjkj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 5,
"nombre": "jkk"
}
}
],
"stockminimo": 6,
"stockmaximo": 88,
"impuesto_id": 1,
"primer_margen": "2.00",
"segundo_margen": "10.00",
"precio": "100.00",
"notas": "jkjkkjjk",
"imagen": "http://maks.test/images/159698573182341068_123146022531884_1345097685962588160_o.jpg"
}
]
Thanks in advance for reading me, I hope you can help me solve this problem, thanks!

In AppProvider.php or similar add the following. This will disable data wrapping, for ItemCollections. Thou i would want you to reconsider, if you need to use pagination or meta attributes on your responses, you have no where to put em withouth data wrapping and thats one of the reason it is used.
public function boot()
{
ItemCollection::withoutWrapping();
}

Related

make a new variable array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I need to make new array from another array variable. So I have 3 variable
$transaction
[
{
"id": 2,
"member_id": 19,
"date_start": "2023-03-14",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Lee Rosenbaum"
},
{
"id": 3,
"member_id": 14,
"date_start": "2023-03-31",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Erik Spinka"
},
{
"id": 4,
"member_id": 15,
"date_start": "2023-03-26",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan"
},
{
"id": 5,
"member_id": 9,
"date_start": "2023-02-21",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anthony Wilkinson"
},
{
"id": 6,
"member_id": 15,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan"
},
{
"id": 7,
"member_id": 13,
"date_start": "2023-04-08",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Pink Moen"
},
{
"id": 9,
"member_id": 10,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Viva Wilkinson"
}
]
$jumlah
[
{
"transaction_id": 2,
"total": "5"
},
{
"transaction_id": 3,
"total": "7"
},
{
"transaction_id": 4,
"total": "2"
},
{
"transaction_id": 5,
"total": "4"
},
{
"transaction_id": 6,
"total": "4"
},
{
"transaction_id": 7,
"total": "2"
},
{
"transaction_id": 9,
"total": "2"
}
]
$tanggal
[
{
"transaction_id": 2,
"tgl_kembali": "2023-05-25"
},
{
"transaction_id": 3,
"tgl_kembali": null
},
{
"transaction_id": 4,
"tgl_kembali": null
},
{
"transaction_id": 5,
"tgl_kembali": null
},
{
"transaction_id": 6,
"tgl_kembali": null
},
{
"transaction_id": 7,
"tgl_kembali": "2023-02-17"
},
{
"transaction_id": 9,
"tgl_kembali": "2023-04-21"
}
]
I need to make a new variabel like $newData
I expected the result should be like this for $newData :
[
{
"id": 2,
"member_id": 19,
"date_start": "2023-03-14",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Lee Rosenbaum",
"tgl_kembali": "2023-05-25",
"total": "5"
},
{
"id": 3,
"member_id": 14,
"date_start": "2023-03-31",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Erik Spinka",
"tgl_kembali": null,
"total": "7"
},
{
"id": 4,
"member_id": 15,
"date_start": "2023-03-26",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan",
"tgl_kembali": null,
"total": "2"
},
{
"id": 5,
"member_id": 9,
"date_start": "2023-02-21",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anthony Wilkinson",
"tgl_kembali": null,
"total": "4"
},
{
"id": 6,
"member_id": 15,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Anabel Monahan",
"tgl_kembali": null,
"total": "4"
},
{
"id": 7,
"member_id": 13,
"date_start": "2023-04-08",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Pink Moen",
"tgl_kembali": "2023-02-17",
"total": "2"
},
{
"id": 9,
"member_id": 10,
"date_start": "2023-02-23",
"created_at": "2023-02-12T10:03:46.000000Z",
"updated_at": "2023-02-12T10:03:46.000000Z",
"name": "Viva Wilkinson",
"tgl_kembali": "2023-04-21",
"total": "2"
}
]
what I can to solve this problem? I work on php(laravel).For some reason I have not been able to find the answer anywhere. I assume it is possible but I just need to make sure.
const newData = [];
for (const transaction of transactions) {
const jumlah = jumlahs.find(j => j.transaction_id === transaction.id);
const tanggal = tanggals.find(t => t.transaction_id === transaction.id);
newData.push({
id: transaction.id,
member_id: transaction.member_id,
date_start: transaction.date_start,
created_at: transaction.created_at,
updated_at: transaction.updated_at,
name: transaction.name,
total: jumlah.total,
tgl_kembali: tanggal.tgl_kembali
});
}

Laravel Collection ordered by day of the week M-F

I have a Laravel collection grouped by day of the week but they are in the wrong order. I need them ordered by day of the week starting with Monday.
My Controller:
public function getWeeklySchedule()
{
$testSchedule = VolunteerSchedule::all();
$result = $testSchedule->groupBy(['assignment', function ($item) {
return $item['scheduled_days'];
}]);
return $result;
}
The result looks like this:
"Wednesday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 4,
"full_name": "Martine Kemmer Sr.",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Thursday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 7,
"full_name": "Leonardo Schaefer V",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 5,
"full_name": "Prof. Pablo Corwin",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 6,
"full_name": "Samantha Feil",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Friday": {
"kitchen": [
{
"id": 1,
"full_name": "Hipolito Carroll",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"driver": [
{
"id": 3,
"full_name": "Prof. Conor Grimes I",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 9,
"full_name": "Miss Eleonore Kutch IV",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 4,
"full_name": "Martine Kemmer Sr.",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Tuesday": {
"kitchen": [
{
"id": 2,
"full_name": "Miss Lessie Torphy",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
},
{
"id": 7,
"full_name": "Leonardo Schaefer V",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
],
"bagger": [
{
"id": 10,
"full_name": "Marcellus Walker",
"created_at": "2021-11-05T17:33:52.000000Z",
"updated_at": "2021-11-05T17:33:52.000000Z"
}
]
},
"Saturday": {...
Some fields have been removed for brevity. The days of the week are stored as a serialized array in the database. What is the best way to accomplish this?
Just define a custom sort function that uses a map for the days of the week:
$days = ["Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6, "Sunday" => 7];
$collection = collect(["Friday" => [], "Wednesday" => [], "Tuesday" => []]);
$collection->sortBy(fn($val, $key) => $days[$key]);
dump($collection);
Output:
=> Illuminate\Support\Collection {#4977
all: [
"Tuesday" => [],
"Wednesday" => [],
"Friday" => [],
],
}
So your code becomes:
public function getWeeklySchedule(): Collection
{
$days = ["Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6, "Sunday" => 7];
return VolunteerSchedule::all()
->groupBy(['assignment', fn ($item) => $item['scheduled_days']])
->sortBy(fn ($val, $key) => $days[$key]);
}
It will 100% work. It will sort collection from monday to sunday.
$ProductAvailabilities =ProductProductAvailability:get();
$days = [
'monday'=>1,
'tuesday'=>2,
'wednesday'=>3,
'thursday'=>4,
'friday'=>5,
'saturday'=>6,
'sunday'=>7
];
$sortingDays = array();
foreach($days as $day => $value)
{
$sortingDays[] = $ProductAvailabilities->where('day_availability',$day);
}
$ProductAvailabilities=collect($sortingDays)->flatten();

How can I list a recursive array in a select with vuejs and laravel?

I have a recursive array that I want to list in a select ... but I have no idea how to do this dynamically, since the idea is that you can create N number of categories and sub categories
my array
{
"categorias": [
{
"id": 1,
"nombre": "Categoria Padre",
"descripcion": "Descricpion",
"estado": 1,
"prioridad": 1,
"parent_id": null,
"imagen": null,
"created_at": null,
"updated_at": "2019-10-14 14:25:32",
"children_recursive": [
{
"id": 2,
"nombre": "Categoria Hijo 1",
"descripcion": null,
"estado": 1,
"prioridad": 1,
"parent_id": 1,
"imagen": null,
"created_at": null,
"updated_at": "2019-10-14 14:25:20",
"children_recursive": [
{
"id": 4,
"nombre": "Categoria Sub Hijo",
"descripcion": "sdsd",
"estado": 1,
"prioridad": 1,
"parent_id": 2,
"imagen": null,
"created_at": null,
"updated_at": "2019-10-14 14:35:02",
"children_recursive": [
{
"id": 5,
"nombre": "Categoria de Prueba",
"descripcion": "Descripcion de Prueba",
"estado": 0,
"prioridad": null,
"parent_id": 4,
"imagen": null,
"created_at": "2019-10-14 14:34:40",
"updated_at": "2019-10-14 14:34:56",
"children_recursive": [
]
}
]
}
]
},
{
"id": 3,
"nombre": "Catgoria Hijo2",
"descripcion": null,
"estado": 1,
"prioridad": 1,
"parent_id": 1,
"imagen": null,
"created_at": null,
"updated_at": "2019-10-14 14:25:12",
"children_recursive": [
]
}
]
}
]
}
I have a method in which I am receiving this arrar in my controller and then what I do is show it in my select, but the children or subs array do not appear
An example of what I have so far is my select
<select v-model="form.parent_id" name="form.parent_id" id="" class="form-control" :class="{ 'is-invalid': form.errors.has('parent_id') }">
<option value="" selected>Seleccione un padre (Opcional)</option>
<option v-for="c in arrayCategoria" :value="c.id" v-text="c.nombre">
</option>
</select>
my function
selectCategoria(){
let me=this
var url= '/admin/selectCategoria'
axios.get(url)
.then(function (response) {
var respuesta= response.data
console.log(respuesta)
me.nodes = respuesta.categorias
})
.catch(function (error) {
toastr.error(error.response.data.message)
});
}

Laravel\Lumen get eager loading parameter and send to another eager loading

Lumen v 5.7
I have json response like this
{
"data": {
"id": 172,
"name": "Group Test",
"groupmates": [
{
"id": 4555,
"name": "Andri Josua",
"peer_reviews": [
{
"id": 509,
"peer_review_period_id": 17,
"peer_review_setting_id": 15,
"peer_review_setting_item_id": 72,
"student_id": 4555,
"peer_review_setting": {
"id": 15,
"criteria": "Partisipasi",
"partner_id": 6,
"created_at": "2017-07-05T06:44:12.000Z",
"updated_at": "2018-11-28T04:12:37.000Z",
"peer_review_setting_items": [
{
"id": 71,
"peer_review_setting_id": 15,
"partner_id": 6,
"remarks": "Sangat Tidak Baik",
"grade": "0",
"description": "Anggota kelompok,\r\n• tidak berpartisipasi dalam tugas kelompok.\r\n\r\n",
"order_number": 1,
"created_at": "2017-07-05T06:44:12.000Z",
"updated_at": "2017-07-05T06:44:12.000Z"
},
{
"id": 72,
"peer_review_setting_id": 15,
"partner_id": 6,
"remarks": "Tidak Baik",
"grade": "40",
"description": "Anggota kelompok,\r\n • jarang berpartisipasi, • waktu terbuang, atau • mengerjakan materi yang tidak terkait.",
"order_number": 2,
"created_at": "2017-07-05T06:44:12.000Z",
"updated_at": "2017-07-05T06:44:12.000Z"
},
]
}
}
]
}
]
}
}
and controller like this
public function csu($studentId, $klassId, $csuId)
{
$student = Student::findOrFail($studentId);
$contentSectionUnit = ContentSectionUnit::findOrFail($csuId);
$prPeriod = $contentSectionUnit->peer_review_periods->first();
$group = ClassGroup::me($student)
->with(['class_group_students' => function ($query) use ($studentId, $prPeriod) {
$query->with(['peer_reviews' => function ($query) use ($studentId, $prPeriod) {
$query->with(['peer_review_setting' => function ($query) {
$query->with('peer_review_setting_items');
}])
->where('peer_review_period_id', $prPeriod->id)
->where('student_reviewer_id', $studentId)
->orderBy('peer_review_setting_id', 'asc');
}])
->where('student_id', '!=', $studentId)
->orderBy('student_id', 'asc');
}])
->whereKlassId($klassId)
->first();
return new PeerReviewContentResource($group);
}
I want to add new attribute checked in peer_review_setting_items, which value of the attribute is obtained from checking where peer_review_setting_item_id == peer_review_setting_items->id.
How to send peer_review_setting_item_id at peer_review attribute to peer_review_setting_items relation?
Expected result is like this
...
"peer_review_setting_items": [
{
"id": 71,
"peer_review_setting_id": 15,
"partner_id": 6,
"remarks": "Sangat Tidak Baik",
"grade": "0",
"description": "Anggota kelompok,\r\n• tidak berpartisipasi dalam tugas kelompok.\r\n\r\n",
"order_number": 1,
"created_at": "2017-07-05T06:44:12.000Z",
"updated_at": "2017-07-05T06:44:12.000Z",
"checked": false
},
{
"id": 72,
"peer_review_setting_id": 15,
"partner_id": 6,
"remarks": "Tidak Baik",
"grade": "40",
"description": "Anggota kelompok,\r\n • jarang berpartisipasi, • waktu terbuang, atau • mengerjakan materi yang tidak terkait.",
"order_number": 2,
"created_at": "2017-07-05T06:44:12.000Z",
"updated_at": "2017-07-05T06:44:12.000Z",
"checked": true
},
]
...

How can I hidden data in JSON if variable is empty

I got a some trouble in my Laravel application, in Search function, when I do search the result is
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "4232343432",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
{
"id": 3,
"user_id": 16,
"identity_number": "213918273",
"name": "Pekerja_3",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2345234234",
"image": null,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"partner_id": null,
"skill": []
}
]
}
as you can see that "Skill" with id number 3 is empty, I want all in id number 3 is empty also.
My controller is :
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])->whereHas('skill');
And I want something like this :
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2534234234",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
]
}
The point is, if "skill" variable is empty then data not show and vice versa.
Thankyou any help will appreciate, sorry for bad english
You can add a callback to your whereHas function with the same filter as the with function
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])
->whereHas('skill', function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
})
->get();
You can try this $worker = Worker:has('skill')->get();
Only worker that have at least one skill are contained in the collection

Categories