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.
Related
I am trying to see if a user id is contained in a json field in my laravel collection. In my postgresql database I have a user_id field which contains a list of integers that pertain to my user model and table. I am aware in eloquent I can use the whereJsonContains method but I am not finding an equivalent method for a collection. How can I go about searching this user_id column for a specific user id?
check the manual for the aviable methods list:
https://laravel.com/docs/8.x/collections#available-methods
In your case i guess that is about:
where
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
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;
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
My User model has many Target and vice versa.
Now I've got a given User and given Target and I want to access pivot data from their relation. The pivot column is called type
How can I achieve this?
On the relationships for both User and Target, tack on a ->withPivot('type') which will instruct Laravel to include that column. Then once you have your result set, you can access the field with $user->pivot->type.
If you're not iterating over a collection, but have a user and one of their targets and want the type field, you could use $target = $user->targets->find($targetId) and access the type with $target->pivot->type.
More at http://laravel.com/docs/4.2/eloquent#working-with-pivot-tables
Laravel 8.x
After accessing the relationship, you can access the intermediate table using the pivot attribute on your models
foreach ($user->targets as $target) {
echo $target->pivot->created_at;
}
Docs: https://laravel.com/docs/8.x/eloquent-relationships#retrieving-intermediate-table-columns
You can also limit columns by passing array as 2nd arg to simplePaginate
$query->users()->simplePaginate($per_page, ['users.id', 'users.email']);