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();
Related
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
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);
I an working on an API, in which I have 2 tables 1 for library and 2nd for issued books. We can issue 5 books to any user. I am working on an API in which I need to check if any user already assigned a book then he same book will not issue to him. I used a sub query for it. but it returns an array. So I can't get the data according my need. I need that if We issue 3 book to him than the books will show in other color. my code is:
$data1=(new \yii\db\Query())->select('book_id')->distinct()->from('issuedBooks')
->where('user_id=:user_id', [':user_id'=>310])->all();
$workout= (new \yii\db\Query())->select('*')->from('library')
->leftJoin(['issuedBooks'=>$data1],'issuedBooks.book_id=library.book_id')
->all();
$data1 return an array so my second query not working properly.
Preface: I know nothing of Yii Query Builder. But I do know SQL. And usually if you want to pass parameterized queries from an array you would list out the items of array with OR or IN() using the WHERE clause not JOIN.
Using above link, consider removing the joins and using a where clause in second query, passing the array, $data1 as a parameter:
->where(array('in', 'book_id', $data1))
I wanted to ask, how can i get data from one table and use this in other find.
For example, i have films table.
I want to get highest rated 3 films. Result should return 3 ID's.
Now, i want to create other query from not associated table, and pass this 3 ID's as "conditions" to find data in other table.
I dont want to use associations, because, data is stored in many databases, and this is problematic.
Thank You.
Once you've got your film IDs you can use in to filter the results from your other Model:-
$filmIds = ['32','55','75'];
$query = TableRegistry::get('Model')->find()
->where(function ($exp, $q) use ($filmIds) {
return $exp->in('film_id', $filmIds);
});
// WHERE film_id IN ('32','55','75')
Check out the docs section on advanced conditions.
If you need to get your film IDs into the correct format (i.e. that shown in the example code) you can use Hash::extract() on the results from your previous query.
if your cakephp version 3.x you can use subqueries in fairly intuitive way
$films = TableRegistry::get('Films')->find('highestRated')
->select(['id'])
->limlt(3);
$query = $related->find()
->where(['id' => $films]);
Subqueries are accepted anywhere a query expression can be used. For example, in the select() and join() methods. http://book.cakephp.org/3.0/en/orm/query-builder.html#subqueries
I was wondering if anyone knew how to get the metadata info from a Zend_Db_Table_Rowset class when using joins in the query that produced that result set? It's easy when there are no joins involved.. for example:
foreach ($rowset as $row) {
$info = $row->getTable()->info(Zend_Db_Table_Abstract::METADATA);
Zend_Debug::dump($info); // outputs array of column info including data type
}
But when I do that to a row that came from a query using joins I just get the data from the main table I was selecting from..
Jose, what kind of metadada info do you need? Maybe there is an alternate way to achieve what you want. Since the joins are always done from one table (and you join it to others) I think you will always get the metadata info for the first one.
By any chance are you doing dynamic joins?