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)
Related
I am trying to write this query with laravel orm to use the eager loading but not getting it.
SELECT p.* FROM tbl_posts as p,tbl_posts_requests as pr WHERE p.post_status='active' AND (p.post_id=pr.request_post_id OR p.post_private='no' )
I have a comment table to eager load with it but i am not able to create a laravel ORM version of this query
Eloquent ORM eager loading happens for Eloquent models with specific relationships.
So assuming you have a tbl_posts_requests relationship in your Posts model, it could be something like this:
Posts::where(['post_status' => 'active', 'post_private'=>'no'])->with('tbl_posts_requests')->get();
I am assuming you called your relationship function tbl_posts_requests there.
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
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 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?
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).