I retrieved data from my database like below and the array response. I am trying to access products and display the pids. How can get this done.
I retrieved data from my database like below and the array response. I am trying to access products and display the pids. How can get this done.
PS: Beginner with laravel. Please help
$amazon_products = Group::where('id',$auth_data)->with('products')->get();
return $amazon_products
[
{
"id":1,
"title":"Shipment",
"id":1,
"created_at":"2018-04-15 23:55:00",
"updated_at":"2018-04-16 00:10:52",
"products":[
{
"id":3,
"name":"MacBook",
"pid":"050",
"group_id":null,
"user_id":1,
"created_at":"2018-04-16 11:06:08",
"updated_at":"2018-04-16 11:06:08",
"pivot":{
"category_id":1,
"product_id":3,
"created_at":"2018-04-16 11:06:08",
"updated_at":"2018-04-16 11:06:08"
}
}
]
},
{
"id":2,
"title":"Gaming",
"id":1,
"created_at":"2018-04-15 23:55:30",
"updated_at":"2018-04-15 23:55:30",
"products":[
{
"id":1,
"name":"Samsung",
"pid":"024",
"group_id":null,
"user_id":1,
"created_at":"2018-04-16 00:14:20",
"updated_at":"2018-04-16 05:31:05",
"pivot":{
"group_id":2,
"customer_id":1,
"created_at":"2018-04-16 05:33:08",
"updated_at":"2018-04-16 05:33:08"
}
}
]
}
]
Below is the model for my group
Group
public function products()
{
return $this->belongsToMany('App\Product','group_product','group_id','product_id')
->withTimestamps();
}
List the product pids after eager loading the products:
...->get()->load('products')->lists('products.pid');
Related
I want to format some column fetched from database but didn't know how. I use Laravel 8.x. Here's the snippet :
GarageController.php
$q = Garage::with('relative')->paginate(15);
return response()->json($q);
the output
{
"data": [
{
"id": 39819,
"name": "john",
"date": "2020-12-20", // i want to format this with date_format()
"relative": {
"rid": 912039,
"rname": "ROV" // and i just want this value instead of all columns
}
},
{
"id": 38178,
"name": "danny",
"date": "2020-12-20", // and this too
"relative": {
"rid": 182738,
"rname": "YIV"
}
}
],
"links": {
.....
},
"meta": {
....
}
}
The model, the garage have is belong to relative (relative could have many garage).
public function relative() {
return $this->belongsTo(Relative::class, 'relative', 'rid');
}
I tried accessor, but it didn't change anything
public function getDateAttributes($value) {
return date_format(date_create($value), 'd/m/Y');
}
Any help would be appreciated. Thank you.
I think you just need to remove s from the getDateAttributes
From
public function getDateAttributes($value) {
return date_format(date_create($value), 'd/m/Y');
}
To
public function getDateAttribute($value) {
return date_format(date_create($value), 'd/m/Y');
}
My model has many options, multiple fields of a single option store values as an array on integers.
I want to filter based on an integer is included.
in php similare to in_array(), but the array is stored in db, and the number is key word.
$productsWithOptions = [
{
"id":2,
"name":"mac 2",
"options":[
{
"id":4,
"price":99.9,
"product_id":2,
"colors":"[4,3,2]",
"sizes":"[5,3,2]"
},
{
"id":5,
"price":99.9,
"product_id":2,
"colors":"[4,4,5]",
"sizes":"[1,2,2]"
},
{
"id":7,
"name":"mac 7",
"options":[
{
"id":19,
"price":99.9,
"product_id":7,
"colors":"[6,3,4]",
"sizes":"[5,2,5]"
},
{
"id":20,
"price":1999.9,
"product_id":7,
"colors":"[1,6,6]",
"sizes":"[5,6,2]"
},
{
"id":21,
"price":1999.9,
"product_id":7,
"colors":"[4,6,5]",
"sizes":"[5,2,2]"
}
]
}
]
$products = Product::whereHas('options', function (Builder $query) use ($arr) {
$query->where('colors','like', "%2%" );
})->get();
This is how options table looks like in db.
I am new to Laravel and I am trying to display data from the controller to view. I am able to display data from the first level of #foreach( $item->name, $item->address, etc) with ease. But I am having difficulty accessing a child of "subscriber_subscription" via Laravel blade convention. I tried accessing it using via code below:
#foreach ($items as $item)
...
#foreach($item->subscriber_subscription as $subs)
...
...
#endforeach
...
...
#endforeach
JSON data used is:
[
{
"id":1,
"name":"Jollibee",
"address":"31st cor. 9th Strata bldg. Ortigas Ave. Pasig City, 600",
"contact":"87000",
"email":"admin#jollibee.com.ph",
"created_at":"2017-09-17 01:04:11",
"updated_at":"2017-09-17 01:04:12",
"subscriber_code":"jollibee",
"subscriber_subscription":[
{
"id":1,
"subscriber_id":1,
"subscription_id":1,
"start_date":"2017-09-17 01:35:27",
"end_date":"2018-09-17 01:35:27",
"created_at":"2017-09-17 01:35:23",
"updated_at":"2017-09-17 01:35:23"
},
{
"id":6,
"subscriber_id":1,
"subscription_id":1,
"start_date":"2017-10-17 01:35:27",
"end_date":"2018-10-17 01:35:27",
"created_at":"2017-09-17 01:35:23",
"updated_at":"2017-09-17 01:35:23"
}
]
},
{
"id":2,
"name":"Bench",
"address":"2207 Tektite Bldg. Ortigas Ave. Pasig City, 1600",
"contact":"+639171234567",
"email":"admin#benchtm.com.ph",
"created_at":"2017-09-17 01:47:10",
"updated_at":"2017-09-17 01:47:11",
"subscriber_code":"bench",
"subscriber_subscription":[
{
"id":4,
"subscriber_id":2,
"subscription_id":1,
"start_date":"2017-09-17 14:19:15",
"end_date":"2018-09-17 14:19:16",
"created_at":"2017-09-17 14:19:20",
"updated_at":"2017-09-17 14:19:20"
},
{
"id":5,
"subscriber_id":2,
"subscription_id":2,
"start_date":"2017-09-17 14:19:15",
"end_date":"2018-09-17 14:19:16",
"created_at":"2017-09-17 14:19:20",
"updated_at":"2017-09-17 14:19:20"
}
]
}
]
I'm getting this error: Invalid argument supplied for foreach()
Any help will be appreciated. Peace :-)
Thanks
I've solved it. Apparently, i have to use $item->SubscriberSubscription which is the name of my model
I Have 2 table that I want to join them and fetch some data like this
I have one student with multiple grade
{
"student": {
"studentID": "2",
"Name": "s1",
"age": " 12",
"grade": [
{
"GradeID": "2"
},{
"GradeID": "3"
}
]
}
I fetch this data from two query in a function in my model and then use json_encode in my controller for my output
but I have this
{
"student": {
"studentID": "2",
"Name": "s1",
"age": " 12"
},
"grade": [
{
"GradeID": "2"
},{
"GradeID": "3"
}
]
}
and now I don't know how to use json_encode for the first format.
thanks
my model(student):
function get_student_id($id)
{
$student['student']=
$this->db->select('tbl_student.*')
->from('tbl_student')
->where('tbl_student.SID',$id)
->get()->row();
$student['grade']=
$this->db->select('tbl_grade.GradeID')
->from('tbl_grade')
->join('tbl_sudent','tbl_grade.StudentID=tbl_sudent.SID')
->where('tbl_student.SID',$id)
->get()->result();
return $student;
}
my controller:
public function get_student_id()
{
$id = $input['one'];
$this->load->model('student');
$temp=$this->student->get_student_id($id);
$output= json_encode($temp);
die($output);
}
You just have to structure the array you're returning from the model correctly. You're putting everything inside two subarrays called student and grade, which are inside the outer array student. Try this:
my model(student):
function get_student_id($id)
{
$student=
$this->db->select('tbl_student.*')
->from('tbl_student')
->where('tbl_student.SID',$id)
->get()->row();
$student['grade']=
$this->db->select('tbl_grade.GradeID')
->from('tbl_grade')
->join('tbl_sudent','tbl_grade.StudentID=tbl_sudent.SID')
->where('tbl_student.SID',$id)
->get()->result();
return $student;
}
I'm not totally certain you want to call get->row() on the first query, if that doesn't work try get->row_array()
I have the following json:
{
"Companies":[
{
"CompanyPersonTask":{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
},
"Company":{
"id":"502d1844-3638-44dc-824c-14f02e0cc009",
"name":"mostro",
"Office":[
{
"id":"502d1844-b90c-44f5-84c5-14f02e0cc009",
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"region":"Emilia-Romagna",
"city":"Rozzemilia",
"address":"-",
"phone":"-",
"legal_office":true
}
],
"CompanyPersonTask":[
{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
}
]
},
"Person":{
"id":"2",
"first_name":"Carlo",
"last_name":"Giusti",
"home_address":"Russi",
"job_address":null,
"phone":null,
"fax":null,
"mail":null,
"full_name":"Giusti Carlo",
"OfficePersonTask":[
],
"CompanyPersonTask":[
{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
}
]
},
"Task":{
"id":"1",
"short_name":"Proprietario",
"full_name":null,
"info":null,
"OfficePersonTask":[
],
"CompanyPersonTask":[
{
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"person_id":"2",
"task_id":"1"
}
]
}
}
]
}
That is created by this method, inside CompanyPersonTask model:
public function getCompaniesByRegion($region){
$this->recursive = 2;
return $this->find('all');
}
but I want my json to be formatted like this:
{
"Companies":[
{
"Company":{
"id":"502d1844-3638-44dc-824c-14f02e0cc009",
"name":"mostro",
"Office":[
{
"id":"502d1844-b90c-44f5-84c5-14f02e0cc009",
"company_id":"502d1844-3638-44dc-824c-14f02e0cc009",
"region":"Emilia-Romagna",
"city":"Rozzemilia",
"address":"-",
"phone":"-",
"legal_office":true
}
]
},
"Person":{
"id":"2",
"first_name":"Carlo",
"last_name":"Giusti",
"home_address":"Russi",
"job_address":null,
"phone":null,
"fax":null,
"mail":null,
"full_name":"Giusti Carlo"
}
}
]
}
How can I modify my method?
Use Containable behavior to filter result data.
For more explanation refer this link.
You can check unbindModel in cake php to remove the relation on the fly. This will prevent the data from the unwanted table. Check this.
<?php
$this->Model->unbindModel(
array('associationType' => array('associatedModelClassName'))
);