laravel join query is not working - laravel

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!

Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.

$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

Related

how laravel join three tables with advance join clause using variable

Larvel have a question about inner joins with multiple tables and on values. I did build my code like this in laravel.
$clients = clients::leftjoin('clients_payment_type','clients_topup', function($join) {
$join->on('clients_payment_type.user_id', '=', 'clients.id') AND
$join->on('clients_topup.user_id', '=', 'clients.id');
})->where('clients.reference_id','=',$reference_id)->get();
Try this :
$clients = clients::leftjoin('clients_payment_type','clients_topup')
->join('clients','clients_payment_type.user_id', '=', 'clients.id')
->join('client_toup','clients.id', '=', 'clients_topup.user_id')
->where('clients.reference_id','=',$reference_id)
->get();
Regards
Working Answer
$clients1 = DB::table('clients')
->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
->where('clients.reference_id','=',$reference_id)
->get();

Laravel 5.2 : How to write query builder with 2 ON in this right join

I have this sql syntax
SELECT vehicles.license_plate, vehicles.year, vehicles.status, vehicles.zone_id, vehicles.car_class_id, harga_sewa.value, harga_sewa.description
FROM `vehicles`
RIGHT JOIN `harga_sewa` ON vehicles.zone_id = harga_sewa.zone_id
AND vehicles.car_class_id = harga_sewa.car_class_id
How I can write something like this?
DB::table('vehicles')
->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id', AND vehicles.car_class_id = harga_sewa.car_class_id)
->select(*)
Try below code
DB::table('vehicles')
->rightJoin('harga_sewa', function ($join) {
$join->on('vehicles.zone_id', '=', 'harga_sewa.zone_id');
$join->on('vehicles.car_class_id', '=', 'harga_sewa.car_class_id');
})
->get();
On mobile, so can't test, but from memory this should work:
DB::table('vehicles')
->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id')
->rightJoin('harga_sewa', 'vehicles.car_class_id', '=', 'harga_sewa.car_class_id')
->select('*')
->get();

Laravel 5 - Check if date is today

I'm creating a timeline and I'm nearly finished. I want that the color of the date for each timeline event is the same beside if the date is "today".
So I need something like:
#if($event[$i]->created_at->format('d.m.Y') == *code or variable that says its today*)
....
#endif
But I couldn't figure out what I can do to save the todays date in a variable.. Does anybody knows a solution for this?
thanks!
You can use isToday() method to check if date is today:
if ($event[$i]->created_at->isToday())
you can use Carbon
#if($event[$i]->created_at->format('d.m.Y') == \Carbon::today() )
....
#endif
You can use
whereDate, whereMonth, whereDay, whereYear, whereTime
whereDate()
$users = DB::table('users')
->whereDate('created_at', '2019-11-31')
->get();
whereMonth()
$users = DB::table('users')
->whereMonth('created_at', '10')
->get();
whereDay()
$users = DB::table('users')
->whereDay('created_at', '20')
->get();
whereYear()
$users = DB::table('users')
->whereYear('created_at', '2019')
->get();
whereTime()
$users = DB::table('users')
->whereTime('created_at', '=', '11:20:45')
->get();

Is there anyway to use startWith in LeftJoin Laravel?

I have the following code:
$get_order = DB::table('orders')
->leftJoin('postcodes', 'orders.postcode', '=', 'postcodes.postcode')
->where('order_ref', '=', $order_ref)
->first();
What I am trying to achieve, is do a left join if postcodes.postcode starts with orders.postcode . Is there any way to make this?
Thanks in advance.
$get_order = DB::table('orders')
->leftJoin('postcodes', 'postcodes.postcode','LIKE', DB::raw( "CONCAT(orders.postcode, '%')" ))
->where('order_ref', '=', $order_ref)
->first();
Try This
->leftJoin('postcodes', function($join) {
$join->on('postcodes.postcode', '=', 'orders.postcode')
->on('postcodes.postcode', 'LIKE', "startWith%"));
})

Why does this return only one result?

I have the following query. The query should be returning all five rows that I have where user_id = 1, but it's only returning the first result. Why?
$servers = DB::table('posts as p')
->select('p.id', 'p.content', DB::raw('COUNT(i.id) as num_results'))
->leftJoin('images as i', 'i.post_id', '=', 'p.id')
->where('p.user_id', Auth::id())
->get();
Thank you.
public function index(User $user)
{
$users = $user
->where('rol','2')//for normal user
->leftjoin('cityes', 'cityes.id', '=', 'users.cityes_id')
->select('users.id', 'users.name','cityes.name as city')
->get();
}
this code is work for me try this way

Resources