I need to show array of an array - php

I'm developing a CMS, which uses yaml files to manipulate the theme. My problem is that I need to list the Submenus of a Menu. I access for example the Header menu where I can change the contents of it, but in that Header has submenus, I just want to list them.
I have a service that does a search
Since the data is an array, how do I print it on the screen?
public function query() {
/* #var $request Request */
$request = app('request');
/* #var $website Site */
$website = $request->route('website');
$menu = $request->route('submenu');
// Carega configurações do site
$this->service->loadWebsite($website->slug);
/*
- title: menu1
submenu:
- title: submenu 1.1
- title: submenu 1.2
- title: menu2
submenu:
- title: submenu 2.1
*/
/*
- title: menu1
- title: - submenu 1.1
- title: - submenu 1.2
- title: menu2
- title: - submenu 2.1
*/
// puxa os menus da configuração do site
$menus = $this->service->getWebsiteConfig($website->slug, 'menu.' . $menu . '.menu');
dd($menus);
return $menus;
}
SERVICE
protected function toWebisteMenuItemCollection(string $menu, array $rows) {
return collect($rows)->map(function (array $data, string $key) use ($menu) {
$data['id'] = $menu . '.' . $key;
if(isset($data['submenu']) && is_array($data['submenu'])) {
$data['submenu'] = $this->toWebisteMenuItemCollection($data['id'], $data['submenu']);
}
return new WebsiteMenuItem($data);
});
}
dd($menu)
#attributes: array:3 [
"title" => "Consórcio"
"submenu" => Collection {#647
#items: array:3 [
0 => WebsiteMenuItem {#642
#keyType: "string"
#fillable: array:7 [
0 => "id"
1 => "title"
2 => "label"
3 => "imagem"
4 => "website_image"
5 => "icons"
6 => "submenu"
]
#connection: null
#table: null
#primaryKey: "id"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:2 [
"title" => "Planos de Consórcio"
"id" => "header_submenu_menu.2.0"
]
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
1 => WebsiteMenuItem {#645
#keyType: "string"
#fillable: array:7 [
0 => "id"
1 => "title"
2 => "label"
3 => "imagem"
4 => "website_image"
5 => "icons"
6 => "submenu"
]
#connection: null
#table: null
#primaryKey: "id"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:2 [
"title" => "Portal do Consorciado"
"id" => "header_submenu_menu.2.1"
]
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
2 => WebsiteMenuItem {#646
#keyType: "string"
#fillable: array:7 [
0 => "id"
1 => "title"
2 => "label"
3 => "imagem"
4 => "website_image"
5 => "icons"
6 => "submenu"
]
#connection: null
#table: null
#primaryKey: "id"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:2 [
"title" => "Como Funciona o Consórcio"
"id" => "header_submenu_menu.2.2"
]
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
]
}

var_dump($array_youd_like_to_print) will give you all the contents (even nested) of an array.
You can either use this as is, if it solves your problem, or use it to find where the value is that you are looking for (and how to access it in the array) and print that.

Related

Laravel fetch all the values of a column

