I have a complex business logic to retrieve data from a MYSQL database.
I am using laravel eloquent query builder to retrieve data but if I call a MYSQL procedure using raw query in laravel, I would not be able to exploit features of laravel like model relationships or pagination, etc.
What is the correct way to handle this situation? Should I write my logic in proc (use joins (model relationships) ) and use it as raw query or is there any better approach?
Related
I am trying to create a script that could help me generate a GraphQL schema with all fields and relations exist in the models.
if I could get that, php-lighthouse would help me for the rest of the work.
There is no way to get all relations of a particular Eloquent model without the user specifying them in some way, such as an attribute on the model that holds all relation names, or type-hinting those methods returning a relation. See laravel-eloquent-to-graphql for an example.
You can however get all fields of a model (that are saved in the database) by inspecting its table.
If you're looking for a GraphQL generator, there are Laravel Bakery and laravel-eloquent-to-graphql. Laravel Bakery generates a GraphQL schema on-the-fly, while laravel-eloquent-to-graphql generates a GraphQL schema statically.
There is also Lighthouse-Utils, which can generate queries and mutations for already-defined types.
I am new to Laravel 5.4 and working on some query manipulation. Now I have created an query using query builder which is shown below:
$view = DB::table('blocks')
->leftjoin('programmes', 'blocks.programme_id', '=', 'programmes.id')
->select('blocks.id', 'blocks.programme_id', 'blocks.name', 'blocks.colour', 'blocks.year', 'programmes.title AS programme');
I have two more table "dates" and "modules". Each dates as well as module belongs to blocks.
Now I want to fetch all blocks with programmes, dates and modules. I know i can use with() method to get all of these. But as per my knowledge on Laravel, I can use with() method only if I have model file of each table and have relationship between them.
But do not want to use model and define relationship between them. I just want to know How can I get block data with programmes, dates and modules without creating model and defining relationship betwen them in model? Is there any other way to use with() method without model?
Block dates and modules are conditional, Sometime I dont want get data of this table with block.
Please help me on this.
You can't do it automatically. Eager loading is only for Eloquent model so you cannot use it with query builder. However in most cases you can use Eloquent also for getting more complicated queries (you can also use joins when using Eloquent) so you will be able to use eager loading.
But if you don't want to use Eloquent at all, obviously you will need to create some custom mechanism for eager loading.
I have been looking into eloquent's source code to see how it manages building its data structure when eager loaded with one or more relationships specified but I have not been successful.
For instance:
User has many photos, and a photo belongs to a User
How does eloquent perform its SQL query?
Does it make multiple SQL? A query to get the user and then another one to get his/her photo, or does it perform a single query.
I am also interested to see the structure of the SQL, because I have tried lots of options and there is absolutely no way I could tell what tables each field belonged to because the returned result is just plain array of objects.
Laravel will create at least two queries when you'll try to use eager loading.
For example, this code:
$users = User::with('photos')->get();
Will produce two queries similar to these:
select * from `users` where `users`.`deleted_at` is null
select * from `photos` where `photos`.`user_id` in ('1', '2', ... '99')
I'd recommend you to use Laravel Debugbar, if you want to better understand how raw SQL queries look like.
Sometimes using toSql() in php artisan tinker tool is also helpful:
\App\User::where('id', 2)->toSql()
I've just started using Laravel and I'm coming from a different system using an existing database. In this system there are 2 users table, one stock with the CMS and one custom one.
I want to create an Eloquent model which retrieves data from both tables into one model. I know I can use the following to create a relationship.
$this->hasOne('App\SecondUser', 'id', 'id);
But this results in 2 sql queries, and I want to join results from 2 tables before returning the model, in one join statement. How do I do this?
That might be a bit more complicated that you would expect.
First of all you have to use \DB facade to join the two collections(arrays) and then recreate the Eloquent collection from these arrays using Collection's make method.
More info about the Collection class here
An easier way might be to join them using standard laravel relationships and user something like Model::user->with('relation')->get.
But this will still create 2 queries (still relatively fast).
I am very beginner to PHP and Laravel.
I am using laravel 5 eloquent ORM. I have an array $caseIDs and want to fetch the data from db where caseID (column name) matches from one of the $caseIDs elements. Can I use where() method of eloquent ORM? or How can i do it?
You need to use whereIn which simulates a WHERE IN('comma','separated','values')
YourModel::whereIn('caseID', $caseIDs)