Laravel Relationship is actually trigger mysql? - laravel-5

class Product extends Model
{
/**
* Table name
* #var string
*/
protected $table = 'products';
/**
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function buyerSupplier()
{
return $this->belongsTo('App\Models\Buyer', 'buyer_id');
}
}
Above is example code, if I call $this->buyerSupplier()->get() will it run MySQL?
Please, someone, tell me how this Laravel relationship is working?

The answer is Yes Laravel runs mysql queries behind the scene, here is a brief description from Laravel Official documents,
Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems.
The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.
And about the relationships queries Laravel uses Eloquent:
Eloquent relationships are defined as methods on your Eloquent model classes. Since, like Eloquent models themselves, relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities.
And for more details about how to choose between relationships methods
read more from Laravel Official Documents

Related

Has break on cross DB connection

Laravel Version: #.#.# 8.18.1
PHP Version: #.#.# 7.4
Database Driver & Version: MySQL 5.7.32
Description:
Had two models (menu, service) with two different databases, connection set in model. when I get menu model with (service) relation, it's working fine. But when used has(service) to get the menu which have service will shown
Base table or view not found: 1146 Table 'common.services' doesn't exist
it will look in my first database
common is my first database name and services are store in second database.
Steps to Reproduce:
create two model in different database
fetch menu with relation has(service) to get only menu which have service
Documentation
From Laravel Documentation :
Eloquent does not currently support querying for relationship
existence across databases. The relationships must exist within the
same database.
QueryBuilder VS Eloquent
What Cause success retrieve on with() and failure on has() ?
QueryBuilder crosses Databases
with() has been implemented in Illuminate\Database\Eloquent\Builder and collects relations in $eagerLoading array to load with query.
Relation has not implement cross Database Feature
but has() is in Illuminate\Database\Eloquent\Concerns\QueriesRelationships trait and adopted in Illuminate\Database\Eloquent\Relations\Relation calls getRelationWithoutConstraints() which is :
/**
* Get the "has relation" base query instance.
*
* #param string $relation
* #return \Illuminate\Database\Eloquent\Relations\Relation
*/
protected function getRelationWithoutConstraints($relation)
{
return Relation::noConstraints(function () use ($relation) {
return $this->getModel()->{$relation}();
});
}
as PHPDoc says, its just getting base query instance for has relation .
its a simple method JUST creating a simple Base Query based on getModel() query (parent query) and add it.
The solution can be achieve using DB::raw.

Query builder with Laravel using Eloquent

I am very new to Laravel and quite confuse with the model and database thing. I understand that a model represent one table. So I created a model using artisan command without migration and it created the code as follow.
namespace App;
use Illuminate\Database\Eloquent\Model;
class RegCars extends Model
{
//
}
Since I have a different table name, so I add protected $table = 'regcars'; and assume that this model can now access the table by running query such as RegCars::where('user_id', $user_id); from controller. But I wasn't able to get anything by running it.
So I am wondering, how does this model able to run the query? Is the migration needed in order to do this? Is there still other area I need to set before I can run any query?
highly recommend to read docs or at least watch a tutorial.
what you get is a query build not a model or a collection of models. laravel(eloquent) doesnt exactly know what you want so you query database then get the data by methods that laravel gives you if assume you want a RegCars you need a method like first:
RegCars::where('user_id', $user_id)->first(); // now you have model
more info: https://laravel.com/docs/8.x/eloquent

How to define eloquent relationship without using model in Laravel?

Following the official documentation it is very easy to define a relationship between two models. However, One of my models just need to access data from 4 different table in a one to one basis. Those tables will have no functionality. So, I don't want to build a model for them.
For example I can do this,
class User extends Model
{
public function phone()
{
return $this->hasOne('App\Phone');
}
}
Here to complete this relationship successfully I need to have a Phone model. However, if I don't have a model can I use just the table name to define relationship? I want to use something like, return $this->hasOne(DB::table('phones');.
I reckon, if I can somehow use this I will have less models to work with which will make my code more manageable. Is it a good practice?

get laravel eloquent model relationships

is there any way to get the defined relationships in eloquent model. I have a situation where I need to get the model relationships so I can update all other eloquent models that relies on a specific id before delete it
There's no unified method to iterate over all registered relationships of a class. You can, however, access all the currently loaded relationships of a model instance (via the ->relations attribute or the getRelations() method), but that's not what you're up to. I'd suggest you take a look at laravel's documentation on inserting and updating relationships. So far that's the best laravel provides out of the box, the rest is developing approaches.
Try this function:
public function getRelations()
You can use
$model->getRelations()
function to get all relations
Also refer below link for details https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_getRelations

Laravel - Eloquent ORM - existing database

I'm trying to learn about laravel / eloquent orm.
How does one implement an existing database/table with eloquent. Any examples out there? Everything out there shows how to create database/tables. I'd like to use existing data structure and implement classes.
Try reading the docs, creating tables is only handled by migrations. Eloquent can work with any pre existing schema
Pretty much top of the Laravel Eloquent page http://laravel.com/docs/4.2/eloquent#basic-usage
class User extends Eloquent {
protected $table = 'my_users';
}

Resources