Comparing columns in laravel - php

Sorry in advance for inappropriate question.I am a beginner in php and laravel. Wondering where i am doing mistake in the following code fragment.
$customer_vlan = Customer::select('vlan_id')->get();
$vlans = Vlans::where(function($query) use ($customer_vlan){
$query->where('id','!=',$customer_vlan);
})->get();
I have two tables in database."Customer" table has a column 'vlan_id'. In first query i am trying to fetch used vlan_id.
For second table "Vlans", column'id' holds all possible vlan. So i am trying to find which vlan's not used.

You are trying to compare id with a collection. Then the result is all of the Vlans.
$query->where('id','!=',$customer_vlan);
I think the best way to do this is loop through $customer_vlan collection and push $customer_vlan[$i]->id to an array. Then you can use this:
$query->whereNotIn('id', $arrayOfId);

Related

How can we use count function with joins in php laravel?

We are working on php laravel project where we find relational data using joins which we get correctly but we also want to find out count of this dafa which we get from joins but when we apply count function with joins then it gives same count value of all data which we get from database using joins.
this is the code and kindly let me know that what is the issue in this code thanks in advance.
I don`t get your question but I think you want count of the retrieve data.
1st method:
$variabel = Model::all();
View
$variabel->count();
2nd method:
$users = DB::table('users')->count();

Can't get result from where condition with variables in laravel

this is how i'm trying to get the type of certificate with where condition , but still recieve
nothing from this query:
$res= student::find($student, ['typecertificate']);
$k = certificate::select('id-cer')->where('name-cer','=',$res)->get();
return $k;
Based on your comments, I'm assuming you want to retrieve the field certificateType from the latest record that was inserted in the students table.
You can achieve that without a where clause, by directly using the Eloquent Builder to retrieve only that specific field like this:
Student::latest()->first('certificateType');
But this would give you an Eloquent Collection with one element. If you just want the value (not wrapped in a collection), you can simply retrieve the latest student and get the corresponding field directly:
$certificateType = Student::latest()->first()->certificateType;
I could explain more, but your question is vague and your database schema isn't clear either, so I'd need more information on that as well as what you intend to achieve.
In any case, Laravel's documentation is often a big help: https://laravel.com/docs/9.x/eloquent#retrieving-single-models

How to query whereIn inside json column in laravel 5.3?

I have been working with a laravel 5.3 version and in a table i have a json column where i have saved average rating of a category table.
so, A product category table has a column "detail" as a json data type which saves avgRating.
I want to run a query inside that json column. And i want to filter that category with a average rating. So, from the frontend side rating comes in a parameter with a comma seperated so that category can be filtered with multiple ratings.
$productCategory = ProductCategory::query();
$ratings = explode(',',$params['ratings']);
$productCategory = $productCategory->whereIn('detail->avgRating',$ratings)->get();
I want to achieve something like this.
I am using Postgres
It turns out that there was too much uncertainty at the time when the question was asked. Once the asker separated the problems and figured out that the raw query of
DB::select(DB::raw("SELECT * FROM product_categories where detail->>'avgRating' in ('2.0','4.0')"));
works in Postgres, the asker from that point onwards had a much easier time figuring out the actual solution in the where clause. I presuppose that it was
detail->>avgRating
instead of
detail->avgRating
but from the comment section that was not yet confirmed. But the moral of the story is that whenever one has an eloquent problem that might be related to the RDBMS, then it makes a lot of sense to first sort out the raw query and then, having solid knowledge about what should be generated, at that point one can switch to the Eloquent code and apply the solution there.
You should use whereJsonContains or whereRaw:
$productCategory->whereJsonContains('detail->>avgRating',$ratings)->get();
OR
$productCategory->whereRaw('JSON_CONTAINS(detail->>avgRating, ?)', $ratings)->get();

Need help writing an array_diff() query for mysql

I'm using PHP but that's not important. I have a variable that contains a standard array of positive int IDs. And I want to do a SELECT query with this array against a MySQL table to find out what IDs are not already existing in the data table. My first thought was to use IN() but then I realized that doing it that way I can only get a list of IDs that do exist not ones that don't. Of course with a list of IDs that do exist, I could compile it into a second array and then use array_diff() but I can't help wondering if there's another way to do it.
I decided to use unset()
SELECT QUERY
where (row) {
unset(id)
}

HBase - How to query based on key-timestamp-id?

All-
New to HBase and I've finally been able to actually take data I was once storing in MySQL (about 50 million rows) and insert it into my HBase table.
I'm now trying to query this data based on the keys and am running into some problems.
Basically I have a key that is constructed like:
objectname-createdtime-customerid
Now I need to query based on the objectname and a range for the createdtime, does anyone know how I can do this? (I'm using PHP/Thrift, but I don't need it to be a specific answer to this)
I can query if I know the exact row/key, I just need to know how to specify a range now for the middle property.
Thanks in advance!
Use a scan where the start row is the one with key objectname-<min created time>-customerid and the stop row has key objectname-<max created time>-customerid.
http://wiki.apache.org/hadoop/Hbase/ThriftApi#Scanner_methods

Categories