Use whereIn and with in the Eloquent Model of Laravel - laravel

I have a relationship as Restaurant has Many Jobs
I have properly set the methods in Restaurant and Jobs model.
I am creating a query where the job.status is in active or upcoming and with that I need restaurant data too.
Below is my code:
Job::whereIn("status", ['upcoming', 'active'])->with(['restaurant'])->get();
with this I am getting restaurant as null
As I am novice in Laravel, I tried the other way around also.
Job::with(['restaurant'])->whereIn("status", ['upcoming', 'active'])->get();
But it gave the same result.
However, if I use where it works properly.
Below is the code which is working and providing restaurant data.
Job::where("end_date_time", '>=', $currentDate->format('Y-m-d H:i:s'))->with(['restaurant'])->get();
I saw the documentation and I found that whereIn is Collections method, so is it something that it will not work if we have with?
I searched on google any example of whereIn and with, but I didn't found any.
May I am doing some conceptual mistake in understanding whereIn and with
Your help is much appreciated.
Thanks.

Related

How to resolve Not unique table/alias error in laravel?

I am very new to the laravel,i am using l5-repository package for orderBy and sortedBy to sort columns ,while hitting API i am getting following error please help me to resolve the issue
my API URL :-http://localhost.com/v1/domain?limit=250&page=1&orderBy=users|name&sortedBy=desc
You probrably have an error in your table name. For default, laravel will look for cards table name for a model named Card. Also, take a look in your models name. I espect you misstype card.php because it should be Card.php. Laravel uses a lot of psr convention and Eloquent (who deal with relationships and that stuff). If you are not well versed into it, take a look in how to name classes. As Alireza said, take a look in the Laravel's documentation, it is awesome and complete. When you have a one to many realtionship, one of they should have a hasMany(...) instead belongsTo(....)

Laravel Modeling System

I have been using Laravel for a couple weeks now and I love the framework. However, with models is there any actual robust system? Creating models, like the user model, just seems to be string arrays of what you do and do not want modified in queries. The place I see validation of the models is in the controller (through $this->validate()) and/or migrations creating the tables.
I just wanted to know if there is a certain place I am missing or not implementing. My brain is referencing something like ASP.NET's decorators that add validation to data models. Thank you!

Missing understanding for pivot tables on Laravel

I know the relationship with table pivot, but i don't understand:
Is the pivot a table that I have to create on my database? Or is it a virtual table that laravel provides to us?!
Beyond that: is better use pivot relationship or use my own third table to relationship many to many?
You will likely only need a pivot table for a Many To Many Relationship, if it is a One to Many you would only need to include a foreign key. However, Laravel is flexible enough to allow you to do whatever you feel like doing as there is no right way to do anything in Laravel.
Personally, I would say it's better to stick to the best practices/conventions as you will find it easier to get help form the community than if you roll your own custom solution that no one else understands.
If you haven't already heard about Laracasts, it's a truly fantastic resource for building things with Laravel. I have been using Laravel for several years now and I still learn new things watching Jeffrey's videos.
If you can't afford the subscription, you can still watch each "What's new in Laravel" for free going back to Laravel 5.0 or further but I don't know how useful they would be. Which should give you a good overview of some of Laravel's best practices and conventions.
Jeffery also has a free series on Learning Vue.js 2 which you should also look into and he'll get to using Vue together with Laravel.

Models responsability

I have a doub about Laravel. The models are used to define the relationships between the models like hasMany, belongsTo, etc. Also the models are used to define the fillable fields. But he models are only for that? Because I already check some examples that it seems that some queries are executed in the models instead of the controller so Im not understanding if the models should also have the querying of the relationships or not. Can you give a help to understand better what is the correct use of models (what should be placed in the models)?
Its same way to execute queries on model or controller. Written queries in model make your controller more clean. We can write mutator, accessor or query scope in eloquent model. Correct me if I'm wrong.
Visit https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html
You can refer this documentation, it's quite helpful if you want to know deep basics and responsibilities about particular part in Laravel.
Models are mostly used to make an outlook of the data i.e what fields are going to be saved in the database and we also use it to associate the relationships with other related data as you already know but we also use it to alter the values that are either going in/out of the data base which you can check in the documentation in link bellow https://laravel.com/docs/5.6/eloquent-mutators
the main purpose is to divide the code between controller and model (were it best fit to be)

Laravel: What are models?

I'm confused as to what models are and do in Laravel. I've tried to find some explanations but couldn't find any.
Can someone briefly explain what models are, when I would use them, and why I should use them?
More so, what are fillable and guarded attributes? I don't find they're very well explained in the docs.
For example, I have a table in my database, called login_log, that contains all login attempts. Would I create a model for this? Why?
Model is represented by M when you talk about MVC which stands for Model, View and Controller.
In Laravel Model is simply your database table object. This allows you to interact with your database tables as if they are PHP objects or classes.
Fillable property is used to tell laravel to allow mass assignment for the listed fields
while Guard property is the opposite of fillable
Laravel documentation is the best documentation so far.
See this Links to Understand well : Mass Assignment in Eloquent ORM for Laravel 4.2
Suggestions:
If you are newbie in Laravel, as I am Android Application Developer
i've find solution and understood too.
You have to learn documentation before putting question.
As MVC stands for Model View Controller, Model Deals with Database for example controller ask to Model to give me the first names of Students from the student table an d then controller pass it to the view.

Resources