Skip to content
Advertisement

Tag: eloquent

Error while adding Eloquent class to User in Laravel

I want to make an eloquent model out of two table, pekerjaan and user. User hasMany pekerjaan and pekerjaan belongsTo User. How do I add eloquent’s method to the User class in Laravel? I have an error that shows: Cannot declare class AppModelsUser, because the name is already in use Here is my User.php model code: And here is my

variable references & in Laravel

In the following code, I noticed &$list is passed by reference in the loop, however $user and $request are passed by variables. Passing $list directly to the loop won’t change $list outside the loop scope. Why is this? Answer Actually its the core PHP functionality rather than Laravel itself, basically when you are passing variables without reference, it actually clone

fill already available values into CRUD edit form in Laravel

Let’s say, I have a Laravel application with articles which can have several tags associated (i.e. an n:m relation). Creating those records is working pretty fine and I can also retrieve the selected tags array. All the necessary relations are available and working in Eloquent or in the models. Now, I would like to create an edit form in this

Is there a better way to check for mandatory child in HasMany in Laravel?

I have a class AdBannerModel with an HasMany relationship to BannerLocalizedContentModel. When validating the model I call the method alterRulesForSaving where i want to check that the given AdBanner has at least the mandatory localizedContents. This is how I handle the check now, looking for suggestions. Answer you can check existence of relation with needed params

Laravel : Combine two tables using Model relations

i have two tables :: shops and attachments . Shops has all the details for shop and attachments with all the pictures for shop with ref_id as FK . now for mobile app i have to list shops with Shop title and one image. I can achieve it using leftjoin as below. This returns just shop name and pictures ,

query inside with eloquent

image address has location and total number of users. this query will return all the users and on relation whose query did not match will be null .I want to pull only those users which do not null value on relation and order By accordingly. Answer You can use whereHas condition

How to apply Eloquent where() to child in hasMany() relationship

I want to get the results of this very simple, common SELECT statement: For the non-SQL fluent, this returns all of the columns for all parent and child rows where the child column named column contains the value 1. Using the Laravel models below, what’s the correct Eloquent way to do this? Answer You were close with your last attempt;

Advertisement