convert DB array output to Eloquent Model for using API Resources - laravel

i want to use DB::select and DB::table in my laravel project
how can i get Eloquent Models from them ?
they returns array and can't used with API Resources

Related

How to get columns of a model instance in Laravel?

Can I extract a specific column for a model instance in Laravel instead of using query methods statically? more clearly, instead of writing the code below:
MyModel::select('columnName')->where('id', $model->id)->first();
can we write?
$model->select('columnName');
Well, when you grab it from a model class, it is a Eloquent Object, and it has all attributes from the database.
But if you are talking about serializing that into an array or JSON everytime, then you need to modify your $visible array ;
if you wanna transform it for a specific case, then you need to do :
MyModel::select('columnName')->where('id', $model->id)->first()->pluck('columnName');

How to use eager loading with custom query builder in Laravel

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.

Is it good to use raw query instead of Eloquent in Laravel

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?

How to get M:1 relation in Yii2

I am trying to get related model however i cannot seem to find correct documention. In yii 1.x i can do $jobsprocess->category0, but yii 2.x tell me to do $jobsprocess->getCategory(). This does not return a model but an ActiveQuery. How can I return a model object?
In your query use $model = YourModel::find()->with(['category])->all().
All relations using the getRelation() functions can be access with the with() function, but without the get and lowercase first letter.
You can then access the relational data with $model->category.

Data processing using eloquent orm methods

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)

Resources