I am running the following query in my laravel code,
$organisations = Organisation::with('projects', 'projects.clients')
->with('clients')
->get();
$organisations->load('users');
This is loading in my organisations table, and should in thoery load in any related models as well so I would expect an object that looks like this to be returned,
organisation {
projects {
clients {
}
}
users {
}
}
Basically an organisation can have many projects, a project can have one client, an organisation can also have many users and many clients.
What I am wanting from my query is for the client data to returned with each project but I am not getting that, currently I am getting the following object returned.
[
{
"id": "114",
"name": "XXXXXXX",
"slug": "XXXXXXX",
"information": "",
"type": "organisation",
"notifications": "0",
"add_all": "0",
"created_at": "2014-12-16 10:16:03",
"updated_at": "2014-12-16 10:16:03",
"clients": [
{
"id": "39",
"name": "Simon's Test",
"information": "",
"address": null,
"website": null,
"email": null,
"phone": null,
"type": "client",
"add_all": "0",
"created_at": "2014-12-16 10:16:20",
"updated_at": "2014-12-16 10:16:20",
"user_id": "1",
"owner_id": "114",
"projects": [
{
"id": "56",
"name": "iOS Application",
"description": "An iOS application that will be available on iPhone and iPad. Something that will change the way we manage projects.",
"total_cost": "5000.00",
"start_date": "2014-01-01",
"finish_date": "2014-12-10",
"status": "1",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-12-15 13:20:07"
},
{
"id": "57",
"name": "Android Application",
"description": "An android application that will run on any android phone, upto a certain version.",
"total_cost": "6500.00",
"start_date": "2015-01-31",
"finish_date": "2015-03-19",
"status": "2",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-12-11 15:01:15"
},
{
"id": "58",
"name": "Java Application",
"description": "A windows phone application, to be released for the small market share that market has.",
"total_cost": "7500.00",
"start_date": "2014-12-12",
"finish_date": "2014-12-31",
"status": "3",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-12-11 15:01:15"
},
{
"id": "59",
"name": "OSX Application Beta",
"description": "A desktop application for the Mac. More feature rich than the native iOS apps, more like the web application for desktop.",
"total_cost": "20000.00",
"start_date": "2014-11-20",
"finish_date": "2014-12-19",
"status": "2",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-11-28 16:01:41"
}
],
"users": []
}
],
"projects": [
{
"id": "56",
"name": "iOS Application",
"description": "An iOS application that will be available on iPhone and iPad. Something that will change the way we manage projects.",
"total_cost": "5000.00",
"start_date": "2014-01-01",
"finish_date": "2014-12-10",
"status": "1",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-12-15 13:20:07",
"pivot": {
"organisation_id": "114",
"project_id": "56"
}
},
{
"id": "57",
"name": "Android Application",
"description": "An android application that will run on any android phone, upto a certain version.",
"total_cost": "6500.00",
"start_date": "2015-01-31",
"finish_date": "2015-03-19",
"status": "2",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-12-11 15:01:15",
"pivot": {
"organisation_id": "114",
"project_id": "57"
}
},
{
"id": "58",
"name": "Java Application",
"description": "A windows phone application, to be released for the small market share that market has.",
"total_cost": "7500.00",
"start_date": "2014-12-12",
"finish_date": "2014-12-31",
"status": "3",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-12-11 15:01:15",
"pivot": {
"organisation_id": "114",
"project_id": "58"
}
},
{
"id": "59",
"name": "OSX Application Beta",
"description": "A desktop application for the Mac. More feature rich than the native iOS apps, more like the web application for desktop.",
"total_cost": "20000.00",
"start_date": "2014-11-20",
"finish_date": "2014-12-19",
"status": "2",
"sales_person": null,
"project_manager": null,
"client_id": "39",
"organisation_id": "114",
"owner_id": "114",
"user_id": "1",
"created_at": null,
"updated_at": "2014-11-28 16:01:41",
"pivot": {
"organisation_id": "114",
"project_id": "59"
}
}
],
"users": [
{
"id": "1",
"email": "simon#pikcells.com",
"first_name": "Simon",
"last_name": "Ainley",
"display_name": "simonainley",
"initials": "SA",
"remember_me": null,
"active": "1",
"invite_code": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2014-12-12 11:53:53",
"deleted_at": null,
"pivot": {
"organisation_id": "114",
"user_id": "1",
"is_admin": "1"
}
}
]
}
]
In the object above you can see that I get the organisations projects and clients returned to me, but the clients have nested projects, i.e what projects they are related too, but I would like to return projects nested with the clients they are related too is this possible?
One note about the naming. If your projects can only have one client the relationship would better off if called "client" instead of "clients".
Project Model
//Client relationship in Project model
client()
{
$this->belongsTo('Client')
}
this way is more readable and you can call it like this
Organisation::with('projects.client', 'clients', 'users')->get()
you need the relationships projects, clients and users in the Organisation model.
Check the Eloquent documentation for more info about relationships
$organisations = Organisation::with(array('Projects'=>function($q){
$q->with('Client')
},'Clients','Users'))->get()
You can fetch expected output using above code
You have to define 3 relationships in Organization model : Projects,Clients,Users
You have to define 1 relationship in Project model : Client
Related
I would like to perform the average rating of respective product which stored in the pivot table called "reviews".Below are my existing code.
Product Model:
public function reviews(){
return $this->belongsToMany(User::class,'reviews')->withPivot('comment','rating')->withTimestamps();
}
public function getProductRatingAttribute(){
return $this->reviews()->average('rating');
}
User Model:
public function reviews(){
return $this->belongsToMany(Product::class,'reviews')->withPivot('comment','rating')->withTimestamps();
}
Migration for the reviews
Schema::create('reviews', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('product_id');
$table->string('comment')->nullable();
$table->integer('rating');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
I have attempted to use the below to code construct the expected output but it leads N+1 issue because it does not take advantage of the eager loading of the Laravel Eloquent.
$s = Product::with('reviews')->append('product_rating');
As I check telescope, it produces 6 query which can lead performance issue when the data is large.
Expected output:
{
"msg": [
{
"id": 4,
"created_at": "2021-04-09T07:32:35.000000Z",
"updated_at": "2021-04-09T07:32:35.000000Z",
"name": "MacDonald",
"nickname": "MCD",
"users_id": 1,
"price": "0.00",
"product_rating": "3.3333",
"reviews": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 4,
"user_id": 1,
"comment": "Ouch",
"rating": 3,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
},
{
"id": 2,
"name": "Kelvin Ooi",
"email": "kelvin.ooi#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-13T12:07:11.000000Z",
"role": 1,
"pivot": {
"product_id": 4,
"user_id": 2,
"comment": "Ouch",
"rating": 5,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
},
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 4,
"user_id": 1,
"comment": "Ouch",
"rating": 2,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
}
]
},
{
"id": 10,
"created_at": null,
"updated_at": null,
"name": "Mary Bown",
"nickname": "MB",
"users_id": 1,
"price": "2.88",
"product_rating": "2.0000",
"reviews": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 10,
"user_id": 1,
"comment": "Ouch",
"rating": 2,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
}
]
},
{
"id": 11,
"created_at": null,
"updated_at": null,
"name": "Pizza Hut",
"nickname": "pizzahut",
"users_id": 1,
"price": "4.10",
"product_rating": null,
"reviews": [
]
},
{
"id": 12,
"created_at": "2021-04-09T08:00:42.000000Z",
"updated_at": "2021-04-09T08:00:42.000000Z",
"name": "Domino Pizza",
"nickname": "domino",
"users_id": 3,
"price": "0.00",
"product_rating": null,
"reviews": [
]
},
{
"id": 13,
"created_at": "2021-04-26T16:12:53.000000Z",
"updated_at": "2021-04-26T16:12:53.000000Z",
"name": "Chicken Chop",
"nickname": "chickchop",
"users_id": 3,
"price": "0.00",
"product_rating": null,
"reviews": [
]
}
]
}
Appending is performing the query on each record.
Why not perform average on the collection itself?
$s = Product::with('reviews')->get();
$avg = $s->avg('pivot.rating');
Alternatively you can modify the relationship method to include a raw select of the average rating.
I am using this package
https://github.com/renoki-co/befriended
My issue is trying to get all my friends posts with my post including user's data, but the problem is the requester data is null.
$user = $request->user();
$followersPost = UserPost::with(['user' => function ($query) use ($user) {
$query->filterFollowingsOf($user);
}])->latest()->get();
return $followersPost;
Response
[
{
"id": 35,
"post_body": "The Requster Post",
"location_id": "4b0da1acf964a520b94c23e3",
"location_title": "Apple Infinite Loop",
"location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png",
"location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}",
"location_vicinity": "Cupertino - 1 Infinite Loop",
"user_id": 1,
"viewer_ids": null,
"created_at": "2020-12-06T18:08:58.000000Z",
"updated_at": "2020-12-06T18:08:58.000000Z",
"images": [
],
"musics": [
],
"movies": [
],
"emojs": [
],
"user": null
},
{
"id": 34,
"post_body": "Test Post",
"location_id": "4b0da1acf964a520b94c23e3",
"location_title": "Apple Infinite Loop",
"location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png",
"location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}",
"location_vicinity": "Cupertino - 1 Infinite Loop",
"user_id": 2,
"viewer_ids": null,
"created_at": "2020-12-06T18:08:14.000000Z",
"updated_at": "2020-12-06T18:08:14.000000Z",
"images": [
],
"musics": [
],
"movies": [
],
"emojs": [
],
"user": {
"id": 2,
"_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0",
"name": "Saleh",
"username": "saleh",
"email": "saleh#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:27:02.000000Z",
"updated_at": "2020-12-05T00:27:21.000000Z"
}
},
{
"id": 24,
"post_body": null,
"location_id": null,
"location_title": null,
"location_icon": null,
"location_coordinate": null,
"location_vicinity": null,
"user_id": 3,
"viewer_ids": null,
"created_at": "2020-12-06T01:53:18.000000Z",
"updated_at": "2020-12-06T01:53:18.000000Z",
"images": [
],
"musics": [
{
"id": 4,
"post_id": 24,
"song_name": "Confetti",
"song_preview": "https://cdns-preview-6.dzcdn.net/stream/c-6eb643a05083c023f874f13739fc7c87-3.mp3",
"song_picture": "https://api.deezer.com/album/183603832/image",
"song_artist": "Little Mix",
"created_at": "2020-12-06T01:53:18.000000Z",
"updated_at": "2020-12-06T01:53:18.000000Z"
}
],
"movies": [
],
"emojs": [
{
"id": 16,
"post_id": 24,
"user_id": 3,
"emojy": "015-smile-1",
"created_at": "2020-12-06T01:59:15.000000Z",
"updated_at": "2020-12-06T01:59:15.000000Z",
"user": {
"id": 3,
"_id": "af65864b-f18a-4609-91db-336acbce1486",
"name": "Ali",
"username": "ali2323",
"email": "ali#gmail.com",
"email_verified_at": null,
"avatar": null,
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T04:05:50.000000Z",
"updated_at": "2020-12-05T04:05:50.000000Z"
}
},
{
"id": 17,
"post_id": 24,
"user_id": 1,
"emojy": "089-broken-heart",
"created_at": "2020-12-06T16:58:07.000000Z",
"updated_at": "2020-12-06T16:58:24.000000Z",
"user": {
"id": 1,
"_id": "737ba284-424d-4b0d-8a56-2e845603f2ba",
"name": "Mohammed",
"username": "mohammed",
"email": "h4ck3r.x0#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128001.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:26:09.000000Z",
"updated_at": "2020-12-05T00:26:41.000000Z"
}
},
{
"id": 19,
"post_id": 24,
"user_id": 2,
"emojy": "021-sad",
"created_at": "2020-12-06T16:58:36.000000Z",
"updated_at": "2020-12-06T16:58:36.000000Z",
"user": {
"id": 2,
"_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0",
"name": "Saleh",
"username": "saleh",
"email": "saleh#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:27:02.000000Z",
"updated_at": "2020-12-05T00:27:21.000000Z"
}
}
],
"user": {
"id": 3,
"_id": "af65864b-f18a-4609-91db-336acbce1486",
"name": "Ali",
"username": "ali2323",
"email": "ali#gmail.com",
"email_verified_at": null,
"avatar": null,
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T04:05:50.000000Z",
"updated_at": "2020-12-05T04:05:50.000000Z"
}
}
]
As you can see the first object in the array dosen't include the user data.
So what should i do $query->filterFollowingsOf($user); to include my data?
I think the with callback is used for modifying the query that selects the user data. Instead, try using a whereHas method that filters the related users.
$user = $request->user();
$followersPost = UserPost
::with('user')
->whereHas('user', function ($query) use ($user) {
$query->filterFollowingsOf($user);
})
->latest()
->get();
return $followersPost;
I'd also recommend using paginate() instead of get() for performance when there could be a significant number of posts.
Edit:
To get either your friend's posts or your posts, try:
$followersPost = UserPost
::with('user')
->whereHas('user', function ($query) use ($user) {
$query->filterFollowingsOf($user);
})
->orWhere('user_id', $user->id)
->latest()
->get();
I have an api ready that returns the following json response.
Response:
{
"success": true,
"conversation": [{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
},
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
}
]
}
This response consists of list of users and their associated last chat message for currently logged in user. I want to sort this collection according to the latest message first. How can I do that?
Sorting should be based on the chat.created_at field.
Expected Result:
{
"success": true,
"conversation": [
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
},
{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
}
]
}
UPDATE :
Adding these lines worked as expected. Thanks #JCode
$sorted = $chats->sortByDesc('chat.created_at');
$chats = $sorted->values()->all();
You are looking for the sortBy() method.
This will of course work if your JSON output can be turned into a collection via collect()—in a perfect scenario, your chat messages would be a model in Laravel.
In my forum I'm showing all topics. It looks like this:
So a user can subscribe by clicking on the red hearth.
I'm loading the topics with this json:
{
"forum": {
"id": 1,
"slug": "test",
"name": "test",
"description": "test",
"created_at": null,
"updated_at": null
},
"topics": {
"total": 6,
"per_page": 5,
"current_page": 1,
"last_page": 2,
"next_page_url": "http://forum.dev/api/forum/test?page=2",
"prev_page_url": null,
"from": 1,
"to": 5,
"data": [
{
"id": 1,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 2,
"slug": "2",
"name": "1",
"description": "1\t",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 3,
"slug": "1",
"name": "1",
"description": "1\t",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 4,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 5,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null
}
]
}
}
I'm returning that ^ like this:
$forum->topics()->orderBy('created_at', 'DESC')->paginate(5);
So how do I get a subscribe value on every topic object?
So like this:
"data": [
{
"id": 1,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null,
"subscribed": true
},
I already made this on my topic model:
/**
* #return mixed
*/
public function subscriptions()
{
return Topic::subscribedBy(Auth::user())->get();
}
And it's working. But how do I send that ^ with every topic.
You can add an attribute (which is not present in the database) by creating an accessor.
public function getSubscriptionsAttribute()
{
return Topic::subscribedBy(Auth::user())->get();
}
and then adding it to the $append property.
protected $appends = ['subscriptions'];
If you're using the $visible whitelist you might have to add it to that property too.
Source (Its all the way in the bottom.)
I have relationship that is setup like this,
Organisation.php
public function projects()
{
return $this->belongsToMany('Project');
}
Project.php
public function organisations()
{
return $this->belongsToMany('Organisation');
}
I am running the following query,
$project = Project::where('user_id', '=', ResourceServer::getOwnerId())
->whereNull('organisation_id')
->get(array(
'projects.id',
'projects.name',
'projects.description',
'projects.total_cost',
'projects.sales_person',
'projects.project_manager',
'projects.client_id',
'projects.start_date',
'projects.finish_date',
'projects.organisation_id',
'projects.created_at',
'projects.user_id',
'projects.owner_id',
'projects.status'
));
$project->load('salesperson');
$project->load('projectmanager');
$project->load('clients')->load('clients.users');
$project->load('organisations')->load('organisations.users');
$project->load('collaborators');
$project->load('user');
$owned_by_me = $project->toArray();
The results I get back looks like this,
[
{
"id": "56",
"name": "iOS Application",
"description": "An iOS application that will be available on iPhone and iPad. Something that will change the way we manage projects.",
"total_cost": "5000.00",
"sales_person": "88",
"project_manager": "86",
"client_id": "39",
"start_date": "2014-01-01",
"finish_date": "2014-12-10",
"organisation_id": "114",
"created_at": "2014-12-15 13:20:07",
"status": "1",
"owner_id": "114",
"user_id": "1",
"is_admin": "1",
"salesperson": {
"id": "88",
"email": "stephen#pikcells.com",
"first_name": "Stephen",
"last_name": "Mooney",
"display_name": "stephenmooney",
"initials": "SM",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null
},
"projectmanager": {
"id": "86",
"email": "matt#pikcells.com",
"first_name": "Matt",
"last_name": "Fell",
"display_name": "Matt Fell",
"initials": "MF",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null
},
"clients": {
"id": "39",
"name": "Simon's Test",
"information": "",
"address": "This is an address and it can used for loads\r\n\r\nmultiple lines and stuff",
"website": "http://website.com",
"email": "email#email.com",
"phone": "07799009988",
"type": "client",
"add_all": "0",
"created_at": "2014-12-16 10:16:20",
"updated_at": "2014-12-17 16:22:55",
"user_id": "1",
"owner_id": "114",
"users": [
{
"id": "85",
"email": "richard#pikcells.com",
"first_name": "Richard",
"last_name": "Benson",
"display_name": "richardbenson",
"initials": "RB",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null,
"pivot": {
"client_id": "39",
"user_id": "85"
}
},
{
"id": "88",
"email": "stephen#pikcells.com",
"first_name": "Stephen",
"last_name": "Mooney",
"display_name": "stephenmooney",
"initials": "SM",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null,
"pivot": {
"client_id": "39",
"user_id": "88"
}
}
]
},
"user": {
"id": "1",
"email": "simon#pikcells.com",
"first_name": "Simons",
"last_name": "Ainley",
"display_name": "simonainley",
"initials": "SA",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": "114",
"forgotten_code": "7bffdaa1b5b238f5ba8b13e46c53dc46",
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2015-01-28 10:48:41",
"deleted_at": null
},
"organisations": [],
"collaborators": [
{
"id": "1",
"email": "simon#pikcells.com",
"first_name": "Simons",
"last_name": "Ainley",
"display_name": "simonainley",
"initials": "SA",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": "114",
"forgotten_code": "7bffdaa1b5b238f5ba8b13e46c53dc46",
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2015-01-28 10:48:41",
"deleted_at": null,
"pivot": {
"project_id": "56",
"user_id": "1"
}
},
{
"id": "91",
"email": "alan.test#testdomain.com",
"first_name": "Alan",
"last_name": "Test",
"display_name": "alantest",
"initials": "AT",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "2014-12-12 11:13:07",
"updated_at": "2014-12-12 11:13:07",
"deleted_at": null,
"pivot": {
"project_id": "56",
"user_id": "91"
}
}
]
}
]
As you can see the organisations attribute is empty, but by the virtue that is has an organisation_id it should return an organisation.
Why would it not be doing?
It's belongsTo:
public function organisation()
{
return $this->belongsTo('Organisation');
}
belongsToMany is a m-m relation with pivot table, which is not the case here, since you have organisation_id, right? Change it as above and you're good to go.
In this case also the other side of the relationship should be changed to hasMany (although it is not a problem in the code you pasted):
public function projects()
{
return $this->hasMany('Project');
}