I want to get multiple row data from table in controller .
like i have a likes table
columns are id and value
i want to get like this statement . select * from likes where id is this and value is that .
i don't really understand your question, but in order to " select * from likes where id is this and value is that ", try
App\Likes::where('id', this)
->where('value', that)
->get();
hope that helps. read more https://laravel.com/docs/5.5/queries#where-clauses
Related
I have a variable $id = $request->id, which stores a particular id.
I want to fetch data from the database according to this id, and also get data for id values lower than it in reverse order.
For example, suppose I passed an id value of 5.
I want to fetch 5,4,3,2,1 id's data from the database.
How can I do this when using something like an employee table?
So if you pass it a value, you want it to retrieve all entries from the table which match that value or lower? In that case it's just a simple where clause (https://laravel.com/docs/8.x/queries#where-clauses) assuming you have an Employee model which is tied to that table :
$employees = Employee::where('id', '<=', $request->id)->get();
If $request->id is 10, this will get all employees with an ID equal to or lower than 10.
$articles = Article::paginate(10, ['*'], 'pag');
What does the second parameter [*] above do?
The first parameters is the number of resources to be displayed by page.
The third parameter is the name of the query that will appear in the URL (i.e, "pag?=3").
What about "[*]"? I've used it for a long time without knowing what it does.
Don't tell me to search in Laravel Docs because I already did this and didn't find anything useful.
2nd parameter is select() method from Illuminate\Database\Eloquent\Builder which means select * from table ... limit 15.
You can specify which columns you want select from database.
For exaple $users->paginate(10, ['id', 'name']); -> select id, name from users ... limit 10
FYI: ['*'] is not fully qualified!
If you are using join in your select, it might be a problem if the columns with the same name are present in both tables. For example uuid, etc ...
In this case you should specify table name in select: ['table_name.*'] -> select table_name.* from table_name ... limit 15
Following is my query
$user = User::select(['uuid','name','about'])->with(['education','work'])->first();
this returns empty data for relationship education and work,
but if I remove select function from query I am getting data in relationship and it also returns all columns of user table which I don't want.
how can solve this problem
The problem is that relationships (with(...)) execute an additional query to get the related results. Let's say you have one to many relationship where users have many works. User::with('work')->find(1) will then execute these 2 queries:
select user where id = 1 and select works where user_id = 1.
So basically in order to be able to execute the second query (fetch relationship data) you need to include id (or whichever column you're referencing) in you select statement.
Fix:
$user = User::select(['uuid','name','about', 'id'])->with(['education','work'])->first();
Same principle in different forms applies to all relationships. For example in the inverse of hasMany which is belongsTo you would need to select the foreign key (for example user_id).
I have a many to many relationship between a student table and a apparatus table (A student performs on many apparatuses and an apparatus has many students). I have a student_results table that has a composite primary key (student_id and apparatus_id) and a 3rd field called results).
I have written a query to find all the apparatuses that a student DOES NOT have a result. The example I give is for student with id = 121.
The sub-query is.
SELECT apparatus_id, strapparatus_name FROM apparatuss
WHERE apparatus_id <> ALL(SELECT apparatus_id FROM student_results
WHERE student_id =' . 121 . ')';
I would like to write this using Eloquent (Laravel 4.1).
Any help greatly appreciated.
Assuming that you already have your Eloquent models Apparatus and StudentResult set, this is a way:
Apparatus::whereNotIn(
'apparatus_id',
StudentResult::where('student_id', 121)->lists('apparatus_id')
)->get('apparatus_id', 'strapparatus_name');
Or, if you don't have a model for your student_results table, you can just:
Apparatus::whereNotIn(
'apparatus_id',
DB::table('student_results')->where('student_id', 121)->lists('apparatus_id');
)->get(array('apparatus_id', 'strapparatus_name'));
I have user with name, location, created_at as important fields in table.
I want to retrieve for each user the latest location,i.e, I want something like this:
username location created_at
abc New York 2012-08-18 16:18:57
xyz Mexico city 2012-08-18 16:18:57
abc Atlanta 2012-08-11 16:18:57
only input is UId(1,2) array of userids.please help me to accomplish this.I just want to know how to write query using active record query interface.
Generally, this should be a standard way to solve this kind of problems:
SELECT l1.user, l1.location
FROM locations l1
LEFT JOIN locations l2 ON l1.user = l2.user AND l2.created_at > l1.created_at
WHERE l2.id IS NULL
The idea is to join the table with itself, and find those rows which don't have any row with the same user and greater created_at.
Of course, you should have (user, created_at) index on your table.
Now you should see how would that be represented in AR interface.
When
u_id
is the array of user ids, then
u_id.map{|i| User.find(i).location}
should be an array of the users locations.
You can Use
User.where(:uid => [1,2,3]).maximum('location')
which will create something like
SELECT MAX(`users`.`location`) AS max_id FROM `users` WHERE `users`.`id` IN (1, 2,3)