The user_enabled_notifications table has 2 rows of data. i wanted to fetch all the values in the id column.
$notificationData = UserEnabledNotifications::all();
dump($notificationData['id']); shows Undefined index: id
dump($notificationData->id); shows Property [id] does not exist on this collection instance
dump($notificationData[0]['id']); shows only 1 id. What else shall i try to fetch all the id column values in a single stretch.
However, dump($notificationData); shows the complete data in table as given below.
Illuminate\Database\Eloquent\Collection {#337
#items: array:4 [
0 => App\Models\UserEnabledNotifications {#338
#table: "user_enabled_notifications"
#fillable: array:3 [
0 => "userId"
1 => "notificationTypesId"
2 => "status"
]
#connection: "pgsql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [
"id" => 1
"userId" => 1
"notificationTypesId" => 1
"status" => true
"deleted_at" => null
"created_at" => null
"updated_at" => null
]
#original: array:7 [
"id" => 1
"userId" => 1
"notificationTypesId" => 1
"status" => true
"deleted_at" => null
"created_at" => null
"updated_at" => null
]
#changes: []
#casts: array:1 [
"deleted_at" => "datetime"
]
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
#forceDeleting: false
#enableLoggingModelsEvents: true
#oldAttributes: []
}
1 => App\Models\UserEnabledNotifications {#339
#table: "user_enabled_notifications"
#fillable: array:3 [
0 => "userId"
1 => "notificationTypesId"
2 => "status"
]
#connection: "pgsql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [
"id" => 2
"userId" => 1
"notificationTypesId" => 2
"status" => true
"deleted_at" => null
"created_at" => null
"updated_at" => null
]
#original: array:7 [
"id" => 2
"userId" => 1
"notificationTypesId" => 2
"status" => true
"deleted_at" => null
"created_at" => null
"updated_at" => null
]
#changes: []
#casts: array:1 [
"deleted_at" => "datetime"
]
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
#forceDeleting: false
#enableLoggingModelsEvents: true
#oldAttributes: []
}
The $notificationData is an object of type Illuminate\Database\Eloquent\Collection, a collection. If you want to get the id of individual elements. Pls do the followings:
foreach ($notificationData as $notification) {
dump($notification->id);
}
// Or
$notificationData->pluck('id')->toArray();
Try this
// get your main collection with all the attributes...
$notificationData = UserEnabledNotifications::get();
// build your second collection with a subset of attributes. this new
// collection will be a collection of plain arrays, not UserEnabledNotifications models.
$subset = $notificationData->map(function ($notification) {
return collect($notification->toArray())
->only(['id'])
->all();
});
dd($subset);
OR
$notificationData = $notificationData->pluck('id')->toArray();
dd($notificationData);
all() gives you an collection (similar to array), so it has index (0 and positive integers) for keys
use pluck() to get all specific value for a key
$notificationData = UserEnabledNotifications::pluck('id');
print($notificationData) // [1, 2, 3, 4, 5, 6, ...]

How to load data from controller to view using ajax call in laravel?

