Laravel group by with select all column - laravel

Is there a way on laravel eloquent to query like this:
select users.* from users group by name
to implement it on code:
Users::select('name')->groupBy('name')->get();
Now I need to get other details (columns) of the user. Is there a way to query without aggregating every column on the model, this will not work:
Users::select('users.*')->groupBy('name')->get();

Try this
Users::groupBy('name')->get();
Since laravel is independent of the type of database you use, so your query should be eloquent.
For more details read this official documentation

this is working;
Users::select('users.*')->groupBy('name')->get();
just make sure your model name. You don't have to write class name, and if model name is User then change to
User::groupBy('name')->get();
Don't forget to import the model at the top according to your folder structure use App\User;

Related

Query builder with Laravel using Eloquent

I am very new to Laravel and quite confuse with the model and database thing. I understand that a model represent one table. So I created a model using artisan command without migration and it created the code as follow.
namespace App;
use Illuminate\Database\Eloquent\Model;
class RegCars extends Model
{
//
}
Since I have a different table name, so I add protected $table = 'regcars'; and assume that this model can now access the table by running query such as RegCars::where('user_id', $user_id); from controller. But I wasn't able to get anything by running it.
So I am wondering, how does this model able to run the query? Is the migration needed in order to do this? Is there still other area I need to set before I can run any query?
highly recommend to read docs or at least watch a tutorial.
what you get is a query build not a model or a collection of models. laravel(eloquent) doesnt exactly know what you want so you query database then get the data by methods that laravel gives you if assume you want a RegCars you need a method like first:
RegCars::where('user_id', $user_id)->first(); // now you have model
more info: https://laravel.com/docs/8.x/eloquent

Laravel 7 | how can i clone eloquent object and add entry in table with all relationship

I want to clone one product with all its relationship like price, attributes, images etc.
also, price and attribute have another relationship (nested relationships)
is there any easy way I can clone all this with few line of code?
You can use the replicate() function for the model itself. But this wont make a deep copy in terms of creating also the related entries.
https://laravel.com/api/7.x/Illuminate/Database/Eloquent/Model.html#method_replicate
You will have to write your own code where you get the related models and replicate them or you can use a package like: https://github.com/Neurony/laravel-duplicate

How to fetch specified attributes from $this->belongsTo() in Laravel Eloquent?

I want to fetch some particular field from table rather than complete attributes in $this->belongsTo(); I can't use select('field_name') or pluck('field_name') or DB::raw(count(),avg()) etc with belongsTo,hasTo() .....
How i solve this problem?
I can't use
$this->belongsTo()->select('fiel_name')
or
$this->belongsTo()->pluck('fiel_name')
Please check the reference : How to join three table by laravel eloquent model
In the above reference, They fetched all attributes in blengsTo(), But in my case i have to fetch only particular attributes.
You have to use it like this
$this->belongsTo('Model')->select(array('field_name'));
where Model can be for example User etc.

October CMS overriding list query

In backend I have a list of records. And this list displays all records of this type. It is possible to override this query?
In controller I have Eloquent query and i grab some data from database,
and i set this data in $query_data variable. Howe push this data in list view?
unfortunately, I didn't get exactly your point of telling override query, if you mean that set some condition in fetching data from database in Eloquent you have to use where for example in the example below we have users which their gender is male:
$users=User::where('gender','male')->get();
for set more condition you can do like this:
$users=User::where([['gender','male'],['status',1]])->get();
and for showing the variable in your view you can use compact method:
return view('YOUR BLADE FILE NAME',compact('users'));
hope be helpful bro

why model name in eloquent are not as same as table name?

When you create model in laravel for eloquent, the User model is treated like a users table. Why is that? Can we use an exact table name for the model?
I think more than anything it's convention. I don't see why you couldn't create a Users model for your users table, but a User is an instance of users, which is why it's done that way. You can always specify the table the model uses with:
protected $table = 'name_of_table';
which can be different than the model name. For example, a Data model can use the table userdata, as long as you specify that.
Hope that helps.

Resources