Select the first 10 rows - Laravel Eloquent - php

So far I have the following model:
class Listing extends Eloquent {
//Class Logic HERE
}
I want a basic function that retrieves the first 10 rows of my table "listings" and passes them on to the view (via a controller?).
I know this a very basic task but I can't find a simple guide that actually explains step-by-step how to display a basic set of results, whilst detailing what is required in the model, controller and view files.

First you can use a Paginator. This is as simple as:
$allUsers = User::paginate(15);
$someUsers = User::where('votes', '>', 100)->paginate(15);
The variables will contain an instance of Paginator class. all of your data will be stored under data key.
Or you can do something like:
Old versions Laravel.
Model::all()->take(10)->get();
Newer version Laravel.
Model::all()->take(10);
For more reading consider these links:
pagination docs
passing data to views
Eloquent basic usage
A cheat sheet

The simplest way in laravel 5 is:
$listings=Listing::take(10)->get();
return view('view.name',compact('listings'));

Another way to do it is using a limit method:
Listing::limit(10)->get();
This can be useful if you're not trying to implement pagination, but for example, return 10 random rows from a table:
Listing::inRandomOrder()->limit(10)->get();

this worked as well IN LARAVEL 8
Model::query()->take(10)->get();

This also worked in Laravel 9
Model::query()->take(10)->get();

Related

Show countif mysql query table to html

I have a countif query in mysql and I would like to show the table on my html. I'm currently using laravel 6.0 framework.
Here is the picture of the table i want to show:
Here is my code in html:
Here is my code in the controller:
There should be numerous errors with index function in your controller. Specifically with how you are trying to assign $count a value. Read these: Eloquent Methods all() vs get(), Eloquent Selects, Eloquent Ordering, Grouping, Limit and Offset, Eloquent Where.
Laravel has an excellent documentation, if you were to follow it - working with Laravel would become much easier.

Laravel PHP ALIAS on Model?

I've been tackling on Laravel and I haven't found a few answers to minor questions.
Is there anyway that in the model I can create ALIASES for columns?
If not, I've been trying a few variations for my queries but none seem to give me what I want.
public function getEditHeader($adhID){
return View::make('adventures.editHeader')
->with('adventureheader', AdventureHeader::find($adhID)->select('adhID', 'adhTitle AS title', 'adhDescription AS description')->get())
->with('adventuretypes', AdventureType::orderBy('adtDescription')->lists('adtDescription', 'adtID'));
}
I've tried many variants but I feel like i'm not comprehending what I'm actually attempting with Laravel. I just want my queries to come out with alias because i dont have my tables prefix in the form's input names.
Use the selectRaw method:
AdventureHeader::find($adhID)
->selectRaw('adhID, adhTitle AS title, adhDescription AS description')->get()
Whoever is looking to achieve ALIASES please take a look into this link:
Laravel 4 Eloquent Column Alias

How to use laravel Eloquent to take just one field?

I am new in Laravel and I am watching a video tutorial that is teaching Laravel 3, unfortunately Laravel 4 is very different with laravel 3 and I don't know why? and I am afraid that maybe laravel 5 will be so different with laravel 4.
I am going to select all data about one field from my users table:
$user=new user;
$username=$user::find(1)->username;
return $username;
The top code is working true but just return the username of a user that it's id is equal to 1, But I want to do something like below:
$user=new user;
$username=$user::all()->username;
return $username;
This code has error $user::all()->username; and the error is :
Undefined property: Illuminate\Database\Eloquent\Collection::$username
You can use the lists() function, described here
It will return an array with all the values of one property.
(Also you don't need to create an instance to retrieve all users)
$usernames = User::lists('username');
If you'd like to have another column as key of your array (e.g. the id) do this:
$usernames = User::lists('username', 'id');
Don't be worried about Laravel 5. Some things will change but many things will stay the same. Including everything about Eloquent and the DB querying in general (at least as far as I know)

How to access db views using Laravel models?

I am porting a Postgres-backed web app to Laravel 4.2, and I can't see a way to utilize the existing eight or so summation views which are built in the psql db. These views preform various aggregation and averaging functions, and as such are properly part of the schema as they illustrate the relationships between the table entities.
Surely someone has had to use db views with the active record style interface of Laravel? How do you go about it?
Your question is about database views and if I'm not wrong then you are talking about the dynamic table that gets created on the fly, for example, in MySql, it's possible to create a View using something like this:
CREATE VIEW students AS SELECT * FROM profiles where type='student' ORDER BY id;
So, it'll allow to query the dynamic table which is the students view here, for example:
select * from students;
This will return the filtered data from students view. So, if I'm right about your question then I think you are able to use Eloquent just like you use for real tables, for example, to create an Eloquent model for students view you can simply create it using something like this:
class ViewStudent extends Eloquent {
protected $table = 'students';
}
So, now you can use this model as usully you may use for other tables, for example;
$students = ViewStudent::all();
It's just the same way. Since you asked for psql so I'm not sure about the syntax of that or how it works in that system but I believe it's possible same way.

Laravel 4 Build Query where clause on the fly

I am porting my code from CodeIgniter to Laravel. and have some question regarding the query builder.
In codeigniter, I can just add where clause to the active record object, as I initialize each property in a class like
$this->db->where('xxxx','bbbb');
in one property initialize function, and
$this->db->where('yyyy','aaaa');
in another property function, and it will all chain up until i fire off the query. But this doesn't seem to be the case of Laravel.
Here is what I do in laravel in each property initialize function
DB::table($this->table)->where('xxxx','bbbb');
DB::table($this->table)->where('yyyy','aaa');
and when a actual method is call from outside, it runs
DB:table($this->table)->get();
but this gives me a SELECT * FROM TABLENAME without anywhere clause. So what am I doing wrong here :x or I just shouldn't treat laravel same as codeigniter and think of something totally different to handle this kind of dynamic where clause?
Also in codeigniter, you can set a section of the query to cache, so even after you fire off the query , those section retains for next query, usually the where clause. Is there a similar function in Laravel? Thank you!
You can assign your current workings to a variable, and build upon that, let me show you an example based on your example:
Instead of this
DB::table($this->table)->where('xxxx','bbbb');
DB::table($this->table)->where('yyyy','aaa');
Try this...
$query = DB::table($this->table)->where('xxxx','bbbb');
$query->where('yyyy','aaa');
$results = $query->get();
I just shouldn't treat laravel same as codeigniter and think of something totally different to handle this kind of dynamic where clause?
This is not dynamic where clause.
and please, make a habit of reading the documentation.
From the docs of Fluent query builder
$users = DB::table('users')->where('votes', '>', 100)->get();
you can set a section of the query to cache, so even after you fire off the query , those section retains for next query, usually the where clause. Is there a similar function in Laravel?
$users = DB::table('users')->remember(10)->get();
Next time, just open up the docs. they contain all this.

Categories