Laravel Eloquent Relationships methods syntax - laravel

everyone:
I'm trying to create an application with several many to many relations, including a m-m rel onto the same model. It's a headache. In the official docs there is no so much information. I've been looking the code for belongsToMany method, in HasRelationShips.php, but there are not description on the parameters use.
Where can I get detailed documentation about the use of the parameters, in order to learn the right way to create any kind of relationships?
Do you know any book or document which details the working of all of the methods and parameters, so I can read it and REALLY learn how do they work?
Thanks everyone

Bro, in the DOCs you can find it easily.
Belongs to Many is like that:
public function companys(){
return $this->belongsToMany('App\Users', 'companys_users', 'user_id', 'company_id')->withTimestamps();
}
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many

Related

Laravel Controllers

I have a page for creating a driver and adding his cars - drivers/create. A driver must have at least one car.
So I keep all the logic of storing the driver info in DriverController#store, but I also need to save cars info. I feel it is not a correct way to store elements of a Car class in the DriverController.
What would be a correct (or just a better) way in my case?
You can create a public static store method in CarController, and then invoke the method in DriverController#store.
Or create a car in CarModule and invoke it in DriverController#store.
If you're using Eloquent ORM, Laracasts has a free video series on v5.4 from scratch.
Two of the videos show you one way to set up a one-to-many relationship, but using a blog with comments. You could follow their example but substitute blog with driver, and comments with cars.
Laravel 5.4 From Scratch: Eloquent Relationships and Comments
https://laracasts.com/series/laravel-from-scratch-2017/episodes/15
Laravel 5.4 From Scratch: Add Comments
https://laracasts.com/series/laravel-from-scratch-2017/episodes/16
There's also a lot of other ways to approach the problem, with the Repositories or Service Layers, but those can lead you down a rabbit hole.

scopes should be in controller or model?

I am new to ruby and rails development and have this question in mind. If i have a concern with scope created say latest_records which gives me latest data for a customer
Now what is the best practice to use scope in this situation. should scopes be in model or in controller?
I read some online articles and it talks about fat model and skinny controller and since scopes are do database related work then i am guessing they should be in model.
Any suggestions or thoughts?
You guessed it right.
Scopes belong to model and needs active record classs object to work on.
Scopes are nothing but a activerecord query divided in parts and it helps look your query elegant and dry.
e.g.
If you want to get users with confirmed emails, you would:
User.where(confirmed: true)
But with scopes in your user model:
scope :confirmed, -> { where(confirmed: true) }
And you would simply:
User.confirmed
For more detailed please refer this answer here
A scope could only be defined in a model so it would have to be on a model.

How Laravel models use function chaining?

I am working on Laravel models kind of thing, I am not able to understand how Laravel models use function chaining when the base model class is abstract. Please provide me some suitable explanation example. Thanks in advance.

A good explanation of Eloquent for Laravel 5.2

I am learning Laravel 5.2 and can see the huge potential of Eloquent, but I find some of the tutorials rather assuming that one understands things. For example I have a table authors and a table items. Obviously authors has many items and items have 1 author.
The various examples and videos tend to jump over this and what I would like to understand is the rules and a good source for setting up the relationships and more complex queries otherwise I will have to resort to using PDO classes of a more trandition manner.
Can someone recommend something please?
Personally I think the Laravel documentation has improved a lot and the topic of Eloquent is quite clear. For instance, your example is clearly a one to many relationship.
class Author extends Model
{
public function items()
{
return $this->hasMany('App\Item');
}
}
class Item extends Model
{
public function author()
{
return $this->belongsTo('App\Author');
}
}
As simple as that and so elegant. Would you mind rephrase what is it that's confusing to you if I am mistaken?

Can i use instances of a laravel model instead of facades?

I'm wondering whether it is possible/advisable to use instances of a laravel model instead of using the Facade. Why all this trouble? I have a model which will be used with many tables, and i want to be setting the model's table automatically using the constructor. Is it possible/advisable, or what is the best approach of achieving the same end?
I have researched around with no much success.
UPDATE
THis is the scenario: an exam system, where different exams are "created". after an exam is created, a table is created in the database under the name Exam_#, where # is the ID of the exam. I want to access all exam from one model: Exam, but you see the particular table the model is to use can vary significantly, so we cannot set the table variable statically. The model shall not know the table it will use until it(the model) is called. So thats why i was wondering whether i can be passing the ID of the exam when i am calling the model or something like that. I hope my question is now more clear.
At the end of this, Laravel is still PHP... Anything you can do in PHP can be done in Laravel.
is (it) possible/advisable to use instances of a laravel model instead of using the Facade?
You can achieve exactly the same results using an instance of the model as you would using the static facade.
$user = User::find(1);
$user2 = new User();
$user2 = $user2->find(1);
Both instances of the above model contain the same results.
Is it advisable? I really don't like the static facades at all, they bring with them more trouble than they are worth, especially when it comes to testing (despite being able to mock them, they create tight coupling where most of us need loose coupling). My answer to this would be: don't use the facades at all.
What is the best approach of achieving the same end?
As #JoelHinz suggested, create a base model with common properties and then use the models as they are intended. i.e. ONE table to ONE model and create the relationships between them. Don't use the same model for multiple tables, this is not how Laravel models were intended and you will lose a lot of the power Eloquent provides by taking the approach you mentioned.
Updates from comments
To get you started with testing in Laravel this is a good end to end tutorial Tutsplus Laravel4 + Backbone. Ignore the backbone part, what you're interested in is the testing parts that start about a 1/3rd of the way down the page. This will get you testing controllers straight away and introduce you to the repository pattern to create testable DAL structures.
Once you get the hang of writing tests, it becomes very easy to write a unit test for anything. It may seem like a scary subject, but that is purely down to not understanding how it works, it really is quite simple. Take a look at the PHPUnit documentation as well, it is an excellent resource.

Resources