get value from database in string using query builder - php

so in controller I am getting a value from a input form in string i.e. product_name
return $request->input('product_name');
And on the behalf of this I want to get product_id of that product from database table using query builder
return category::where('product_name',$request->input('product_name'))->get('product_id');
problem is, I am getting the value in array form but i want this value in string
//output
[{"product_id":7}]
but i want it in string like
7
please help to achieve this in single line using query builder, thanks in advance

Use the value method to get a single value:
category::where('product_name', $request->input('product_name'))
->value('product_id')
Laravel 5.8 Docs - Queries - Retrieving Results - Retrieving A Single Row / Column From A Table value

This should work:
return category::where('product_name',$request->input('product_name'))
->first()->pluck('product_id');
It looks like you want only one entry. For this you should use first() instead of get().

Related

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 do make Where clues to compare input data with Columns

I want to get DATA from mysql TABLE by submit this multiple select value & compare with this columns.
http://prntscr.com/nmsmw1
http://prntscr.com/nmsnts
I followed this , but not working.
$Data=Employee::whereIn('skills', $request->skills)->get();
you can convert this sql query into laravel, So you can use FIND_IN_SET() in Laravel like as bellow query.
$data = Employee::whereRaw("find_in_set('".$request->skills."',employees.skills)")->get();

Yii2 - active record query search for a number in a string field

i have a string field that contains a json string in my table. lets name that tbl_table
lets say it has these 3 records in it:
id json
----------------
1 [123,45,1]
2 [23,4,5]
3 [7,8,9]
now I want to fetch records that contain a number in their json field (using active records in Yii2), so:
$res = Table::find()->where(['like','json','23'])->all();
this query returns these records:
id json
----------------
1 [123,45,1]
2 [23,4,5]
this is not actually a wrong result but what I want is to return only the record with id=2. how can I do this?
and so for ['like','json','5'] or ['like','json','4'] and so on? the numbers are similar but I want the exact number to be match.
and another question I want the last question answer with change in this part ['like','json',[4,5,6]].
in other words I want to pass an array to like operator and it automatically make an OR operation for each element. is it somehow possible? if not how can I iterate on array and make multiple Or_like conditions with last question functionality without multi query executions ?
use expression to use mysql functions and use JSON_CONTAINS function
use yii\db\Expression;
$expression = new Expression('JSON_CONTAINS(fied, value)');
$now = (new \yii\db\Query)->select($expression);
p.s json_contains begins from version MySQL 5.7
refs https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html
http://www.yiiframework.com/doc-2.0/yii-db-expression.html

how to add sub query in yii when subquery return array

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))

How to get the value of an UPDATE ... RETURNING query in postgreSQL in Doctrine DBAL

I'm trying to use this correct postgreSQL query working with Doctrine DBAL but so far I'm not succeeding, the query is:
UPDATE table SET field = field + 1 WHERE id = ? RETURNING field AS fieldName;
The query works ok but trying to recover the value from the RETURNING clause I found no way to get this value from the query result in DBAL.
I tried getLastInsertedId with the fieldName value and the return value of the call itself but no luck. Anyone knows a way to do that?
Thanks
Ok, after fighting a little and with the help of #daniel-v%c3%a9rit%c3%a9 I found the way to do that.
To retrieve the data after an update with returning in postgres you must execute the query like this:
$statement = $conn->executeQuery($sql, $params);
$statement->fetchAll();
This way with only one query you do the update and get the last value updated.

Categories