Hello i have a table with 10 rows in total where 6 have ID 1 and 4 have ID 2 i want to count only the rows with ID 1 but my code counts all of them in total. This is my code:
$clientOrder = clientHasOrder::where('order_id', $id)->firstOrFail();
$rowCount = $clientOrder->count();
dd($rowCount);
If the code worked correct i should get 6 rows with specific $id but this code gives 10 rows that are total in the table.
If i do dd(clientHasOrder::all()); i get:
This is the result in details as you can see i get the 10 results but i need only 6 rows with specific ID
Illuminate\Database\Eloquent\Collection {#1367 ▼ // app/Http/Controllers/OrderController.php:121
#items: array:11 [▼
0 => App\Models\clientHasOrder {#1369 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "36"
"product_id" => 1
"amount" => "10"
]
#original: array:3 [▼
"order_id" => "36"
"product_id" => 1
"amount" => "10"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
1 => App\Models\clientHasOrder {#1370 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "36"
"product_id" => 2
"amount" => "20"
]
#original: array:3 [▼
"order_id" => "36"
"product_id" => 2
"amount" => "20"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
2 => App\Models\clientHasOrder {#1371 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "36"
"product_id" => 3
"amount" => "30"
]
#original: array:3 [▼
"order_id" => "36"
"product_id" => 3
"amount" => "30"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
3 => App\Models\clientHasOrder {#1372 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "36"
"product_id" => 4
"amount" => "40"
]
#original: array:3 [▼
"order_id" => "36"
"product_id" => 4
"amount" => "40"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
4 => App\Models\clientHasOrder {#1373 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "37"
"product_id" => 7
"amount" => "10"
]
#original: array:3 [▼
"order_id" => "37"
"product_id" => 7
"amount" => "10"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
5 => App\Models\clientHasOrder {#1374 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "37"
"product_id" => 8
"amount" => "20"
]
#original: array:3 [▼
"order_id" => "37"
"product_id" => 8
"amount" => "20"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
6 => App\Models\clientHasOrder {#1375 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "37"
"product_id" => 9
"amount" => "30"
]
#original: array:3 [▼
"order_id" => "37"
"product_id" => 9
"amount" => "30"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
7 => App\Models\clientHasOrder {#1376 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "37"
"product_id" => 10
"amount" => "40"
]
#original: array:3 [▼
"order_id" => "37"
"product_id" => 10
"amount" => "40"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
8 => App\Models\clientHasOrder {#1377 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "38"
"product_id" => 1
"amount" => "10"
]
#original: array:3 [▼
"order_id" => "38"
"product_id" => 1
"amount" => "10"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
9 => App\Models\clientHasOrder {#1378 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "38"
"product_id" => 7
"amount" => "20"
]
#original: array:3 [▼
"order_id" => "38"
"product_id" => 7
"amount" => "20"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▶]
}
10 => App\Models\clientHasOrder {#1379 ▼
#connection: "mysql"
#table: "client_has_orders"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:3 [▼
"order_id" => "38"
"product_id" => 9
"amount" => "30"
]
#original: array:3 [▼
"order_id" => "38"
"product_id" => 9
"amount" => "30"
]
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: array:3 [▼
0 => "order_id"
1 => "product_id"
2 => "amount"
]
#guarded: array:1 [▼
0 => "*"
]
}
]
#escapeWhenCastingToString: false
}
You can invoke count directly on the query, like this:
$clientOrder = clientHasOrder::where('order_id', $id)->count();
dd($clientOrder);
And that will show the correct amount, you do not need to firstOrFail, because if the results are empty, it will show 0 count.
Related
Here is my Session and i would like to fetch the value from Sample_table to get the original array value how can i do that ?
{{ dd(Session::get('sample')[0]) }}
Sample_table{#266 ▼
#fillable: array:6 [▶]
#table: "Sample_table"
#primaryKey: "id"
+timestamps: true
#connection: "mysql"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:20 [▶]
#original: array:20 [▼
"id" => "9"
"firstName" => "test"
"lastName" => "asdf"
"middleName" => "test"
"contactNo" => "2147483647"
"emailAddress" => "test#gmail.com"
"residenceAddress" => "afsf"
"province_id" => "6"
"city_id" => "3"
"barangay_id" => "16982"
"copyMailing" => null
"mailingAddress" => null
"mailing_province_id" => "6"
"mailing_city_id" => "3"
"mailing_barangay_id" => "16982"
"status" => "0"
"userIdentity" => "0"
"global_id" => "0"
"updated_at" => "0000-00-00 00:00:00"
"created_at" => "0000-00-00 00:00:00"
]
You can use ->getRawOriginal():
Session::get('sample')[0]->getRawOriginal();
Today I'm facing some very weird behavior in Laravel while fetching records from the table. I'm having a table which has One to Many Relation with itself. A Menu item can have multiple Submenu. Below is a brief structure:
Table name: site_navigation
id => Primary Key, Auto Increment
parent_id => Index, Defaults Null
menu_name => Varchar
Model => 'Sitemenu_model.php'
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Sitemenu_model extends Model
{
protected $table = 'site_navigation';
public function getParent()
{
return $this->belongsTo('App\Models\backend\Sitemenu_model', 'parent_id');
}
public function getChildren()
{
return $this->hasMany('App\Models\backend\Sitemenu_model', 'parent_id');
}
}
Controller => 'Pagescontent.php'
use App\Models\backend\Sitemenu_model;
class Pagescontent extends Controller
{
$collSiteMenu = Sitemenu_model::with('getChildren')
->orderBy('display_order', 'asc')
->get();
return view('backend.pages.pagescontent.create', ['collSiteMenu'=>$collSiteMenu]);
}
View => 'create.blade.php'
#foreach($collSiteMenu as $menu)
#php
$arrMainMenu[$menu->id] = $menu->menu_name;
#endphp
#endforeach
The '$arrMainMenu' is only having the last record of the collection. When i tried to print $menu->id, it prints blank. Even the child record doesn't print its menu id. Even if i use below code, still no output. Which means 'id' attribute is not set.
#foreach($collSiteMenu as $menu)
#isset($menu->id)
{{ 'menu id is set to '. $menu->id }}
#endisset
#endforeach
However, in controller, when i dd($collSiteMenu) it prints below:
Collection {#380 ▼
#items: array:12 [▼
0 => Sitemenu_model {#393 ▼
#table: "site_navigation"
#fillable: array:10 [▶]
+id: null
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▼
"id" => 1
"menu_name" => "About Us"
"parent_id" => null
"url_slug" => "about-us"
"canonical_url" => null
"show_in_nav" => "show_both"
"meta_title" => "About Us"
"meta_keywords" => null
"meta_description" => "this is test description"
"is_active" => "yes"
"display_order" => 1
"created_at" => "2020-09-02 12:39:20"
"updated_at" => "2020-09-04 16:21:51"
]
#original: array:13 [▼
"id" => 1
"menu_name" => "About Us"
"parent_id" => null
"url_slug" => "about-us"
"canonical_url" => null
"show_in_nav" => "show_both"
"meta_title" => "About Us"
"meta_keywords" => null
"meta_description" => "this is test description"
"is_active" => "yes"
"display_order" => 1
"created_at" => "2020-09-02 12:39:20"
"updated_at" => "2020-09-04 16:21:51"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"getChildren" => Collection {#411 ▼
#items: array:2 [▼
0 => Sitemenu_model {#415 ▼
#table: "site_navigation"
#fillable: array:10 [▶]
+id: null
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▶]
#original: array:13 [▼
"id" => 7
"menu_name" => "Who We Are"
"parent_id" => 1
"url_slug" => "who-we-are"
"canonical_url" => null
"show_in_nav" => null
"meta_title" => "Who We Are"
"meta_keywords" => null
"meta_description" => null
"is_active" => "yes"
"display_order" => 1
"created_at" => "2020-09-02 14:14:03"
"updated_at" => "2020-09-02 14:14:03"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Sitemenu_model {#416 ▼
#table: "site_navigation"
#fillable: array:10 [▶]
+id: null
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:13 [▶]
#original: array:13 [▼
"id" => 8
"menu_name" => "Our Cause"
"parent_id" => 1
"url_slug" => "our-cause"
"canonical_url" => null
"show_in_nav" => null
"meta_title" => "Our Cause"
"meta_keywords" => null
"meta_description" => null
"is_active" => "yes"
"display_order" => 2
"created_at" => "2020-09-02 14:15:27"
"updated_at" => "2020-09-02 14:15:27"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
I have searched on Google but couldn't find a solution to this. Can any one guide me as where I'm going wrong.
Much Regards,
Javed
I'm writing tests in PhpStorm. So when I dd(...) something ( or ->dump() it), then it doesn't automatically 'fold' up the dumped content, like in the browser. Therefore it takes longer to find the lines I'm looking for, when debugging tests (which is all the time).
How do I make a debugging-function that leaves out lines where I've put four slashes (////) in the end? ... Or does something like this already exist?
App\Team {#1631
#table: "teams"
#fillable: array:7 [ ////
0 => "id" ////
1 => "created_at" ////
2 => "updated_at" ////
3 => "deleted_at" ////
4 => "name" ////
5 => "sport_id" ////
6 => "type" ////
] ////
#connection: "sqlite" ////
#primaryKey: "id" ////
#keyType: "int" ////
+incrementing: true ////
#with: [] ////
#withCount: [] ////
#perPage: 15 ////
+exists: true ////
+wasRecentlyCreated: false ////
#attributes: array:7 [
"id" => "1"
"created_at" => "2020-07-28 21:36:40"
"updated_at" => "2020-07-28 21:36:40"
"name" => "Test team name"
"sport_id" => null
"type" => "team"
"deleted_at" => null
]
#original: array:10 [ ////
"id" => "1" ////
"created_at" => "2020-07-28 21:36:40" ////
"updated_at" => "2020-07-28 21:36:40" ////
"name" => "Test team name" ////
"sport_id" => null ////
"type" => "team" ////
"deleted_at" => null ////
"pivot_user_id" => "4" ////
"pivot_team_id" => "1" ////
"pivot_role_id" => "6" ////
]
#changes: [] ////
#casts: [] ////
#classCastCache: [] ////
#dates: array:1 [ ////
0 => "deleted_at" ////
] ////
#dateFormat: null ////
#appends: [] ////
#dispatchesEvents: [] ////
#observables: [] ////
#relations: array:1 [
"pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#52
+incrementing: false ////
#guarded: [] ////
#connection: "sqlite" ////
#table: "role_team_user"
#primaryKey: "id" ////
#keyType: "int" ////
#with: [] ////
#withCount: [] ////
#perPage: 15 ////
+exists: true ////
+wasRecentlyCreated: false
#attributes: array:3 [
"user_id" => "4"
"team_id" => "1"
"role_id" => "6"
]
#original: array:3 [ ////
"user_id" => "4" ////
"team_id" => "1" ////
"role_id" => "6" ////
] ////
#changes: [] ////
#casts: [] ////
#classCastCache: [] ////
#dates: [] ////
#dateFormat: null ////
#appends: [] ////
#dispatchesEvents: [] ////
#observables: [] ////
#relations: [] ////
#touches: [] ////
+timestamps: false ////
#hidden: [] ////
#visible: [] ////
#fillable: [] ////
+pivotParent: App\User {#56
#fillable: array:7 [
0 => "name"
1 => "email"
2 => "password"
...
...
...
Ideally (in my head), I would add a debugging function somewhere, that extends dd(), that is called ddd(), that I can customize for when writing tests. But I'm open for suggestions that achieves anything in the same ballpark.
I am retrieving entities from a database, however I do have pivot table that links users and entities and for each entity I want to retrieve a user, the problem is I don't know how to use a collection inside a laravel controller function:
public function handle()
{
$dir = "public/ical/*";
foreach(glob($dir) as $file)
{
$entity_id = preg_match("/\d/", $file);
$entity = Entity::where('id', $entity_id)
->with('users')
->first();
dd($entity->users->id); //I want to retrieve that.
$icals = new ICal($file, array(
'defaultSpan' => 2,
'defaultTimeZone' => 'UTC',
'defaultWeekStart' => 'MO',
'skipRecurrence' => false,
'useTimeZoneWithRRules' => false,
));
$time_period = array(
'interval' => true,
);
if ($time_period['interval']) {
$events = $icals->eventsFromInterval('2 week');
$count = 1;
}
foreach ($events as $event) {
$dtstart = $icals->iCalDateToDateTime($event->dtstart);
$dtend = $icals->iCalDateToDateTime($event->dtend);
$dtstart_tz = $icals->iCalDateToDateTime($event->dtstart_tz);
$dtend_tz = $icals->iCalDateToDateTime($event->dtend_tz);
$database_event = new Event;
$database_event->type = "External Event";
$database_event->startdate = $dtstart->format('d-m-Y');
$database_event->endate = $dtend->format('d-m-Y');
$database_event->startime = $dtstart_tz->format('H:i');
$database_event->endtime = $dtend_tz->format('H:i');
$database_event->title = $event->summary;
$database_event->description = $event->description ;
$database_event->location = $event->location;
$database_event->entity_id = $entity_id;
$database_event->save();
$this->info("Event Retrieved");
$count++;
}
data dump dd($entity) gives:
App\Entity {#620
#table: "entities"
+timestamps: true
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [
"id" => 1
"name" => "dasd"
"type" => "dsa"
"email" => "adas"
"logo" => null
"twitter_business" => null
"facebook_business" => null
"instagram_business" => null
"google_places" => null
"ical" => null
"slug" => null
"api_key" => null
"geoloc_id" => null
"deleted" => 0
"created_at" => null
"updated_at" => null
]
#original: array:16 [
"id" => 1
"name" => "dasd"
"type" => "dsa"
"email" => "adas"
"logo" => null
"twitter_business" => null
"facebook_business" => null
"instagram_business" => null
"google_places" => null
"ical" => null
"slug" => null
"api_key" => null
"geoloc_id" => null
"deleted" => 0
"created_at" => null
"updated_at" => null
]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:1 [
"users" => Illuminate\Database\Eloquent\Collection {#622
#items: array:2 [
0 => App\Entity {#630
#table: "entities"
+timestamps: true
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [
"id" => 1
"name" => "dasd"
"type" => "dsa"
"email" => "adas"
"logo" => null
"twitter_business" => null
"facebook_business" => null
"instagram_business" => null
"google_places" => null
"ical" => null
"slug" => null
"api_key" => null
"geoloc_id" => null
"deleted" => 0
"created_at" => null
"updated_at" => null
]
#original: array:18 [
"id" => 1
"name" => "dasd"
"type" => "dsa"
"email" => "adas"
"logo" => null
"twitter_business" => null
"facebook_business" => null
"instagram_business" => null
"google_places" => null
"ical" => null
"slug" => null
"api_key" => null
"geoloc_id" => null
"deleted" => 0
"created_at" => null
"updated_at" => null
"pivot_user_id" => 1
"pivot_entity_id" => 1
]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:1 [
"pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#629
+parent: App\Entity {#597
#table: "entities"
+timestamps: true
#connection: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
#foreignKey: "user_id"
#relatedKey: "entity_id"
#guarded: []
#connection: null
#table: "entity_user"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"user_id" => 1
"entity_id" => 1
]
#original: array:2 [
"user_id" => 1
"entity_id" => 1
]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
1 => App\Entity {#633
#table: "entities"
+timestamps: true
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:16 [
"id" => 2
"name" => "ddasd"
"type" => "fsdf"
"email" => "fsdf"
"logo" => null
"twitter_business" => null
"facebook_business" => null
"instagram_business" => null
"google_places" => null
"ical" => null
"slug" => null
"api_key" => null
"geoloc_id" => null
"deleted" => 0
"created_at" => null
"updated_at" => null
]
#original: array:18 [
"id" => 2
"name" => "ddasd"
"type" => "fsdf"
"email" => "fsdf"
"logo" => null
"twitter_business" => null
"facebook_business" => null
"instagram_business" => null
"google_places" => null
"ical" => null
"slug" => null
"api_key" => null
"geoloc_id" => null
"deleted" => 0
"created_at" => null
"updated_at" => null
"pivot_user_id" => 1
"pivot_entity_id" => 2
]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:1 [
"pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#625
+parent: App\Entity {#597}
#foreignKey: "user_id"
#relatedKey: "entity_id"
#guarded: []
#connection: null
#table: "entity_user"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [
"user_id" => 1
"entity_id" => 2
]
#original: array:2 [
"user_id" => 1
"entity_id" => 2
]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: false
#hidden: []
#visible: []
#fillable: []
}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
]
}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
Your relationship between the entity object and the user object is a many to many. You cannot do
$entity->users->id
This is because you are trying to the id property on a collection of users.
Try this:
$entity->users->first()->id
To loop through your users, do this:
foreach ($entity->users as $user)
{
echo($user->id);
}
This is related to another question I asked previously that wasn't answered successfully.
I think I have narrowed it down to make the question more precise.
I have a parent/child BelongsTo relation in Laravel and it is returning the following (which is the result of a {{dd($task->asset->parent_asset())}}
BelongsTo {#657 ▼
#foreignKey: "parent_asset_id"
#otherKey: "id"
#relation: "parent_asset"
#query: Builder {#663 ▶}
#parent: Asset {#664 ▼
#dates: array:1 [▶]
#fillable: array:19 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:24 [▼
"id" => 4
"agency_id" => 1
"name" => "Bedrock Park"
"description" => ""
"serial_number" => ""
"asset_category_id" => 5
"activity_center_id" => 1
"original_cost" => 0.0
"in_service_date" => "1920-01-01"
"expected_lifespan" => 0
"status" => 1
"notes" => ""
"created_at" => "2015-09-14 17:59:47"
"updated_at" => "2016-10-31 20:01:29"
"square_feet" => 0
"gps_longitude" => "0.000000"
"gps_latitude" => "0.000000"
"parent_asset_id" => 0
"supervisor_id" => 0
"model_number" => 0
"is_location" => 1
"asset_sub_category_id" => 0
"asset_group_id" => 2
"address" => ""
]
#original: array:24 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
#related: Asset {#682 ▶}
}
I can see that the "name" attribute is there and I am trying to access that attribute of "name" in the following way but I receive an error "Trying to get property of non-object".
$task->asset->parent_asset->name
How should I be accessing the "name" attribute in the collection?
Model relations:
This is the relation in the asset model:
public function parent_asset()
{
return $this->belongsTo('\pmms\Asset', 'parent_asset_id');
}
and the relation in the task model
public function asset()
{
return $this->belongsTo('\pmms\Asset');
}
results of dd($task) on query $task = Task::with('asset.parent_asset')->first();
Task {#738 ▼
#dates: array:2 [▶]
#fillable: array:20 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:23 [▼
"id" => 71214
"agency_id" => 1
"sourceable_id" => 6
"sourceable_type" => "\pmms\Primary_task_type"
"primary_task_type_id" => 6
"activity_center_id" => 0
"asset_id" => 158
"hours" => 0.0
"labor_code_id" => 1
"user_id" => 7
"assigned_to_user_id" => 7
"do_date" => "2016-10-01"
"date_completed" => null
"status" => 0
"supervisor_notes" => ""
"staff_notes" => ""
"created_at" => "2016-07-17 21:44:22"
"updated_at" => "2016-07-17 21:44:22"
"priority_level" => 3
"crew_assignment_id" => null
"multi_asset_task_id" => null
"scheduled_maintenance_series_id" => 23
"pay_rate" => 0.0
]
#original: array:23 [▼
"id" => 71214
"agency_id" => 1
"sourceable_id" => 6
"sourceable_type" => "\pmms\Primary_task_type"
"primary_task_type_id" => 6
"activity_center_id" => 0
"asset_id" => 158
"hours" => 0.0
"labor_code_id" => 1
"user_id" => 7
"assigned_to_user_id" => 7
"do_date" => "2016-10-01"
"date_completed" => null
"status" => 0
"supervisor_notes" => ""
"staff_notes" => ""
"created_at" => "2016-07-17 21:44:22"
"updated_at" => "2016-07-17 21:44:22"
"priority_level" => 3
"crew_assignment_id" => null
"multi_asset_task_id" => null
"scheduled_maintenance_series_id" => 23
"pay_rate" => 0.0
]
#relations: array:2 [▼
"assignee" => User {#670 ▶}
"asset" => Asset {#666 ▼
#dates: array:1 [▶]
#fillable: array:19 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:24 [▶]
#original: array:24 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
I can only assume that your relationships are working.
You are dumping the definition of relationship, not result of the query.
Try using this for query:
$task = Task::with('asset.parentAsset')->first();
And then dump like this:
dd($task->asset->parent_asset->name);
Assuming your relationships are defined correctly this should give you the result you want.
Edit:
As it turns out the relationship must be camelCased in order to use the magic property. More Laravel 4 - Can't retrieve data in a one-to-many relationship
So rename your parent_asset() method to parentAsset() and try the above queries.
I found the answer after much head shaking:
It is not this that I needed to call:
$task->asset->parent_asset->name
It is this:
$task->asset->parent_asset['name']
I am not sure why it is an array in this case but I did dd($task->asset->parent_asset) and it showed an array. Once I changed to an array in my view, it worked like I wanted.