I want to get all products by category when clicked on the category name. I get data from the database but couldn't load that in view using ajax.
My controller code
public function getCategoryItems($id) {
$category = FoodItemCategory::find($id);
$products = $category->foods()->get();
$data = [];
$image = [];
$prices = [];
foreach ($products as $product) {
foreach ($product->price as $price) {
$data[] = $product->name;
$image[] = $product->image;
$prices[] = $price->discounted_price;
}
}
return response()->json(['data' => $data, 'image' => $image, 'price' => $prices]);
}
In ajax success response, I'm trying this way
success: function(response) {
if (response) {
$.each(response.data, function(key, value) {
$.each(response.image, function(key, image) {
// console.log(response.products);
// var href = window.location.pathname;
var href = "{{ route('home') }}/";
$("#category").append(
"<div class='col-md-11 col-lg-10 col-xl-6 menu-holder left fixed'><a href='' class='menu-thumb'>
<img src='" + href + "storage/items/food/" + image +"' height='80' width='80'></a>" +
"<div class='menu-item'><h5 class='color-fff'><a href=''></a>
<span class='dots'>" +value +"</span>
<span class='price'><span> ৳ </span>21</span></h5>
<ul>
<li>" +"<a href=''>" + value +"</a></li>
</ul>
</div>
</div>"
);
});
});
here is the console.log(response) screenshot. See this image of what I get in the console
[first click] 1
2nd click
but it shows the same data twice. Can anyone tell me the right way to view this data using ajax?
Here is the dd($products)
Illuminate\Database\Eloquent\Collection {#1252 ▼
#items: array:2 [▼
0 => App\Models\FoodItem {#1308 ▼
#guarded: []
#connection: "mysql"
#table: "food_items"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 25
"name" => "Burger"
"image" => "bg1.jpg"
"is_visible" => 1
"is_available" => 1
"created_at" => "2021-02-07 03:19:39"
"updated_at" => "2021-02-07 03:19:39"
]
#original: array:11 [▼
"id" => 25
"name" => "Burger"
"image" => "bg1.jpg"
"is_visible" => 1
"is_available" => 1
"created_at" => "2021-02-07 03:19:39"
"updated_at" => "2021-02-07 03:19:39"
"pivot_food_item_category_id" => 44
"pivot_food_item_id" => 25
"pivot_created_at" => "2021-02-07 03:19:39"
"pivot_updated_at" => "2021-02-07 03:19:39"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#1098 ▼
+incrementing: false
#guarded: []
#connection: "mysql"
#table: "food_items_have_categories"
#primaryKey: "id"
#keyType: "int"
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [▼
"food_item_category_id" => 44
"food_item_id" => 25
"created_at" => "2021-02-07 03:19:39"
"updated_at" => "2021-02-07 03:19:39"
]
#original: array:4 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
+pivotParent: App\Models\FoodItemCategory {#1163 ▶}
#foreignKey: "food_item_category_id"
#relatedKey: "food_item_id"
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
1 => App\Models\FoodItem {#1059 ▶}
]
}

Why are not appearing all conferences that belong to the clicked category? ( Column not found: 1054 Unknown column)

I have a list that shows some categories:
<ul class="Categories__Menu">
#foreach($categories->take(6) as $category)
<li class="active">
{{$category->name}}
</li>
#endforeach
<li><a data-toggle="modal" data-target=".bd-example-modal-lg" href="">More <i class="fa fa-caret-down" aria-hidden="true"></i></a></li>
</ul>
When a user clicks in a category is done an ajax reqquest to get the conferences that belong to that clicked category. For example when the user clicks in the category "IT" that has 2 conferences in the database that have the category "IT" instead of appearing 2 conferences in the view in the network tab with " dd($conferences);" it appears:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.conference_id' in 'where clause' (SQL: select * from conferences where exists (select * from categories where conferences.id = categories.conference_id and category_conference.id = 2))".
Route:
Route::get('conferences/where/category/{id}','ConferenceController#WhereHasCategory')->name('category.conferences');
ConferenceController method:
public function WhereHasCategory(Request $request)
{
$conferences = Conference::whereHas('categories', function ($categories) use (&$request) {
$categories->where('category_conference.id',$request->id);
})->get();
//dd($conferences);
return response()->json($conferences);
}
Ajax:
$("a[name='category']").on('click', function(){
var category_id = $(this).attr("id");
$('.Categories__Menu li').removeClass('ative');
$(this).parent('li').addClass('ative');
$.ajax({
url: '{{ route('category.conferences',null) }}/' + category_id,
type: 'GET',
success:function(result){
$('#conferences').empty();
var newConferences='';
var placeholder = "{{route('conferences.show', ['id' => '1', 'slug' => 'demo-slug'])}}";
$.each(result, function(index, conference) {
var url = placeholder.replace(1, conference.id).replace('demo-slug', conference.slug);
newConferences += '<div class="col-12 col-sm-6 col-lg-4 col-xl-3 mb-4">\n' +
' <div class="card box-shaddow">\n' +
' <img class="card-img-top" src='+ conference.image +' alt="Card image cap">\n' +
' <div class="card-body">\n' +
' <p class="font-size-sm"><i class="fa fa-calendar" aria-hidden="true"></i> '+conference.start_date+'</p>\n' +
' <h5 class="card-title h6 font-weight-bold text-heading-blue">'+conference.name+'</h5>\n' +
' <p class="card-text font-size-sm"><i class="fa fa-map-marker" aria-hidden="true"></i> '+conference.place+', '+conference.city+'</p>\n' +
' </div>\n' +
' <div class="card-footer d-flex justify-content-between align-items-center">\n' +
' More' +
' <span class="font-weight-bold font-size-sm text-heading-blue"> </span>\n'+
' </div>\n' +
' </div></div>';
});
$('#conferences').html(newConferenes);
},
error: function(error) {
console.log(error.status)
}
});
});
For example, in the database there are 5 conferences that have category_id "2". And when the category with id 2 is clicked the result is what is in the question with only one result. The pivot table category_conference has:
id conference_id category_id
1 1 1
2 7 2
3 8 2
4 9 2
5 10 2
...
11 16 2
Models:
class Conference extends Model{
public function categories(){
return $this->belongsToMany('App\Category');
}
}
class Category extends Model
{
public function conferences(){
return $this->belongsToMany('App\Conference');
}
}
The dd(Category::find(1)->conferences) shows:
Collection {#269
#items: array:2 [
0 => Conference {#273
#fillable: array:18 [
0 => "name"
...
]
#dates: array:2 [
0 => "start_date"
1 => "end_date"
]
#appends: array:1 [
0 => "price_range"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [
"id" => 1
"name" => "Conference title test"
...
]
#original: array:25 [
"id" => 1
"name" => "Conference title test"
...
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#262
+pivotParent: Category {#268
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [
"id" => 1
"name" => "Category1"
...
]
#original: array:5 [
"id" => 1
"name" => "Category1"
...
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"conferences" => Collection {#269}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
#foreignKey: "category_id"
#relatedKey: "confenrece_id"
#guarded: []
#connection: "mysql"
#table: "category_conference"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"category_id" => 1
"conference_id" => 1
]
#original: array:2 [
"category_id" => 1
"conference_id" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
1 => Conference {#270
#fillable: array:18 [
0 => "name"
1 => "description"
]
#dates: array:2 [
0 => "start_date"
1 => "end_date"
]
#appends: array:1 [
0 => "price_range"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [
"id" => 2
"name" => "conference test title 2"
...
]
#original: array:25 [
"id" => 2
"name" => "conference test title 2"
...
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#267
+pivotParent: Category {#268}
#foreignKey: "category_id"
#relatedKey: "conference_id"
#guarded: []
#connection: "mysql"
#table: "category_conference"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"category_id" => 1
"conference_id" => 2
]
#original: array:2 [
"category_id" => 1
"conference_id" => 2
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
]
}
Just get the conferences from the category:
$conferences = Category::find($request->id)->conferences;
As Farrukh Ayyaz suggested, route model binding is more elegant:
Route::get('conferences/where/category/{category}', ...
public function WhereHasCategory(Category $category)
{
return response()->json($category->conferences);
}

How to Foreach loop through Laravel collection.?

i m a beginner at laravel. using 5.5 . i have this collection return by my controller :
$products = Product::with('bazar.resellers')->take(2)->get();
dd($products);
//return view('shop.index')->with('products', $products);
which basically is three tables with nested relations. Note the relations lists in the code below. i want to access data(all columns in red colour) from every model i have in the collection i.e. Product, bazar and reseller.
Relationships are quite fine. but how to retrieve it from a collection? i dunno how foreach loops play with a collection.
Collection {#578 ▼
#items: array:2 [▼
0 => Product {#487 ▼
#fillable: array:7 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 1
"created_at" => "2018-04-27 12:54:41"
"updated_at" => "2018-04-27 12:54:41"
"imgp" => "shirt.jpg"
"title" => "shirt1"
"Prod_descript" => "shirt1 is a good shirt"
"price" => 10
"reseller_id" => 1
"City_id" => 1
"bazar_id" => 1
]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"bazar" => Bazar {#523 ▼
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 1
"Bazarname" => "Saddar"
"Bazarlat" => null
"Bazarlong" => null
"created_at" => null
"updated_at" => null
"City_id" => 1
]
#original: array:7 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"resellers" => Collection {#574 ▼
#items: array:1 [▼
0 => Reseller {#564 ▼
#guard: "reseller"
#fillable: array:12 [▶]
#hidden: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:15 [▼
"id" => 1
"Fname" => "talha"
"Lname" => "ali"
"email" => "p#g.com"
"password" => "$2y$10$M7wOmSouxSAYADN7NeFdSObT8fGwkEOFxVmOgcNSWvrixWCDtA/1S"
"mobile_no" => "03169880008"
"landline_no" => "0987654"
"shop_name" => "MyTestSHop"
"NIC_no" => "170178359437589754"
"shop_address" => "University Town, Peshawar, Pakistan"
"remember_token" => "wrb3nOwOWQcTuoF0P9KnaknwpOxfpTHt4gkShUmTzkR2Df9A7pPY5shBq6pQ"
"created_at" => "2018-04-27 12:42:25"
"updated_at" => "2018-04-27 12:42:25"
"City_id" => 1
"bazar_id" => 1
]
#original: array:15 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▼
0 => "*"
]
}
1 => Product {#488 ▶}
]
}
the code i have tried is not working!
#foreach($products as $product)
<h1>{{ $product->title }}</h1>
#foreach($product->bazar as $bazar)
<h3>{{ $bazar->Bazarname }}</h3>
#foreach($bazar->resellers as $reseller)
<p>{{ $reseller->Fname }}</p>
#endforeach
#endforeach
#endforeach
If product has single bazar,then you don't need to use foreach on $product->bazar. You just need to use bazar property directly. and if product has many bazars, then update your product model bazar() method like:
public function bazars()
{
return $this->hasMany(Bazar::class);
}
It returns collection of bazars and then you can run loop on that.

