#### Category : eloquent–relationship

I have three tables in my project (User, Cases and Details). I have set up the relationships in the model like following: A user can file many cases. A case can have only one detail. A user can have many details. (Since he can file many cases) Now, the users can file cases and only ..

Read more

this is my table => table image //This is my controller public function listUserStripeActionableRequests(Request $request) { return$model = UserRequest::whereNull(‘deleted_at’) ->with([‘oldPackage’, ‘newPackage’]) ->get(); } userRequest.php public function oldPackage() { return $this->hasOne(Package::class, ‘id’, ‘old_package_id’); } public function newPackage() { return$this->hasOne(Package::class, ‘id’, ‘new_package_id’); } I need to get data according to package_type in the table if ..

Read more

I need some help with defining my models/relationships with Laravel/Eloquent. Generally my idea is that users are able to create items like todolists which can be categorized. Items can be shared among users but each user is able to have a different "view" which means that Items can be added to a category on a ..

Read more

i have a query that i want to return an object with its relationship, it has a many to one relationship, one order can have more than one drug i wrote a query like this $orders = DrugRequest::where(‘user_id’,$user_id)->with([‘drugs’])->latest()->get(); this gets the relationship but not the full rows, for example if an order has two ..

Read more

I’m facing one problem and I can’t find the perfect and best optimized solution. So I’m kindly asking for your opinion. Here’s what bothering me. I’ve got a serialized array from DB that looks like this: a:6:{s:13:"property_type";s:1:"1";s:16:"property_feature";a:2:{i:0;s:1:"3";i:1;s:1:"4";}s:19:"property_offer_type";s:1:"5";s:19:"property_built_type";a:1:{i:0;s:2:"10";}s:24:"properties_office_phones";s:2:"13";s:15:"property_labels";a:1:{i:0;s:1:"8";}} Here is the non-serialized version for more clarity: Array( [property_type] => 1 [property_feature] => Array ( [0] => ..

Read more

How can I get the total of the by the relationship I need to display the final total of each shops instead of displaying the individual order amounts. Here’s the relationship in the Venue model //! Orders public function orders() { return $this->hasMany(AppEntitiesOrdersOrder::class); } Here’s how my Dashboard Controller function class DashboardController extends Controller { .. Read more I have 3 Tables.1) Recent Views 2) posts and 3) users. i need to join these three tables with intermediate relation. recent table ———— id post_id user_id date posts table ———– id user_id post_title description date users table ———– id username image email date now what is need to get is all data from recent .. Read more I have 5 tables. User tabel => This table stores the data of users. Exam table => This table stores the data of exams. Feeds table => This table stores the data of feeds. Exam_User_Pivot table => This table stores the data of users and exams means the user_id and exam_id. Exam_Feed_Pivot table => This .. Read more I am using beyondcode/laravel-comments (https://github.com/beyondcode/laravel-comments) and I try to specify columns I want to get from commentator. here is the code:$comment = $post->comments()->with(‘commentator’, function ($query) { $query->select(‘commentator.name’, ‘commentator.email’); //or$query->select(‘users.name’, ‘users.email’); })->latest(); I am getting error that said Undefined table: 7 ERROR: missing FROM-clause entry for table "commentator" // or users How can I ..

Read more

I am using beyondcode/laravel-comments (https://github.com/beyondcode/laravel-comments) and I try to specify columns I want to get from commentator. here is the code: $comment =$post->comments()->with(‘commentator’, function ($query) {$query->select(‘commentator.name’, ‘commentator.email’); //or $query->select(‘users.name’, ‘users.email’); })->latest(); I am getting error that said Undefined table: 7 ERROR: missing FROM-clause entry for table "commentator" // or users How can I .. Read more I have this schema, how i can access it Admins -> packages -> package_unit -> units admins: id, … packages: id, admin_id,… package_unit: package_id, unit_id units: id, … access it from admin just like this admin->units Sourc.. Read more When using nova fields, I can pass an array of attributes that will appear regarding certain resource so I can view or edit. It mainly goes like this: public function fields(Request$request) { return [ ID::make()->sortable(), Text::make(‘Name’)->rules([ ‘required’, ‘max:255’ ]), BelongsToMany::make(‘Customers’), } The current issue is that I have a rather complex relation that includes ..

Read more

I have 3 models, Industry, Category, Machine and they are linked as following Industry can have multiple Categories, and a Category can be in only one Industry (industry Id stored in category table) A category can have multiple machines, and a machine can be in multiple Categories (using pivot table) Problem : I want to ..

Read more

i have four table in database which they names are: Category, User, Role and then Post every user belongsToMany roles and categories and each category belongsToMany posts, i must get all posts by role which logged into our application as you can see in below screen shot manager 1 and manager 2 belongsToMany programings, dart, ..

Read more

i have four table in database which they names are: Category, User, Role and then Post. in Category table i have another column as category_id which by this column i can have multiple child in category. every user belongsToMany roles and categories and each category belongsToMany posts, i must get all posts by role which ..

Read more

I have a Product Model with a following relationship: public function recommendedPricing() { return $this->hasMany(RecommendedPricing::class); } The Recommended Pricing Model is: protected$fillable = [ ‘sku_id’, ‘unit_type_id’, ‘base_price’, ‘min_billable_qty’, ‘max_billable_qty’, ‘discount_method’]; protected $with = [‘bands’]; public function bands() { return$this->hasMany(‘AppModelsRecommendedPricingBand’); } The Recommended Pricing Band Model is like: protected $fillable = [‘sku_id’,’recommended_pricing_id’,’start’,’end’,’percent_change’,’fixed_price’]; Now In .. Read more Morning, il would like to use Eloquent to make this request but i have an error.$ModelVars=Model1::with([‘Model2’ => function($query,$var){ return $query->where(‘field1’, ‘like’, ‘%’.$var.’%’);}])->get(); Could somebody help me. Thks in advance. Sourc..

Read more

This relationship is in my Maintenance.php public function contactedContractor() { return $this->hasMany(ContactedContractor::class, ‘maintenance_id’); } I want to use the relationship in a query in my controller$contractor_maintenances = Maintenance::whereHas(‘contactedContractor’, function ($query) { return$query->where(‘contacted_contractors.user_id’, ‘=’, 8); }) ->where(‘contacted_contractors.user_id’, $contractor_user_id) ->latest(‘maintenances.created_at’) ->get(); but the where clause is not working giving me this error Unknown column ‘contacted_contractors.user_id’ .. Read more I’m trying to order by a relationship field in the following way: Post::with([‘comments’ => function($query) { $query->orderBy(‘some_comment_field’, ‘desc’); }]); But this doesn’t work. Is there a way to do this without the use of ->join()? I use Laravel 5.8. Sourc.. Read more I’m trying to order by a relationship field in the following way: Post::with([‘comments’ => function($query) { $query->orderBy(‘some_comment_field’, ‘desc’); }]); But this doesn’t work. Is there a way to do this without the use of ->join()? I use Laravel 5.8. Sourc.. Read more I really need your help urgently. I have four tables. User Table (user_id) Orders Table(order_id & user_id) Order_detail Table(post_id, order_id, product_id) Product Table (comment_id) Now my question is this, How to create relationship between user table and product table? I have managed to create relationships between the other tables. Sourc.. Read more I have query like this SELECT * FROM ‘discussions’ INNER JOIN comments ON comments.commentable_id = discussions.id WHERE discussions.user_id = 1 ORDER BY comments.id DESC How to create relationships in laravel models Not with query builder like this DB::table(‘discussions’) ->join(‘comments’, ‘comments.commentable_id’, ‘=’, ‘discussions.id’) ->where(‘discussions.user_id’, ‘=’, 1) ->orderBy(‘comments.id’, ‘DESC’) ->get();“` Sourc.. Read more I have spent better part of the weekend tyring to solve this problem. This is what I want to achieve. I have Four tables. Country | User | Post | Comment. This is the structure of these tables. Country Table country_id (PrimaryKey) country_name User Table user_id (PrimaryKey) country_id (ForeignKey) user_name Post Table post_id (PrimaryKey) user_id .. Read more Using Laravel 8 I have 3 models (Album, Track, Playcount). One Album has many Tracks, and one track has many Playcounts (every 24 hours a job will run and update the playcount). Album Model public function playcounts() { return$this->hasManyThrough(‘AppModelsPlaycount’, ‘AppModelsTrack’); } Track Model public function playcounts() { return \$this->hasMany(Playcount::class, ‘track_id’); } Playcount Model public ..

Read more

I am trying to make my own api with Laravel 8. But I have run into some roadblocks. As you can see from this image (is a part of my ERD). I have the following relationships: a user has many pets, but a pet belongs to 1 user a user can input many weights, but ..

Read more