I'm trying to get the data from my Lists table and include an array with the ID's of the tracks in that list.
This is a sample of the database model, with a relation N:M.
In my List model, I added this method:
public function tracks()
{
return $this->belongsToMany(Track::class, 'List_Tracks', 'id_list', 'id_track');
}
So, in my ListController, I'm doing the following:
$list = List::find($id);
$list->tracks_list = $list->tracks->pluck('track_id');
return $list;
And what I get is as many objects as tracks I have in a same list, for example:
[
{
"id_track": 1,
"name": "Yesterday",
"tracks_list": [
1,
2
]
"pivot": {
"id_list": 1,
"id_track": 1
}
},
{
"id_track": 2,
"name": "Lucy in the sky with diamonds",
"pivot": {
"id_list": 1,
"id_track": 2
}
}
]
But what I want to get is:
{
"id_list": 1,
"name": "The Best of The Beatles",
"tracks_list": [
1,
2
]
}
I think the things I've tried are much more complex than the proper solution.
How would you get the data in this way?
Thanks in advance.
You need to load the relationship first and then write rest of the eloquent query, otherwise you will get only list of ids from the pivot table.
List::with('tracks')->where('id', '=', $id)->first();
// Following method should work too
List::with('tracks')->find($id);
Based on your commentary - to get only array of ids related to the list you don't need to load the relationship and can use only:
List::find($id)->tracks()->pluck('id');
// In case previous method will not work, you can try this one:
List::find($id)->tracks()->pluck('tracks.id');
// before dot should be name of the table, not relationship's.
So if you need only ids of the tracks that are attached to the playlist. I would recommend adding to your List model following method:
// App\Models\List
public function getTrackIdsAttribute()
{
return $this->tracks->pluck('id')->toArray();
}
Then you should be able to simply call List::find($id)->trackIds to get all ids of tracks attached to the given list. If you're not sure why I am defining the method as getTrackIdsAttribute and call only trackIds on the model, take a look at Eloquent: Mutators.
Related
{
"movies": {
"movie1": {
"genre": "comedy",
"name": "As good as it gets",
"lead": "Jack Nicholson"
},
"movie2": {
"genre": "Horror",
"name": "The Shining",
"lead": "Jack Nicholson"
},
"movie3": {
"genre": "comedy",
"name": "The Mask",
"lead": "Jim Carrey"
}
}
}
I am a Firebase newbie. How can I retrieve a result from the data above where genre = 'comedy' AND lead = 'Jack Nicholson'?
What options do I have?
Using Firebase's Query API, you might be tempted to try this:
// !!! THIS WILL NOT WORK !!!
ref
.orderBy('genre')
.startAt('comedy').endAt('comedy')
.orderBy('lead') // !!! THIS LINE WILL RAISE AN ERROR !!!
.startAt('Jack Nicholson').endAt('Jack Nicholson')
.on('value', function(snapshot) {
console.log(snapshot.val());
});
But as #RobDiMarco from Firebase says in the comments:
multiple orderBy() calls will throw an error
So my code above will not work.
I know of three approaches that will work.
1. filter most on the server, do the rest on the client
What you can do is execute one orderBy().startAt()./endAt() on the server, pull down the remaining data and filter that in JavaScript code on your client.
ref
.orderBy('genre')
.equalTo('comedy')
.on('child_added', function(snapshot) {
var movie = snapshot.val();
if (movie.lead == 'Jack Nicholson') {
console.log(movie);
}
});
2. add a property that combines the values that you want to filter on
If that isn't good enough, you should consider modifying/expanding your data to allow your use-case. For example: you could stuff genre+lead into a single property that you just use for this filter.
"movie1": {
"genre": "comedy",
"name": "As good as it gets",
"lead": "Jack Nicholson",
"genre_lead": "comedy_Jack Nicholson"
}, //...
You're essentially building your own multi-column index that way and can query it with:
ref
.orderBy('genre_lead')
.equalTo('comedy_Jack Nicholson')
.on('child_added', function(snapshot) {
var movie = snapshot.val();
console.log(movie);
});
David East has written a library called QueryBase that helps with generating such properties.
You could even do relative/range queries, let's say that you want to allow querying movies by category and year. You'd use this data structure:
"movie1": {
"genre": "comedy",
"name": "As good as it gets",
"lead": "Jack Nicholson",
"genre_year": "comedy_1997"
}, //...
And then query for comedies of the 90s with:
ref
.orderBy('genre_year')
.startAt('comedy_1990')
.endAt('comedy_2000')
.on('child_added', function(snapshot) {
var movie = snapshot.val();
console.log(movie);
});
If you need to filter on more than just the year, make sure to add the other date parts in descending order, e.g. "comedy_1997-12-25". This way the lexicographical ordering that Firebase does on string values will be the same as the chronological ordering.
This combining of values in a property can work with more than two values, but you can only do a range filter on the last value in the composite property.
A very special variant of this is implemented by the GeoFire library for Firebase. This library combines the latitude and longitude of a location into a so-called Geohash, which can then be used to do realtime range queries on Firebase.
3. create a custom index programmatically
Yet another alternative is to do what we've all done before this new Query API was added: create an index in a different node:
"movies"
// the same structure you have today
"by_genre"
"comedy"
"by_lead"
"Jack Nicholson"
"movie1"
"Jim Carrey"
"movie3"
"Horror"
"by_lead"
"Jack Nicholson"
"movie2"
There are probably more approaches. For example, this answer highlights an alternative tree-shaped custom index: https://stackoverflow.com/a/34105063
If none of these options work for you, but you still want to store your data in Firebase, you can also consider using its Cloud Firestore database.
Cloud Firestore can handle multiple equality filters in a single query, but only one range filter. Under the hood it essentially uses the same query model, but it's like it auto-generates the composite properties for you. See Firestore's documentation on compound queries.
I've written a personal library that allows you to order by multiple values, with all the ordering done on the server.
Meet Querybase!
Querybase takes in a Firebase Database Reference and an array of fields you wish to index on. When you create new records it will automatically handle the generation of keys that allow for multiple querying. The caveat is that it only supports straight equivalence (no less than or greater than).
const databaseRef = firebase.database().ref().child('people');
const querybaseRef = querybase.ref(databaseRef, ['name', 'age', 'location']);
// Automatically handles composite keys
querybaseRef.push({
name: 'David',
age: 27,
location: 'SF'
});
// Find records by multiple fields
// returns a Firebase Database ref
const queriedDbRef = querybaseRef
.where({
name: 'David',
age: 27
});
// Listen for realtime updates
queriedDbRef.on('value', snap => console.log(snap));
var ref = new Firebase('https://your.firebaseio.com/');
Query query = ref.orderByChild('genre').equalTo('comedy');
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot movieSnapshot : dataSnapshot.getChildren()) {
Movie movie = dataSnapshot.getValue(Movie.class);
if (movie.getLead().equals('Jack Nicholson')) {
console.log(movieSnapshot.getKey());
}
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Frank's answer is good but Firestore introduced array-contains recently that makes it easier to do AND queries.
You can create a filters field to add you filters. You can add as many values as you need. For example to filter by comedy and Jack Nicholson you can add the value comedy_Jack Nicholson but if you also you want to by comedy and 2014 you can add the value comedy_2014 without creating more fields.
{
"movies": {
"movie1": {
"genre": "comedy",
"name": "As good as it gets",
"lead": "Jack Nicholson",
"year": 2014,
"filters": [
"comedy_Jack Nicholson",
"comedy_2014"
]
}
}
}
For Cloud Firestore
https://firebase.google.com/docs/firestore/query-data/queries#compound_queries
Compound queries
You can chain multiple equality operators (== or array-contains) methods to create more specific queries (logical AND). However, you must create a composite index to combine equality operators with the inequality operators, <, <=, >, and !=.
citiesRef.where('state', '==', 'CO').where('name', '==', 'Denver');
citiesRef.where('state', '==', 'CA').where('population', '<', 1000000);
You can perform range (<, <=, >, >=) or not equals (!=) comparisons only on a single field, and you can include at most one array-contains or array-contains-any clause in a compound query:
Firebase doesn't allow querying with multiple conditions.
However, I did find a way around for this:
We need to download the initial filtered data from the database and store it in an array list.
Query query = databaseReference.orderByChild("genre").equalTo("comedy");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ArrayList<Movie> movies = new ArrayList<>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String lead = dataSnapshot1.child("lead").getValue(String.class);
String genre = dataSnapshot1.child("genre").getValue(String.class);
movie = new Movie(lead, genre);
movies.add(movie);
}
filterResults(movies, "Jack Nicholson");
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Once we obtain the initial filtered data from the database, we need to do further filter in our backend.
public void filterResults(final List<Movie> list, final String genre) {
List<Movie> movies = new ArrayList<>();
movies = list.stream().filter(o -> o.getLead().equals(genre)).collect(Collectors.toList());
System.out.println(movies);
employees.forEach(movie -> System.out.println(movie.getFirstName()));
}
The data from firebase realtime database is as _InternalLinkedHashMap<dynamic, dynamic>.
You can also just convert this it to your map and query very easily.
For example, I have a chat app and I use realtime database to store the uid of the user and the bool value whether the user is online or not. As the picture below.
Now, I have a class RealtimeDatabase and a static method getAllUsersOnineStatus().
static getOnilineUsersUID() {
var dbRef = FirebaseDatabase.instance;
DatabaseReference reference = dbRef.reference().child("Online");
reference.once().then((value) {
Map<String, bool> map = Map<String, bool>.from(value.value);
List users = [];
map.forEach((key, value) {
if (value) {
users.add(key);
}
});
print(users);
});
}
It will print [NOraDTGaQSZbIEszidCujw1AEym2]
I am new to flutter If you know more please update the answer.
ref.orderByChild("lead").startAt("Jack Nicholson").endAt("Jack Nicholson").listner....
This will work.
This question already has answers here:
Laravel avoid duplicate entry from model
(2 answers)
Closed 3 years ago.
I'm building a Laravel API. I have a models called Reservations. I want to avoid that a user creates two reservations for the same product and time period.
I have the following:
$reservation = Reservation::firstOrCreate([
'listing_id' => $request->listing_id,
'user_id_from' => $request->user_id_from,
'start_date' => $request->start_date,
'end_date' => $request->end_date,
]);
return new ReservationResource($reservation);
Documentation says:
The firstOrCreate method will attempt to locate a database record
using the given column / value pairs
The above code is working:
it does add the item to the database if it does not exist yet (in
this case, the reply to the REST API returns the new model instance
which is correct.
it does not add the item to the database if the combination equals listing_id, user_id_from, start_date and end_date.
However, in the latter case (item already exists), it will also return in the REST API reply a reservation with the ID of the matched row.
Example: in the below table, reservation with id 2 already exists:
id listing_id user_id_from start_date end_date
------------------------------------------------------
1 2 3 2019-09-12 2019-10-14
Sending the below REST API request:
{
"listing_id": 2,
"user_id_from": 3,
"start_date": "2019-09-12",
"end_date": "2019-10-14",
}
returns
"data": {
"id": 1,
"user_id_from": 3,
"listing_id": 2,
"price": 388,
"start_date": "2019-09-12",
"end_date": "2019-10-14",
}
I would want to have a JSON reply stating that the item already exists. How can this be achieved?
firstOrCreate() is just a two-part function as you might expect, doing a lookup on first() and falling back to create() if not exists. You could implement much the same logic in your controller, and append a message to the first() result if it exists, and return the result as-is if created. Alternatively, you could modify the response with a HTTP_OK or HTTP_CREATED depending on the result. Or both.
public function store(Request $request)
{
$attributes = [
'listing_id' => $request->input('listing_id'),
'user_id_from' => $request->input('user_id_from'),
'start_date' => $request->input('start_date'),
'end_date' => $request->input('end_date')
];
// return existing reservation if exists
$reservation = Reservation::where($attributes)->first();
if ($reservation !== null) {
// add explicit message here if you want
return response(json_encode($reservation), Response::HTTP_OK);
}
// else create a new one
$reservation = Reservation::create($attributes);
// reload model
$reservation = Reservation::find($reservation->id);
return response(json_encode($reservation), Response::HTTP_CREATED);
}
I think you are looking for Laravel's wasRecentlyCreated:
if(! $reservation->wasRecentlyCreated){
// create & encode a JSON response or use this variable & method on the blade page
}
In my HTML frontend, I have a jQuery DataTable displaying all records fetched via AJAX from the database - a rather pretty straight forward thing. I use the Laravel Collection's ->transform(function($o){ . . . }) to iterate through the collection and return it in an array-esque manner. Just think of the following piece of code in a controller:
$cAllRecords = DatabaseRecord::all();
if(!empty($aData['sFilterIds']))
{
$cAllRecords = $cAllRecords->whereIn('creator', explode(',', $aData['sFilterIds']));
}
return response()->json(['data' => $cAllRecords->transform(function ($oDatabaseRecord) {
/** #var $oDatabaseRecord DatabaseRecord */
$sActionsHtml = 'edit';
$sUrl = route('some.route', ['iDatabaseRecordId' => $oDatabaseRecord->getAttribute('od')]);
return [
$oDatabaseRecord->getAttribute('id'),
$oDatabaseRecord->getAttribute('updated_at')->toDateTimeString(),
$oDatabaseRecord->getAttribute('created_at')->toDateTimeString(),
$sActionsHtml
];
})]);
I'm actually just filtering for records created by certain user IDs (the whereIn() call in line 4. However, the response sent back to the client looks different for different users filtered leading the jQuery table to show 'no records available', as it had received an malformed answer from the server. For one user, the response looks like this:
{
"data":[
[
1,
"2019-05-29 16:44:53",
"2019-05-29 16:44:53",
"<a href=\"#\">edit<\/a>"
]
]
}
This is a correctly formed server response and will show up in the table regularly. Great! Now something that drives me insane - the same code for another user (ID 1, while the first request was for user ID 2) returns this:
{
"data":{
"1":[
3,
"2019-05-29 17:08:49",
"2019-05-29 17:08:49",
"<a href=\"#\">edit<\/a>"
]
}
}
which, pretty obviously, is malformed and is not correctly parsed by the datatable. OK, now combing them two filters and filtering for user ID 1 and 2 will, again, return the response correctly formatted:
{
"data":[
[
1,
"2019-05-29 16:44:53",
"2019-05-29 16:44:53",
"<a href=\"#\">edit<\/a>"
],
[
3,
"2019-05-29 17:08:49",
"2019-05-29 17:08:49",
"<a href=\"#\">edit<\/a>"
]
]
}
I tried a number of things, none of which had worked since it's merely guessing why it could work with one user and not with another. (Things like reversing the order of IDs to be filtered, etc., but I found out that the filtering is not the problem. It MUST be the transform, which behaves inconsistent.)
Any ideas on why this happens and how to tackle it? I mean, it's not the only way to achieve what I'm after, I was using ->each() and array_push for all the time before but wanted to get rid of it for the sake of making use of Laravel's helpers (or possibilites) - the manual iteration and array pushing process worked out seamlessly before, and even other parts of the app work well with the Collection transform over array iteration and pushing. Why doesn't it here?
Update: The ->map() collection method behaves exactly same. Map, as opposed by transform, does not alter the collection itself. However, this should not be a relevant part within this application any way. I really can't understand what's going wrong. Is this possibly Laravel's fault?
Please note that transform method returns a Illuminate\Support\Collection.
It's better that you call all() after the transform to get an array result.
Like this:
...
return response()->json(['data' => $cAllRecords->transform(function ($oDatabaseRecord) {
/** #var $oDatabaseRecord DatabaseRecord */
$sActionsHtml = 'edit';
$sUrl = route('some.route', ['iDatabaseRecordId' => $oDatabaseRecord->getAttribute('od')]);
return [
$oDatabaseRecord->getAttribute('id'),
$oDatabaseRecord->getAttribute('updated_at')->toDateTimeString(),
$oDatabaseRecord->getAttribute('created_at')->toDateTimeString(),
$sActionsHtml
];
})->all()]);
#Cvetan Mihaylov's answer made me look at all the available collection methods (https://laravel.com/docs/5.8/collections#available-methods) and I found ->values() to return the values reindexed. And - that did the trick! :-)
return response()->json(['data' => $cAllRecords->transform(function ($oDatabaseRecord) {
/** #var $oDatabaseRecord DatabaseRecord */
$sActionsHtml = 'edit';
$sUrl = route('some.route', ['iDatabaseRecordId' => $oDatabaseRecord->getAttribute('od')]);
return [
$oDatabaseRecord->getAttribute('id'),
$oDatabaseRecord->getAttribute('updated_at')->toDateTimeString(),
$oDatabaseRecord->getAttribute('created_at')->toDateTimeString(),
$sActionsHtml
];
})->values()]);
Let say, I have 2 collection
first one :-
db.product_main
{
_id:123121,
source_id:"B4456dde1",
title:"test Sample",
price: 250
quantity: 40
}
which consist approx ~10000 objects (Array) and unique field is source_id.
Second :-
db.product_id
{
"_id":58745633,
"product_id":"B4456dde1"
}
which consist of ~500 and only have field "product_id" which is equals to "source_id" of db.product_main
now, i want to intersect two collection so that i only find those which don't exist in db.product_id.
db.product_main.aggregate({any query})
Just use the lookup stage to find the products associated with the 'product_main' collection and then match for empty array (i.e. records where no product_id was found)
db.product_main.aggregate([
{
$lookup: {
from: "product_id",
localField: "source_id",
foreignField: "product_id",
as: "products_available"
}
},
{
$match: {
products_available: {
$size: 0
}
}
}
])
On WRITE operations using aggregate pipeline You can also directly offload statistics update by using $out command and store cached result in product_stats collection (for example).
Later in web/ui/api READ operations just use this cached collection. Of cause, You can create database query methods for cached and non-cached results.
I'm quite new to Laravel and I'm trying to figure out how to properly work with Eloquent so far so good, but I'm stuck in something I want to do:
I have 3 tables in a database well 4 if you count the migrations one: food, food_group and portions which food_group and portion have the same structure which is and id as primary key and a name columns
the food ones have
| id(primary) | name | food_group_id (foreign key) | portion_id (foreign key |
all good because I have a nice formatted JSON with this in my route
Route::get('/read', function() {
$categories = App\FoodGroup::with('Foods')->get();
return Response::json(array('data' => $categories));
});
data: [
{
id: 1,
name: "Frutas",
foods: [
{
id: 18,
name: "Acelga",
cant_portion: 2
},
{
id: 19,
name: "Espinaca",
cant_portion: 2
},
]
}
]
and if I change the App\FoodGroup with App\Portion it gives me the same array but now ordered as the Foreign Key Portion
what I need is to first order with FoodGRoup and inside each item of the FFo_group one, now ordered by the second Foreign key which is Portion so I can have something like this
data: [
{
id: 1,
name: "Frutas",
portions:[
id: 18,
name: "Gr",
foods: [
{
id: 18,
name: "Acelga",
cant_portion: 2
},
{
id: 19,
name: "Espinaca",
cant_portion: 2
},
]
]
}
]
Your expected JSON shows foods as a child of portions. In order to do this, you need to setup this relationship.
On your Portion model, you need to setup the following relationship:
public function foods() {
return $this->hasMany(Food::class);
}
With this relationship setup, you can now get your data like this:
$categories = App\FoodGroup::with('portions.foods')->get();
This will load your food groups, then it will load the portions into the food groups, and then it will load the foods into the portions.
Edit
I may have slightly misread your question. I assumed you had a portions relationship defined on \App\FoodGroup. If not, you can add this like so:
FoodGroup:
public function portions() {
// the second parameter is the name of the pivot table.
// in this case, your foods table connects your portions and food groups.
return $this->belongsToMany(Portion::class, 'foods')->distinct();
}
Edit 2
This solution is a little hacky because it is treating the foods table as a pivot table, though it wasn't specifically designed for that. Because of this, there are multiple entries in the foods table that contain the same key pair values, and this is why you're getting duplicate related models.
If you throw a distinct() onto the relationship, this should take care of the issue, as it will eliminate the duplicates created from the inner join. The code above has been modified.
Based on the picture(ie your tables) you sent food_group does not have direct relationship with the portions so you can't chain food_group with portion like this
App\FoodGroup::with('portion.foods')
it should rather be (that is why you getting BadMethodCallException in Builder::portions())
App\FoodGroup::with('foods.portion')
because foodgroup has many foods and foods has many portion. so you can try something like this
App\FoodGroup::with(['foods.portion'=>function($q){
$q->orderBy('id')
}])->get();