Category : laravel-relations

This is a continuation of my last question. I like to create a relationship between a user (with an account type that’s equal to a “profile”) and my job posts. What I did was create a relationship like this in my models (not sure if correct tho) User.php public function jobposts() { $this->hasMany(JobPost::class)->where(‘account_type’, ‘profile’); } ..

Read more

Hi I want to implement this through relationships that show questions that the user of my site has registered answers and can see the questions in which he participated. I dont know what should I do. this is my controller public function index(){ $threads=Thread::with(‘answers’)->where(‘user_id’,’=’,auth()->user()->id)->get(); return view(‘answer-question.thread.thread’, compact(‘threads’)); } Column ‘user_id’ is in the answer table ..

Read more

$data[‘tutors’] = User::where(‘status’, ‘active’) ->whereRelation(‘role’,’name’, ‘teacher’) ->where(‘name’, ‘like’, "%" . $req . "%") ->where(‘first_name’, ‘like’, "%" . $req . "%") ->orWhere(‘last_name’, ‘like’, "%" . $req . "%") ->orWhere(‘description’, ‘like’, "%" . $req . "%") ->orWhereRelation(‘country’,’name’, ‘like’, "%" . $req . "%") ->orWhereRelation(‘state’,’name’, ‘like’, "%" . $req . "%") ->orWhereRelation(‘city’,’name’, ‘like’, "%" . $req . "%") ..

Read more

So I am trying to control output utilizing resources as I was told it’s the best way to model the data for api output. Customer Model public function invoices () { return $this->hasMany(‘AppModelsInvoice’); } Invoice Model: public function customer() { return $this->belongsTo(‘AppModelsCustomer’); } Customer Resource: public function toArray($request) { return [ ‘id’ => $this->id, ‘invoices’ ..

Read more

I use laravel 8 & have 3 table: Products, ProductPrice & ProductsPublisher: this is my Products model for this relationship: public function lastPrice(){ return $this->hasMany(ProductPrice::class)->where(‘status’,’active’)->orderBy(‘created_at’,’DESC’)->distinct(‘publisher_id’); } and this is my productsPrice model for publisher relationship: public function getPublisher(){ return $this->belongsTo(ProductsPublisher::class,’publisher_id’); } now, i want to use laravel resource for my api, i wrote products resource: ..

Read more

I’m currently trying to return a single value in the with(). Current code: class Compare extends Model { protected $table = ‘*******’; protected $with = ["steps", "compare_data"]; public function steps() { $StepsRelation = $this->hasOne("AppComparesComparesSteps", "compare_id", "id")->first(); $StorageModule = new StorageModule(null, null); return $StorageModule->readS3JSONFile($StepsRelation->steps); } public function compareData() { $CompareRelation = $this->hasOne("AppComparesComparesCompareData", "compare_id", "id")->first(); $StorageModule = ..

Read more

We are getting an issue in creating the relationship inside the laravel model. Requirement: We want to create a relation in such a way that we have three tables block_primary_table | block_id| block_name| |:—- |:——:| | 1| XYZ| | 2| ABC| block_translation_table |id|language_id| block_id| block_name| |:—- |:——:| —–:|—–:| | 1|1|1| XYZ-English| | 1|2|1| XYZ-Hindi| | ..

Read more

I’m trying to use hasOne to return a single value within my data set, however I can’t seem to return the single column as a value without returning the full object. What the object looks like when returned when you just return hasOne: protected $with = ["steps"]; public function steps() { return $this->hasOne("AppComparesComparesSteps", "compare_id", "id"); ..

Read more

I am using Laravel 8 and I have the following very simple models and migrations, Author Model <?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; class Author extends Model { use HasFactory; public function profile() { return $this->hasOne(‘AppModelsProfile’); } } Profile Model <?php namespace AppModels; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateDatabaseEloquentModel; class Profile extends Model { use HasFactory; ..

Read more

I have a function where I can get all sell records where between dates. Here is the function: private function getSoldsBetweenDates($days, $user, $filter_by) { $date_from = Carbon::now()->subDays($days); $date_to = Carbon::now(); return Inventory::where(‘inventory.client_id’, $user->client_id) ->withCount(["sellRecord as $filter_by" => function($query) { $query->select(DB::raw("created_at"))->take(1); }]) ->join(‘inventory_sell_records’, ‘inventory_sell_records.product_id’, ‘=’, ‘inventory.id’) ->groupBy(‘inventory_sell_records.product_id’) ->whereBetween(‘inventory_sell_records.created_at’, [$date_from, $date_to]) ->paginate(100); } But now I need ..

Read more

I trying to import the lines of the job request by using an Excel file but I don’t know how to store the id of the job request in the lines of it. I have 2 models that are Job Request protected $table = "jobs"; protected $fillable=[‘job_id’, ‘customer_id’, ‘note’, ‘created_by’, ‘updated_by’]; public function products(){ return ..

Read more

Goodmorning everyone, I’m literally losing my mind over an entity relationship issue with Laravel. I’m using the latest version of Laravel, 8.x. My data model is composed as follows. Parties Table id party_details_id party_type People Table id party_type (‘P’) name surname Companies Table id party_type (‘C’) name surname As you can see from the example, ..

Read more

This is about Laravel query. I want get some data from db. I used below code for it, $ids = [1, 2, 3, 4, 5] $stock = $item->stocks->whereIn(‘paperorder_paper_id’, $ids)->where(‘status’, ‘qc-pass’)->get(); My problem is if id no 1 has a record it should display that value otherwise should display zero ("0"). Because I want to put ..

Read more

I’ve this query to list all the users with their corresponding relationships. user has many tasks each tasks has many worktimes and these worktimes can have different users ie, each task can be shared by different users, hence they have individual worktime for each task. i’ve tried the below code $users = User::select(‘users.id’, ‘users.first_name’, ‘users.last_name’, ..

Read more

I have one to many relationship between two entities/tables. /** * Get all of the products. */ public function products() { return $this->belongsToMany(Product::class)->select( [ ‘products.id’, ‘products.title’, ‘products.sku’, ‘automation_products.automation_id as auto_id’, ‘display_order’, ] )->orderBy(‘display_order’); } When I want eager load this relationship, it seems like there are duplicate queries running in the background. I use this ..

Read more