I am using PHP SDK to fetch all posts and comments with relative likes from a facebook page. When I try to fetch also the name of the commentators with the property from, nothing change. This is the link:
/me/posts?fields=message,comments.summary(true) {message,from{id,name},likes.summary(true)},likes.summary(true),created_time'
I still get:
"comments" => array:3 [▼
"data" => array:2 [▼
0 => array:3 [▼
"message" => "commento al secondo post"
"likes" => array:3 [▶]
"id" => "126427989164897_126428849164811"
]
1 => array:4 [▶]
]
There is no attribute from in the response, as you can see. I have tried a lot of combination but I can see the name and the id of the commentator only if is the page itself.
Any suggestion?
Related
I have one to many relationship between users and posts.
I want to know if I eager load in the posts can I modify the collection without a new attribute getting created on the User model.
$user = User::with('posts')->get();
// Filter the posts on the user using business logic.
// Below is an example. I DO NOT want to do this logic in the db/query builder.
// Arbitrary business rule that is not easily possible to calculate in DB
$shouldGetTestPost = true;
$user->posts = $user->posts->filter(function($post) use ($shouldGetTestPost) {
if ($shouldGetTestPost && $post->name = 'test') {
return true;
}
return false;
});
dd($user);
If I run the above code laravel will create a new attribute called posts and assign the collection to that attribute instead of modifying the relationship.
e.g.
// I've removed irrelevant model data
App\User {#1145
#table: "users"
#attributes: array:6 [
"id" => 1
"email" => "test#test.com"
"password" => "secret"
"updated_at" => "2019-02-11 18:56:35"
"created_at" => "2019-02-11 18:56:35"
// Below is new
"posts" => Illuminate\Database\Eloquent\Collection {#1217
#items: array:1 [
0 => App\Post {#1269
#table: "posts"
#attributes: array:24 [
"id" => 1
"name" => 'test'
"user_id" => 1
]
}
]
}
]
#original: array:5 [...]
#relations: array:1 [
"posts" => Illuminate\Database\Eloquent\Collection {#1264
#items: array:2 [
0 => App\Post {#1269}
1 => App\Post {#1234
#table: "posts"
#attributes: array:24 [
"id" => 1
"name" => 'test'
"user_id" => 1
]
}
]
}
]
]
Another interesting thing that happens is that in #relations the data is removed from theitems array but the reference still remains. See 0 => App\Post {#1269}
Is this intended for laravel? I'm aware that laravel prioritises attributes over relations but it seems odd that it wouldn't change the relations array.
How would I only change the relations collection and not create a new attribute?
The setRelation method can be used to override what's stored in a relation.
$model->setRelation($relation, $value)
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_setRelation
However, there's very few situations where this would be useful so I still believe this is an XY problem. You can constrain the eager loading query by passing a closure as the second argument to with() which would be the solution to conditionally retrieving related items.
I am using pagination in Laravel 5.6 to build an application. I am using Laravel DB instead of Eloquent. However, when I add a pagination link on view, it shows an error Call to a member function links() on array. This is because I am using an array, the default is an object with a collection. The output I am getting looks like
array:12 [▼
"current_page" => 1
"data" => array:3 [▶]
"first_page_url" => "http://127.0.0.1:8000/posts?page=1"
"from" => 1
"last_page" => 3
"last_page_url" => "http://127.0.0.1:8000/posts?page=3"
"next_page_url" => "http://127.0.0.1:8000/posts?page=2"
"path" => "http://127.0.0.1:8000/posts"
"per_page" => 3
"prev_page_url" => null
"to" => 3
"total" => 9
]
When I use normal pagination i.e. without converting to an array, the output is
LengthAwarePaginator {#264 ▼
#total: 9
#lastPage: 3
#items: Collection {#251 ▶}
#perPage: 3
#currentPage: 1
#path: "http://127.0.0.1:8000/posts"
#query: []
#fragment: null
#pageName: "page"
}
The problem I face here is adding other data to collection
I can't use the default output return by pagination() method. Can someone please suggest how to solve this issue?
Using the below, I am getting the required output.
Step 1: Include LengthAwarePaginator in the file
use Illuminate\Pagination\LengthAwarePaginator;
Step 2: Create an object of LengthAwarePaginator
$paginate = new LengthAwarePaginator(Array, countOfArray, perPage, currentPage, [
'path' => request()->url(),
'query' => request()->query()
]);
I kept currentPage value as null.
Return $paginate to view.
return view('home', ['data' => Array, 'page' => $paginate]);
I tried to set cookie (easy example):
Route::get('set', function () {
Cookie::queue(Cookie::forever('cart_id', 123));
});
Route::get('get', function (Illuminate\Http\Request $request) {
dump($request);
});
But I get in dump of $request:
Request {#40 ▼
...
+cookies: ParameterBag {#43 ▼
#parameters: array:3 [▼
"cart_id" => null
"XSRF-TOKEN" => null
"laravel_session" => null
]
}
+headers: HeaderBag {#46 ▼
#headers: array:9 [▼
...
"cookie" => array:1 [▼
0 => "cart_id=eyJpdiI6IldNbjJwMHdRclBmWkFkUFJ3SnJ6Z2c9PSIsInZhbHVlIjoiVnhBWGl3ODFBaGhwTCtSc0NoSmlLUT09IiwibWFjIjoiYjU2NjRmNDI4OGQ4NGZmZjNjMDhmYTEwMWJlOWQ2ZjUwYjAxNTMxYjU2OWY3Y2I0NjYxNzQzZTNhZjVkMDcxOSJ9; XSRF-TOKEN=eyJpdiI6Ino2ckNVSU5cL3VEcE5VQWRqQm1wTkV3PT0iLCJ2YWx1ZSI6IkF2b1Y0VDFkMlVqam8zeWVJQk55Mm96XC9PZFlyZUxma3FBTXNBV2VoK3AyTkp5Q2RNOHZtQnRyMFB0a3c0Mm5cL1hJdkkyY1haUHhZV0FEejdKSjMzaHc9PSIsIm1hYyI6IjFlNjVkZDE2NDJiYjA2MDQzM2JiYWYzMWY2YWRiNTk0ZDM4MjBlNmEzZmEwNTUxZWUzY2NmYmEyZDljMmYwOGIifQ%3D%3D; laravel_session=eyJpdiI6Imh1QW1iamlscnVpV3JFTThHYWdTNkE9PSIsInZhbHVlIjoiZElUMU1MZlRwa1BRZFd6T0pWMGhSanNCcmVNcjdEUGJrd2Zhc3RqbFUzVFBOamxsclBnbWhHcVk4QXFXU2VFMkVGYXBUSm4xVVFvcGZoZnVxN1VTVnc9PSIsIm1hYyI6IjkyYjA5YTI3MmE3MWE5OWRhNDBjYWZkZjQwZjk0NjI1OTY4MGNjZDM4ZDQ4YWE3YjdkMWMxNmQwZjgxMzQzZTYifQ%3D%3D"
]
]
#cacheControl: array:1 [▶]
}
}
Of course, firstly I go to /set link, then /get.
Where did I go wrong? Why all of my cookies equals to null?
By the way, last 5 hours I tried to make a function, that will contains Cookie::queue() function, but it goes wrong too. I dont know, what's wrong with my laravel cookies
Vender hasMany Banquet
Here is my query:
$banquet = Vendor::with(['banquet' => function($query){
$query->where('active',1)->where('id' ,1)->first();
}])->findOrfail(1);
but the relation part return an collection like get() method not first()
#relations: array:1 [▼
"banquet" => Collection {#283 ▼
#items: array:1 [▼
0 => Banquet {#285 ▼
How to trans like this? So that I can just get the banquet data like $vendor->banquet not $vendor->banquet[0]
#relations: array:1 [▼
"banquet" => Banquet {#285 ▼
I think, you are using OneToMany for banquet.
So, you are getting in the collection/array.
Please, try it as OneToOne. and say if you get any change or not.
return $this->belongsTo();
in banquet method.
or
return $this->hasOne();
I'm trying to use https://github.com/skmetaly/laravel-twitch-restful-api package to get twitch integration to my website.
That's the error that i get.
ErrorException in helpers.php line 469:
htmlentities() expects parameter 1 to be string, array given (View: /var/www/rafdev.ovh/html/msvixen/resources/views/twitch.blade.php)
My controller
$code = Input::get('code');
if ($code !== null)
{
$token = TwitchApi::requestToken($code);
} else
{
$token = null;
}
$data = TwitchApi::streamsFollowed($token);
return view('twitch', ['token' => $token, 'data' => $data]);
my view
#extends('master')
#section('content')
<h1>Twitch.TV</h1>
{{ $token }}
{{ $data }}
#endsection
After using dd()
array:9 [▼
0 => array:11 [▼
"_id" => 17733016640
"game" => "World of Warcraft"
"viewers" => 15551
"created_at" => "2015-11-15T22:27:13Z"
"video_height" => 1080
"average_fps" => 60.2769481401
"delay" => 0
"is_playlist" => false
"_links" => array:1 [▶]
"preview" => array:4 [▶]
"channel" => array:22 [▶]
]
1 => array:11 [▶]
2 => array:11 [▶]
3 => array:11 [▶]
4 => array:11 [▶]
5 => array:11 [▶]
6 => array:11 [▶]
7 => array:11 [▶]
8 => array:11 [▶]
]
so it works, but when i try to display data - its back to the htmlentities() error
This is happening because $data is returned as an array.
When TwitchApi::streamsFollowed($token); is called, the Facade calls the method in Skmetaly\TwitchApi\Services\TwitchApiService.
This in turn creates an instance of Skmetaly\TwitchApi\API\Users and calls the streamsFollowed() method there.
This method makes a call to /streams/followed which returns a data set such as the example below. It's automatically converted to an array rather than JSON using the Guzzle HTTP Client's json() method.
{
"_links": {
"self": "https://api.twitch.tv/kraken/streams/followed?limit=25&offset=0",
"next": "https://api.twitch.tv/kraken/streams/followed?limit=25&offset=25"
},
"_total": 123,
"streams": [...]
}
In order to display the streams you'd need to iterate over the streams array within $data.
If you were to modify your controller slightly
return view('twitch', ['token' => $token, 'streams' => $data->streams]);
You'd then be able to iterate over the streams in your view.
#foreach($streams as $stream)
{{ $stream }}
#endforeach
Update: You'll notice that each stream is also an array. What this means is you need to choose which of the keys in each array you'd like to display. Let's assume that inside one of the streams there is a key called broadcaster which contains a string; you could modify the above as follows.
#foreach($streams as $stream)
{{ $stream['broadcaster'] }}
#endforeach
Having now read the streams example response documentation it would appear that the contents of a stream varies depending on whether or not the stream is online. NB: This is assuming the data structure is the same as you've not posted the contents of a stream in your question.
This means that offline, {{ $stream['broadcaster'] }} would work, but when online it wouldn't and you'd get the same error. What you'll likely need to do is use an #if #else block in your #foreach to determine if the stream is null before trying to echo part of the information.
You could also filter the offline streams in the controller by removing null values from data.