How to use AND and where caluse in Laravel 5.2 [duplicate] - laravel-5

This question already has answers here:
How to Create Multiple Where Clause Query Using Laravel Eloquent?
(26 answers)
Closed 6 years ago.
Hey guys I am facing some problem I am using laravel 5.2 and I want to run a query like this:
SELECT * FROM Chat
WHERE (From='1234' AND Number='420') OR WHERE (From='420' AND Number='1234');
For that I am trying this but not getting the exact result:
$products = Chat::where('from', '=', $from)->where('number', '=', $number)->orwhere('from', '=', $number)->orwhere('number', '=', $from)->orderBy('id', 'ASC')->paginate(10);
How can I make it work there is no such document where I can generate a query like that.

You can try something like this
$products = Chat::where([
['from', '=', $from],
['number', '=', $number],
])->orwhere([
['from', '=', $number],
['number', '=', $from],
])->get();
with reference from here : How to create multiple where clause query using Laravel Eloquent?

Related

Could someone help me build the elequent query laravel

I am on laravel, I am developing a library. I would like to display a list of books with a publication date of less than 3 months. Could someone help me build the elequent query. I tried with the DB class but it doesn't work.
$today = date('Y-m-d');
$books = DB::select("SELECT * from books where(DATEDIFF($today, 'publication_date') <=90)");
You can benefit from laravel query builder and pass now()->subDays(90) as the point that the date must be greater than.
DB::table('books')
->whereDate('publication_date', '>', now()->subDays(90))
->get();
The carbon package that comes with Laravel is quite handy for these types of thing.
What you could do is the following:
$books = Book::where('published_date', '<=', now()->addMonths(3))->get();

how can I write complex queries in eloquent Orm?

SELECT * FROM user_fields WHERE (SELECT CITY FROM register_expert WHERE PERMISSION=1 AND ID=user_fields.ID_USER_FIELD)='$city_save'AND TITLE_USER_FIELD='$text_search_service';
Here is register_expert table image
And here is user_fields table mage
Assuuming,
userFields is your models.
userFields::selectRaw("user_fields.ID_USER_FIELD")
->join('register_expert', function($join) use ($city_save){
$join->on('register_expert.id', 'user_fields.ID_USER_FIELD')
->where('PERMISSION', '1')
->where('city', $city_save)
})->where('TITLE_USER_FIELD', $text_search_service);
Try this code.
I think the Eloquent query should look something like this but as I said in my comment the database and query is not well designed so I don't think it will work but just so you have an idea of how to make more complex queries in Eloquent.
$registerExperts = RegisterExperts::whereColumn('ID_USER_FIELD', 'user_fields.ID')
->Andwhere('PERMISSION', '=', 1)
->limit(1)
->select('city')
->get();
$users = User::where($registerExperts->get('city') , '=', $city_save)
->andWhere('TITLE_USER_FIELD', '=', $text_search_service)
->get();
And here's a good first article to see how to make complex queries using Eloquent ORM : Dynamic relationships in Laravel using subqueries by Jonathan Reinink

How i write this Query. Want not use Eloquent

Please can anyone tell me the query? I use Laravel 5.7 with mysql.
My Tables:
users
id
name
recipes
id
user_id
name
flavours
id
name
flavour_recipe (pivot)
flavour_id
recipe_id
-A User can have many recipes(one-to-many)
-A recipe can have many flavours and a flavour can be in many recipes.(many-to-many)
How i can get all recipes(with his flavours) from a given user?
I want use the Laravel Querybuilder, not Eloquent.
I was trying this, but its not working:
$allRecipesFromUserX = DB::table('flavour_recipe')
->join('flavours', 'flavours.id', '=', 'flavour_recipe.flavour_id')
->join('recipes', 'recipes.id', '=', 'flavour_recipe.recipe_id')
->join('users', 'users.id', '=', 'recipes.id')
->get();
If i understand your question correctly, you want all recipes for a given user.
If i take a look at your query, you are starting from the pivot table, i dont think this is the right approach. Also you do never declare a WHERE clause to limit it to the specific user.
Take it step by step. You want to get everything for a specific user, so lets start with the User table
$allRecipesForUser = DB::table('users')->where('id', $id)->get();
A User can have many recipes
$allRecipesForUser = DB::table('users')
->join('recipes', 'users.id', '=', 'recipes.user_id')
->where('id', $id)
->get();
And a recipe can have many flavours and a flavour can be in many recipes
$allRecipesForUser = DB::table('users')
->join('recipes', 'users.id', '=', 'recipes.user_id')
->join('flavour_recipe', 'recipes.id', '=', 'flavour_recipe.recipe_id')
->join('flavours', 'flavours.id', '=', 'flavour_recipe.flavour_id')
->where('users.id', $id)
->get();
I hope this works out for you. Let me know if this is not what you need.

Laravel Sorting Data with Relationship and Pagination

I need sorting the Records data according to Relationship.
I am trying below Query.
$data = Lead::with('bdm', 'status_code', 'bdm.bdm')->get()->sortByDesc('bdm.bdm.name');
It works fine but I need data with pagination which is giving by Laravel 5 by default.
So If I am trying with below query . It is giving error.
$data = Lead::with('bdm', 'status_code', 'bdm.bdm')->pagination(20)->sortByDesc('bdm.bdm.name');
I am trying an other way to do the same task. It works fine but it is not sorting the records.
$data = Lead::with(['bdm','status_code', 'bdm.bdm' => function ($query) {
$query->orderBy('name', 'desc');
}])->paginate(20);
So kindly can anyone give me solution how to adjust this query.
Any Help will be appreciated.
Thanks
You should be using a join statement to do that.
If I were to assume your column names it should look something like this:
$data = Lead::with(['status_code', 'bdm.bdm'])
->join('bdms', 'bdms.id', '=', 'leads.bdm_id')
->orderBy('bdms.name', 'desc')
->paginate(20);
And the first bdm parameter in your query is redundant. It will be handled during bdm.bdm anyway.

Multiple inner joins using laravel and translation

I have problem using translatable library, I am trying to sort the companies by name, so as written in the doc, I need to use inner joins, I tried this code, everything is good but the data is messed up when I add voucher_translations, its like it get from different table or records, I dont know what I am doing wrong :(
$vouchers = Voucher::join('company_translations', 'vouchers.company_id', '=', 'company_translations.company_id')
->join('voucher_translations', 'voucher_translations.voucher_id', '=', 'vouchers.id')
->where('company_translations.locale', '=', 'en');
->where('voucher_translations.locale', '=', 'en')
->orderBy('company_translations.name', 'asc');

Resources