interview question :- we will fist get the data from set of first two tables and store it in a varialble as $company=Member::find(1)->company; and then we will get the data from another set of table that is related to company suppose a table branch by $branch = Company::find($company->id)->getbranch; correct me if i am wrong.....
i wanna do all of this from the migration file, since you didn't mention it because you have already created all the tables manually which is bad as far as i know
Interview Question answer: we can replace hasOne by hasMany then we can get many members how have same id public function getCompany() { return $this->hasMany(Company::class); }
from the official documentation : "Eloquent determines the foreign key of the relationship based on the parent model name. In this case, the Company model is automatically assumed to have a member_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method:"
to fetch the data ex. if USER ORDERs some PRODUCT and admin wants to see the data in ORDERS table then we have to make many to many relation between USER, PRODUCT in ORDER Table. Beacause USER ORDERs many PRODUCTS.
Take this explanation with a grane of salt since I also am a beginner when it comes to Laravel and am not very knowledgable in how Laravel works behind the scenes. This is just my understanding of this: Relationships are defined in the Model. The Model does all the database connections in the background for you. The JOIN command returns one table with data from multiple tables. And at least in raw sql you may also choose which columns from which table to show and rename them in the resulting table (especially if there are conflicts like the column "name" inf multiple tables). Datasets are merged where the condition is true (usually that would be sth like member.id = company.member_id but can be anything really). Afaik the JOIN command does not specify what kind of relationship there is (One-To-One, One-To-Many, Many-To-Many should all work). So if it is a One-To-Many relationship the resulting data can also have multiple datasets with the same data for the employee (at least in sql) where only the company data is different (when the employee has multiple companies). The member->hasOne(...) or member->hasMany(...) functionality does not exist in sql and as shown in the video only returns data from the company table that fits that one member (one dataset if it is a One-To-One relationship or multiple if it is a One-To-Many relationship). Ofcourse you can reach the same result in raw sql through the JOIN command but you have to write a bit more. In general the JOIN command is bit more flexible in what you want to get as a result but also has more potential to make mistakes or transfer more / redundant data. The RELATIONSHIP is - once it it defined - in special cases simpler to use and thus less prone to mistakes but also has a narrow functionality. When you want more you may have to add steps putting more workload on the server-side with multiple redundant queries which you could do all in one more complex query including JOIN commands.
What if you have hundreds of tables? You don't need confusing set of model and controllers per table and don't need to load them in your router. I been using Laravel and CodeIgniter and uses only one model for all. Inside I have function called getData, insData and updData. Try to analyze this function and you will see how easy to use it. This can be used in blade or in controller and even in helpers. Using ONLY DB::select($qry) as my main function. // -- Start -- Data Collection // $tbl = "theTable c"; $tbl .= " join seconndTable as d on (d.id = c.dId); // if needed. $fields[] = "*"; // can be any $fields[] = "id"; // can be single field only. $filter[] = "id = ". intValue; $filter[] = "field1 like '". $value . "%'"; $params['limit'] = [0, 100]; // can be orderby, groupby, distinct.... etc $type = 0; // returns multiRow; 1 returns only single row. $mod = new nameClass(); $res = $mod->getData($tbl, $fields, $filter, $type, $params); // this will give you result. // -- End of Data Collection // // The Model class nameClass extends Model{ public function getData($tbl, $fields = [], $filter = [], $type = 0, $params = []){ $distinct = ''; if(array_key_exists('distinct', $params)){ $distinct = " DISTINCT "; } $qry = "select ". $distinct . implode(', ', $fields) . ' FROM '. $tbl; if($filter){ if(is_array($filter)){ $qry .= " where " . implode ( ' AND ', $filter); }else{ $qry .= " where " . $filter; } }else{ $qry .= " where 1"; } if(array_key_exists('groupby', $params)){ $qry .= " group by ". $params['groupby']; } if(array_key_exists('orderby', $params)){ $qry .= " order by ". $params['orderby']; } if(array_key_exists('limit', $params)){ $qry .= " limit ". implode(', ', $params['limit']). ""; } $rowData = DB::select($qry); if($rowData){ if($type == 1){ return $rowData[0]; } return $rowData; } return []; } }
Please support me by subscribe, like and comment :) thank you
great explanation
interview question :-
we will fist get the data from set of first two tables and store it in a varialble as $company=Member::find(1)->company;
and then we will get the data from another set of table that is related to company suppose a table branch by
$branch = Company::find($company->id)->getbranch;
correct me if i am wrong.....
i wanna do all of this from the migration file, since you didn't mention it because you have already created all the tables manually which is bad as far as i know
hey awesome understanding stuff great video keep it up and make lot about Laravel
Interview Question answer:
we can replace hasOne by hasMany then we can get many members how have same id
public function getCompany()
{
return $this->hasMany(Company::class);
}
Thank you so much
Nice Tutorial thanks
How is member_id is link with id coloumn in companies table? Please answer
I also want to know how it was happen
from the official documentation : "Eloquent determines the foreign key of the relationship based on the parent model name. In this case, the Company model is automatically assumed to have a member_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method:"
How to find all column from both table one to one
& we can connect more then two table many to many
what is the different between join and one to one relation?
Sir can you please tell me when and why we use relation ?
please response ..
Aceess data in one table to another table
to fetch the data ex. if USER ORDERs some PRODUCT and admin wants to see the data in ORDERS table then we have to make many to many relation between USER, PRODUCT in ORDER Table. Beacause USER ORDERs many PRODUCTS.
But we are not getting any data to the table 1??
what happens if id of table 1 and member id is different? (eg: table 1 id 2 and table 2 member id is 3)
"hasOne" would return "NULL"
"hasMany" would return an empty array
Yes ,we can get data from 3 tables and even more tables
how?
Please make a tutorial about next js
can you redo a video doing the relationship of 1on1 from the migration file !?
ruclips.net/video/CFTHlyBQeLo/видео.html
@@kabahblog thank you
@@kabahblog you should always check what is on the documentation it's always helpful!
hindi ma krdety yar mixup kr rehy ho kuch leacture hindi or kuch eng
Hi, Sir its not working
difference between JOIN and RELATIONSHIP?
Take this explanation with a grane of salt since I also am a beginner when it comes to Laravel and am not very knowledgable in how Laravel works behind the scenes. This is just my understanding of this:
Relationships are defined in the Model. The Model does all the database connections in the background for you.
The JOIN command returns one table with data from multiple tables. And at least in raw sql you may also choose which columns from which table to show and rename them in the resulting table (especially if there are conflicts like the column "name" inf multiple tables).
Datasets are merged where the condition is true (usually that would be sth like member.id = company.member_id but can be anything really).
Afaik the JOIN command does not specify what kind of relationship there is (One-To-One, One-To-Many, Many-To-Many should all work).
So if it is a One-To-Many relationship the resulting data can also have multiple datasets with the same data for the employee (at least in sql) where only the company data is different (when the employee has multiple companies).
The member->hasOne(...) or member->hasMany(...) functionality does not exist in sql and as shown in the video only returns data from the company table that fits that one member (one dataset if it is a One-To-One relationship or multiple if it is a One-To-Many relationship).
Ofcourse you can reach the same result in raw sql through the JOIN command but you have to write a bit more.
In general the JOIN command is bit more flexible in what you want to get as a result but also has more potential to make mistakes or transfer more / redundant data.
The RELATIONSHIP is - once it it defined - in special cases simpler to use and thus less prone to mistakes but also has a narrow functionality. When you want more you may have to add steps putting more workload on the server-side with multiple redundant queries which you could do all in one more complex query including JOIN commands.
What if you have hundreds of tables? You don't need confusing set of model and controllers per table and don't need to load them in your router.
I been using Laravel and CodeIgniter and uses only one model for all. Inside I have function called getData, insData and updData. Try to analyze this function and you will see how easy to use it. This can be used in blade or in controller and even in helpers. Using ONLY DB::select($qry) as my main function.
// -- Start -- Data Collection //
$tbl = "theTable c";
$tbl .= " join seconndTable as d on (d.id = c.dId); // if needed.
$fields[] = "*"; // can be any
$fields[] = "id"; // can be single field only.
$filter[] = "id = ". intValue;
$filter[] = "field1 like '". $value . "%'";
$params['limit'] = [0, 100]; // can be orderby, groupby, distinct.... etc
$type = 0; // returns multiRow; 1 returns only single row.
$mod = new nameClass();
$res = $mod->getData($tbl, $fields, $filter, $type, $params); // this will give you result.
// -- End of Data Collection //
// The Model
class nameClass extends Model{
public function getData($tbl, $fields = [], $filter = [], $type = 0, $params = []){
$distinct = '';
if(array_key_exists('distinct', $params)){
$distinct = " DISTINCT ";
}
$qry = "select ". $distinct . implode(', ', $fields) . ' FROM '. $tbl;
if($filter){
if(is_array($filter)){
$qry .= " where " . implode ( ' AND ', $filter);
}else{
$qry .= " where " . $filter;
}
}else{
$qry .= " where 1";
}
if(array_key_exists('groupby', $params)){
$qry .= " group by ". $params['groupby'];
}
if(array_key_exists('orderby', $params)){
$qry .= " order by ". $params['orderby'];
}
if(array_key_exists('limit', $params)){
$qry .= " limit ". implode(', ', $params['limit']). "";
}
$rowData = DB::select($qry);
if($rowData){
if($type == 1){
return $rowData[0];
}
return $rowData;
}
return [];
}
}