I want to get specific column from Model relation in Laravel, but the relation model returning empty array. Eloquent Query Medicine Modal Answer You always need to also select the primary and foreign keys of the table to make the relation work:
Tag: eloquent
find() does not get the existing row
I have a resource Controller with this index method like this: But it returns this error: Trying to get property ‘slug’ of non-object And when I dd(Category::find(25), Category::find(24), Category::find(23), Category::find(22)); I get NULL results. Meaning that it can not find data with specified ids. However there are 25 records stored at the categories table: So what is going wrong here?
Laravel Livewire error “must not be accessed before initialization”
I try to using Livewire Binding Directly To Model Properties, and i get this error, Anyone help please ? Thank in advance. Typed property AppHttpLivewireV2SettingsLocations::$country must not be accessed before initialization Answer When you define the type of a variable in PHP, you need to provide it a default value. If your component is creating a Country, you can set
Laravel API Resourse returning relation only on ‘collection’ method
guys I’m new to Laravel. I currently building an API, and I have a couple of endpoints (the one that we will discuss are the Get All Tasks and Get Single Task) The problem is that whenever I call the Get All Tasks endpoint it returns me the task resource + the user resource there. However when I call the
How to get foreign key from eloquent collection in laravel
I have hasMany relationship in my User model; I need to get a foreign id in Eloquent data Controller; Expected output; 1 How can I get the foreign key? Answer You can do the following. Since posts has hasmany relation so it return collection of object even though you have one item in posts. or If you still need one
Laravel does not show all the available results from table
I am working with Laravel 8 to develop online ordering project and in this project, I want to show all the orders from orders table if the status of each order is set to awaiting, so here is my code: But now the problem is, it only shows the first result from table, however there are more than one record
SQLSTATE[HY000]: General error: 1005 Can’t create table `school`.`posts` (errno: 150 “Foreign key constraint is incorrectly formed”)
before everything, I tried hard many websites and forums to solve this problem until these posts related to this problem in StackOverflow, but I can’t solve the problem. I want to create to one to many relationship between Post and Category model but I get that error code. SQLSTATE[HY000]: General error: 1005 Can’t create table school.posts (errno: 150 “Foreign key
How to calculate balance when the records I want to show in desc order?
I want to to show the recently created amount at the top but also I want to show the balance column, the balance column will add or subtract the amount from the record Controller Blade The problem with this is it starts adding from top, which I don’t want. I want it starts adding from bottom Expected results are marked
Insert a value at a NOT NULL column int in SQL Laravel Eloquent
I need to have this eloquent inserting data into my table with my first two columns, id, category_id be added with auto increment, specifically, the highest value + 1. my code above has the error: what can I at the “max()” for this to function as intended? any help would be appreciated thanks. Answer you have error in I think
Eloquent “where” on relationship returns wrong models
I have this method on my model “Employer”: I thought it would work fine, but it ends up giving me models that aren’t even related to my Model. Like, the id of the model is 37 and it gives me the relationships of all the models (like id 6). Any idea of why this would happen? Answer I think you