Concatenate two field into a alias name laravel - laravel

$data = DB::table('tables')
->select('orders.order_id', 'users.*')
->leftJoin('lists', 'lists.order_id', '=', 'orders.order_id')
->leftJoin('users', 'users.user_id', '=', 'lists.user_id')
->where('users.type', '=', 'admin')
->get();
dd($data);
getting first_name and last_name how can i group those i.e alias says full_name
fullname => Max jones
Concatenate two field into a alias name laravel

used raw method laravel and mysql concat method used here
DB::raw("CONCAT(`users`.`first_name`,`users`.`last_name`) as full_name")
try
$data = DB::table('tables')
->select('orders.order_id', DB::raw("CONCAT(`users`.`first_name`,`users`.`last_name`) as full_name"))
->leftJoin('lists', 'lists.order_id', '=', 'orders.order_id')
->leftJoin('users', 'users.user_id', '=', 'lists.user_id')
->where('users.type', '=', 'admin')->get();
dd($data);

DB::raw("CONCAT('name','id') AS ID"))

Related

Laravel 8 - How to write statement with paginate instead with get

I use Alias name of column because I have two columns in different tables with the same name, when retrieve them one column will override another column
DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->get(['*','trips.name As tirp_name','trip_user.id As reservation_id'])
How can I write it with paginate?
Try it this way:
$result = DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->paginate(100, ['*','trips.name As tirp_name','trip_user.id As reservation_id']);

using whereNotIn and subquery in query builder (Laravel 8)

I would like to make a controller function to search for users by name and display only those name that are not already in a project.
here is the SQL query to display users with name containing 'mmm' who are not already in project id = 2:
SELECT `firstname`
FROM `user_details`
WHERE (
`firstname` LIKE "%mmm%" AND `user_id` NOT IN (
SELECT `member_id` FROM `project_members` WHERE `project_members`.`project_id` = 2)
)
and in the query builder (which doesn't work):
$searchResults = DB::table('user_details')
->select('user_details.user_id', 'user_details.firstname', 'user_details.lastname', 'user_details.nickname', 'user_details.status')
->whereNotIn('user_details.user_id', DB::table('project_members')
->select('member_id')
->where('project_id', '=', $request->project_id)
->get()
->toarray()
)
->where('user_details.firstname', 'LIKE', '%'.$request->searchString.'%')
->orWhere('user_details.lastname', 'LIKE', '%'.$request->searchString.'%')
->get();
Do you know where I did wrong? Thank you!
Got it. I just needed ->pluck('member_id') instead of toarray()
And also, where/orWhere must be grouped.
$searchResults = DB::table('user_details')
->select('user_details.user_id', 'user_details.firstname', 'user_details.lastname', 'user_details.nickname', 'user_details.status')
->whereNotIn('user_details.user_id', DB::table('project_members')
->select('member_id')
->where('project_id', '=', $request->project_id)
->get()
->pluck('member_id')
)
->where(function ($query) use ($request) {
$query->where('user_details.firstname', 'LIKE', '%'.$request->searchString.'%')
->orWhere('user_details.lastname', 'LIKE', '%'.$request->searchString.'%')
->orWhere('user_details.nickname', 'LIKE', '%'.$request->searchString.'%');
})
->get();

How create a subquery with eloquent and laravel with softdeletes

I am using Laravel 7 and Vue.js 2.
I want to retrieve the tasks that are not assigned to a specific user.
I made the following code that works correctly.
$tasks_user = TaskUser::select('task_id')
->where('user_id', $id)
->get();
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', $tasks_user)
->distinct()
->get();
By the way to be more elegant I decided to transform the above code into a single query as follows:
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', function($q) use ($id)
{
$q->select('task_id')
->from('task_user')
->where('user_id', $id)
->get();
})
->distinct()
->get();
Unfortunately I discovered that the above query didn't work because it doesn't considers softdeletes.
For example, if the user with id 3 was related with the task 7 but now that row has been deleted with softdeletes in the table task_user, the first code returns also the task with id 7 (correctly) and the second one not (uncorrectly).
So finally, I must do a single query that works as the first code.
Can help?
You can actually combine both approaches. whereNotIn accepts also an Eloquent Query, it doesnt need to be a callback. Try this:
$userRelatedTasksQuery = TaskUser::select('task_id')
->where('user_id', $id);
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', $userRelatedTasksQuery)
->distinct()
->get();
Be sure to not use get() at the end of the $userReleatedTasksQuery, as you want the eloquent query instance, not the result.

View user name by id in laravel 5.2

I have two table that I join in my controller.
Here is my controller :
$data = DB::table('classrooms')
->select('classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
->join('users', 'classrooms.user_id', '=', 'users.id')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')->get();
return View::make('std_classes')->with('data',$data);
In my joinclass table I have user_id. Now I want to view user name and that has in my users table. How can I select in this controller and view my blade page. I try this many time but I can't.
Here is my view:
Teacher: {{$data->name}}
Here, I added few modifications, You should try this in your controller
$data = DB::table('classrooms')
->join('users', 'classrooms.user_id', '=', 'users.id')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')
->select('users.name as name', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
->get();
return view('std_classes',compact('data'));
And use following code on view to display records
#foreach($data as $stud)
{{$stud->name}}
#endforeach
You have to select it first then you could use it as $data->name, so try to add users.name to select :
$data = DB::table('classrooms')
->select('users.name as name', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
->join('users', 'classrooms.user_id', '=', 'users.id')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')
->get();
return View::make('std_classes')->with('data',$data);
You could get the image also using :
->select('users.name as name','users.image as image', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
Or get all users attributes in same time using users.* :
->select('users.*', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
Hope this helps.

laravel join query is not working

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();

Resources