Category : eloquent–relationship

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’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 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

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 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 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

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 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