Laravel Nested Relationship Get List of Ids in Column - laravel

I have encounter a problem that I am not able to solve :/
So, I currently have 4 Models: Make > Model > Generation > Version(['engine_type'])
I need to get a unique list of all the fuels (engine_type) that a certain car model has within it's versions. i.e: for $model = Model::find(1) I want all of it's $model->generations()->versions()->get('engine_type') as a list of unique ids so I can know which engine types this car models has!
Thanks in advance!

first step: you should get all generations for the current model, that would be achieved by join table 'models' with table 'generations' ....
second step: join the results with the table 'versions' witch contain the result column we desire to show ('engine_type')
this chain of join should use the foreign keys that represent relations between the tables, so you have to make sure that you setup up your relation well ...
third step: after selecting 'versions.engine_type' use 'distinct()' to make sure there is no duplicate result,
more about distinct in:
https://laravel.com/docs/7.x/queries#selects
$ids= Model::where('id',$modelId)->join('generations','generations.model_id','models.id')
->join('versions','versions.generation_id','generations.id')
->select('versions.engine_type')->distinct()->get();

Related

Insert id from table to another table in laravel +vue.js

first image
second image
I want to get Id from students table where i want insert this id in differs table . I don't really know how to explain it but maybe if you see my codes, you would understand. I have already finish making the relationship between this 2 tables already
my student controller
this my differ controller but I do not know any idea about if this code true or no
first: 'app\Differ'? but Differ is in App\Models
you can use hasOne(Differ::class); when Student model and differ in the same folder
the second thing you should know, when using functions of relationship in laravel such as hasOne, a foreign key should be named by student_id in Differ or
you show it in function like hasOne(Differ::class,'id_student','id')

make the display order based on another table value

I have two tables request and status_tracker. I want to list the row in top which has only one entry in status tracker.
I have the query in $user=DB::table('request')->select('request.*)->get();
I want to add the order based on status_tracker table. If request_id has count '2' in status_tracker table I want to display in top of the table
$user=DB::table('request')->select('request.*)(order may happened here)->get();
if ID 19,11,15 has 2 count in status_tracker it should display first and rest of them will display below as 19,11,15,18,17,16,14,13,12,10.
Is it possible by order the row based on another table field count in laravel?
You could use 'withCount' which will add a column named {relation}_count and orderBy with that, so try something like this:.
$user = App\Request::withCount('status_tracker')
->orderBy('status_tracker_count', 'desc')
->get();
You can read more about this on official Laravel docs
Note: Your model relationship should be well established in order for this to work, so make sure you have specified the relations between both tables :D

Laravel - Getting a collection from a table using "join" clause

LaraCSV uses Laravel Collections to easily export csv files from an Eloquent Model. The table I'm extracting the CSV from has 3 FKs from another table. The fields are ['Operator','Supervisor','Escort'] which relate to the PERS_ID from another table.
I need to create a Collection that considers the join clause needed for these 3 fields to contain the full name of the workers, instead of just the PERS_ID.
I've tried the following:
$excesos = ExcesosVelocidad::all();
Which gives me a Collection but does not allow me to perform the JOIN clause needed to bring the full names of the workers. Is there a way to do this?
You can use joins like this:
$excesos = ExcesosVelocidad::join('x', 'x.x', '=', 'x.x')->get();

Laravel - Get values from pivot table column, using pivot tables unique row ID

I'm trying to figure out if there's some way to get the values of two columns in a pivot table, based on a unique incrementing ID that I gave each row.
I have a Job model, a Location model, and an Application model.
The Job and Location have a many to many relationship with a pivot table "job_location". The two foreign keys it contains are job_id and location_id. However, I also gave every row a unique incrementing ID, jobloc_id.
If I only have the jobloc_id, how can I look up the values of the other two columns?
$jobLoc = $location->jobs->wherePivot('jobloc_id', '=', 1);
return $jobLoc->job_id;
Returns "Method wherePivot does not exist" even though I've seen references of people using it. All I have is the jobloc_id, and am trying to lookup the other two values based on this so that I can fully load the Job and Location to pull attributes from them.
Any pointers would be greatly appreciated. This is in Laravel 5.3.
If you're added ID to pivot table (jobloc_id for example) and you want to get row by ID, just use query builder:
DB::table('job_location')->where('jobloc_id', 1)->first();
That's the fastest and simpliest way to do that when you have unique row ID in the pivot table.

GAS ORM many-many relation with attributes

I'm using CodeIgniter and I'm starting to work with gas orm.
One of my m-n-relationship-tables using a composite key has also some additional attributes to the releation.
For Example:
Table teams, Table employees, and a m-n table which binds them together + adding the attribute role
Is it possible to get the attribute using GAS ORM?
Yes, it is possible.
Simply create a new relationship in one of the two tables you are going to link with the pivot table that refers to the pivot table itself as a has_many relation. (But dont do the linking stuff in the model file, eg:
ORM::has_many('\\Model\\User\\Role')
instead of
ORM::has_many('\\Model\\User\\Role => \\Model\\Role')
See http://ellislab.com/forums/viewreply/1050559/ for exact the same question.

Resources