Using select on eloquent model causes "Undefined offset:0"

I am new to laravel. I have written code like below. And when I add select, it causes an error saying undefined offset: 0.
It also causes same error when I have more than two records even though I commented out select part.
When I check the query using toSql(), it is perfectly fine.
So when I use dd() on $clientDrivers before return, the output is below. (Seems fine as well)
What would be the problem with my code?
Any suggestion or advice would be appreicated.
EDIT: I have found the problem which was the $appends in Driver Model. Why does $appends cause the problem?
Solution: I have fixed my Driver Model getBankAttribute which is used for $appends and it works fine.
Here is my code:
Model
public function drivers() {
return $this->belongsToMany('App\Model\User\Driver', 'ClientDriver', 'client_id', 'userdriver_id');
}
Controller
$client = ClientModel::findOrFail($id);
$select = ['UserDriver.name as userdriver_name', 'UserDriver.phone_number as userdriver_phone_number'];
$clientDrivers = $client->drivers()
->select($select) // this does not work. If this is commented out it works perfectly fine.
->get();
return response($clientDrivers, 200);
Result of dd($clientDrivers)
Collection {#805
#items: array:2 [
0 => Driver {#798
#dates: array:1 [
0 => "deleted_time"
]
#table: "UserDriver"
+timestamps: false
+appends: array:1 [
0 => "bank"
]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"userdriver_name" => "driver1"
"userdriver_phone_number" => "140412351235"
]
#original: array:4 [
"userdriver_name" => "driver1"
"userdriver_phone_number" => "140412351235"
"pivot_client_id" => 1
"pivot_userdriver_id" => 1
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#801
+pivotParent: Client {#713
#table: "Client"
#guarded: array:2 [
0 => "id"
1 => "created_time"
]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [
"id" => 1
"created_time" => "2017-12-28 05:23:50"
"invoice_email" => ""
"biz_number" => "1234512345"
]
#original: array:20 [
"id" => 1
"created_time" => "2017-12-28 05:23:50"
"invoice_email" => ""
"biz_number" => "1234512345"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#forceDeleting: false
}
#foreignKey: "client_id"
#relatedKey: "userdriver_id"
#guarded: []
#connection: "mysql"
#table: "ClientDriver"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"client_id" => 1
"userdriver_id" => 1
]
#original: array:2 [
"client_id" => 1
"userdriver_id" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
1 => Driver {#799
#dates: array:1 [
0 => "deleted_time"
]
#table: "UserDriver"
+timestamps: false
+appends: array:1 [
0 => "bank"
]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"userdriver_name" => "driver2"
"userdriver_phone_number" => "140412351236"
]
#original: array:4 [
"userdriver_name" => "driver2"
"userdriver_phone_number" => "140412351236"
"pivot_client_id" => 1
"pivot_userdriver_id" => 2
]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"pivot" => Pivot {#803
+pivotParent: Client {#713}
#foreignKey: "client_id"
#relatedKey: "userdriver_id"
#guarded: []
#connection: "mysql"
#table: "ClientDriver"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"client_id" => 1
"userdriver_id" => 2
]
#original: array:2 [
"client_id" => 1
"userdriver_id" => 2
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
}
]
}
You need to pass string in $select. You are currently passing array in it.
Change your $select as below:
$select = "'UserDriver.name as userdriver_name', 'UserDriver.phone_number as userdriver_phone_number'";
You could use with() to get associated drivers for a client
$client = ClientModel::where('id',$id)
->with('drivers:name,phone_number')
->get();
$clientDrivers = $client->drivers;
return response($clientDrivers, 200);

Categories