How can I use Laravel load function with DB::table as following
DB::table('table_name)->where('field', 'value')->load('relation_1')->get();
or an alternative to this statement
You can write directly like
DB::table('users')->get();
or
DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
Post::all()->where('field_name', 'value')->load('relation_1','relation_2');
I could not use load function with DB::table
I created a Model named Post and I used the where function and load
Related
Instead of get() I would like to use findOrFail but it doesn't work that way.
So what is the best way to return a 404 response if the row doesn't exist?
$log = DB::table('dmlog')
->select(
'dmlog.*',
'membership.membership',
'department.department',
'category.category',
'communication.communication',
'room.room AS room',
'room.category AS room_cat',
'roommove.room AS roommove',
'roommove.category AS roommove_cat'
)
->join('membership', 'membership.id', '=', 'id_membership')
->join('department', 'department.id', '=', 'id_department')
->join('category', 'category.id', '=', 'id_category')
->join('communication', 'communication.id', '=', 'id_communication')
->join('room', 'room.id', '=', 'id_room')
->join('room AS roommove', 'roommove.id', '=', 'id_roommove')
->where('dmlog.id', $id)->get(); // <----- HERE
return response()->json($log);
You can add a line abort_if($log->isEmpty(), 404); before the return line to abort if $log is empty
findOrFail is an eloquent method, not a DB raw class method. You need to use eloquent.
You need to reference the model directly to use findOrFail
$model = App\YourModel::findOrFail($id);
https://laravel.com/docs/master/eloquent#retrieving-single-models
In laravel elequent query how to make these two where select in one.
I also feels if there too many conditions, I've too use too many where clause.
->where('trade_status', '<>', 'TRADE_FINISHED')
->where('trade_status', '<>', 'TRADE_SUCCESS')
Use whereNotIn():
->whereNotIn('trade_status', ['TRADE_FINISHED', 'TRADE_SUCCESS']);
Here in documentation you can notice
You can pass array of condition like
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
for your condition it should be like
DB::table('your_table')->where([
['trade_status', '<>', 'TRADE_FINISHED'],
['trade_status', '<>', 'TRADE_SUCCESS'],
])->get();
In my controller I have wrote this following code
$usersCount = User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->count();
$locations_array_result = explode(",",$locations_array_result);
foreach ($locations_array_result as $param)
{
$usersCount = $usersCount->whereHas('location', function($q) use($param){
$q->where('location_id', '=', $param );
});
}
This code giving following error
Call to a member function whereHas() on a non-object
Can anyone help me to find out what i have done wrong!!!
$usersCount is already a number from the 1st line of your sample.
You want instead to replace $usersCount->whereHas with User::whereHas in your foreach loop.
Taking a very wild guess here, I would think you need to get all users with these requirements
->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)
plus having a location_id value which exists on an array named $locations_array_result
If this is the case, this is all you need:
User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->whereIn('location_id', $locations_array_result)->get();
EDIT
Following your comment below, I assume user has many to many relation with locations (defined in your model), so eager loading and then using a condition with a callback should do the job:
$users = User::where('activated', '=', 1)
->where('group_id', '=', 1)
->where('availability_date', '<=', $opportunity_date)
->with(array('location' => function($query) use ($locations_array_result)
{
$query->whereIn('location_id', $locations_array_result);
}))->get();
I am new to laravel 4 and keep getting the same error trying to learn about some methods in DB class.
Call to undefined method Illuminate\Database\Query\Builder
I get same erros trying to use "->or_where", "->order_by".
another problem is parsing the dynamic methods:
->where_name("test")
turns into
`users` where `_name` = test)
but if i try to do
->wherename("test")
then everything is fine.
You're using an incorrect syntax for orWhere and orderBy.
This is the correct syntax for orWhere:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
And this for orderBy:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
Query Builder - Advanced Wheres - Laravel
All methods in Laravel 3 have changed from snake case
->where_name("test")
to camel case in Laravel 4
->whereName("test")
I have this join:
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->where('is_published', '=', 1)
But it unsurprisingly returns duplicate records, so I try to use distinct():
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->distinct()
->where('is_published', '=', 1)
but I want to use distinct() on a specific single field which I'd easily be able to do in SQL. It seems distinct() does not take parameters, i.e. I can't say distinct('volunteer.id').
Can anyone point me to how can I remove my duplicate records? I bet this is another forehead slapper for me.
In my project I tried distinct() and groupby() too and both of them worked:
//Distinct version.
Company_Customer_Product::where('Company_id', '=', $companyid)->distinct()->get(array('Customer_id'));
//Goup by version.
Company_Customer_Product::where('Company_id', '=', $companyid)->groupby('Customer_id')->get(array('Customer_id'));
According to this, distinct() should work in your case too, just use it with get():
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->distinct()
->where('is_published', '=', 1)
->get(array('volunteer.id'));
Otherwise you don't need distinct() when you use groupby() so you could just use:
Return DB::table('volunteer')
->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
->select(array('*','volunteer.id AS link_id'))
->group_by('volunteer.id')
->where('is_published', '=', 1)
->get(array('volunteer.id'));