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

$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 . "%") ..

I have a Model named Topic. My topics have many views and many comments. Is there in any way I can combine the length of the views and comments and then sort the topics based on the total length of its views and comments? I also figured out that I also cannot use SortBy and ..

I have an orders table. Each order has many messages posted by different users. I need to display these messages grouped by each user under that order so I can use a foreach loop for each user. What I tried is this: Order::with(‘notes’)->find($order_id); With this, what I get is the order with the messages and .. Read more I want to change my model name and it’s database table name in a laravel project, but I’ve connected to other models with relationships. Now I wanna know if is there any way or artisan command to change it so it do not make problems for it’s connection? Thank You. Sourc.. 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 //controller$promotion = Promotion::findOrFail($id); //return Array ( [id] => 2 [en_title] => promo1 [game_id] => Array ( [0] => 3 [1] => 4 [2] => 5 ) [amount] => 100.00 [start_at] => 2021-02-22 [end_at] => 2222-02-22 [status] => 1 ) //model promotion class Promotion extends Model { use HasFactory; protected$guarded = []; protected $casts .. 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 I am trying to prepare a structure for E-Commerce. I need your help with something. I have the products and product types model. The database structure is as follows; Product Table ID Group_ID Name 1 1 Test Book 2 2 Online Lesson ProductType Table ID Name 1 Book 2 Lesson Teacher Table ID Name 1 .. 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 work_orders is hasMany() relationship. foreach ($deal->work_orders as $work_order) { if ($work_order->status != completed) { return 0; } else { completeDeal($deal->id); } } I want to check If All Of the Orders in a deal are completed then run a function to complete a deal. If any of work orders is not completed Then Just Do .. 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; ..

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

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

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

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

I have SaleInvoice model, Sale invoice belongs to User model and User model belongs to another model called Client. On the User model I am using eager loading to load the Client class User extends Authenticatable { /** * The relations to eager load on every query. * * @var array */ protected $with = .. 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’, ..

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 i have a probleme with implementing laravel relations in react. Let’s say i have one to one relation(One user has one profile) and i’m sending user object to React via API. In blade, it was too easy to access user’s profile by doing:$user->profile->bio But, how can i do it inside a react project? Thx ..