I am trying to fetch the user name instead of multi user name but unfortunately, the single user name is not showing.
Controller
public function search(Request $request)
{
$date = explode(' - ', $request->date);
$auth = Auth::user();
$hourLog = Hourlog::with('project', 'user');
if ($request->user) {
$hourLog->whereIn("user_id", $request->user)->get();
} else if ($auth->user_type == 1) {
$hourLog->where("user_id", $auth->id);
}
if ($request->project) {
$hourLog->whereIn("project_id", $request->project)->get();
}
$data = [
"hourlogs" => $hourLog->whereBetween('date', $date)
->orderBy('date', 'desc')
->orderBy('start_time', 'asc')
->get(),
"date"=> $date,
];
return view('cms.projectreport.projectreport-list', $data);
}
html view
#foreach($hourlogs as $hourlog)
<tr>
<td>{{$hourlog->user->name}}</td>
</tr>
#endforeach
Use groupBy() on your query with the user_id:
$data = [
"hourlogs" => $hourLog->whereBetween('date', $date)
->orderBy('date', 'desc')
->orderBy('start_time', 'asc')
->groupBy('user_id')
->get(),
"date"=> $date,
];
This will give you a collection with unique user, containing the related models
You can use distinct to make the query like this:
$data = [
"hourlogs" => $hourLog
->select('user_id')
->whereBetween('date', $date)
->orderBy('date', 'desc')
->orderBy('start_time', 'asc')
->distinct()
->get(),
"date"=> $date,
];
Related
Currently I'm getting the list of table list in controller. And i got it successfully..But there is a problem, i return a list and a total.But when there is value package my count() is limited by skip - take .So I have to split it into 2 queries. Is there any way to combine those 2 queries into one to compact the code. Please give me your opinion. Thanks.
public function listData(Request $request)
{
$package = $request->package;
$userId = [1,3];
$list = List::whereIn('user_id', $userId)
->when(!empty($package), function ($query) use ($package) {
$query->where('idPackage', '=' . $package);
})->orderBy('created_at', 'DESC')->skip(0)->take(20)->get()->toArray();
$count = List::whereIn('user_id', $userId)
->when(!empty($package), function ($query) use ($package) {
$query->where('idPackage', '=' . $package);
})->count();
return response()->json([
'list' => $list,
'total' => $count
], 200);
}
This is so simple.. I think you should try this:
public function listData(Request $request)
{
$package = $request->package;
$userId = [1,3];
$list = List::whereIn('user_id', $userId)
->when(!empty($package), function ($query) use ($package) {
$query->where('idPackage', '=' . $package);
});
$count = $list->count();
$listData = $list->orderBy('created_at', 'DESC')->skip(0)->take(20)->get()->toArray();
return response()->json([
'list' => $listData,
'total' => $count
], 200);
}
good luck :))
hello friends can you help me to get the contents of $filter? I want to run where which is where the target column is in the relation array
$filter = $request->get('Biogear');
$data = DetailBarang::with(['barang' => function ($q) use ($filter) {
$q->where('brand', '=', $filter);
}])->get();
return response()->json($data);
you can try this
$filter = $request->get('Biogear');
$data = DetailBarang::with('barang')->whereHas('barang',function (Illuminate\Database\Eloquent\Builder $q) use ($filter) {
$q->where('brand', '=', $filter);
})->get();
return response()->json($data);
$user = User::select('user_id', 'name', 'email', 'username', 'type')
->where('name', '=', $name)
->where('active', 1)
->first();
It will return first record. But what else if we have 4 records.
Mean there are four records in database with same name But i would like to display that whose type == 6 if type is not equal 6 then it should take first();
How this possible please guide
Just updated the Rwd code try this code
$query = User::select('user_id', 'name', 'email', 'username', 'type')
->where('name', '=', $name)
->where('active', 1);
$users = $query->where('type', 6)->get();
if (!$users->isEmpty()) {
$user = $query->first();
}
looking for some help, I'm fetching a survey and questions, from method, when select some survey by id, from a route. But when I delete a survey, and when writing in url like http://localhost/survey_details/27 ( 27 is survey Id , which not exist anymore), I'm getting not some expection or redirection or Page not found info, but getting error where undefined variable $dat. But I need some how to show 404 page not found, or just redirect to other page if id not exist. Can some one help me?
here is my route:
Route::get('survey_draft/{id}', 'SurveyController#editSurveyDraft');
Here is my controller:
public function viewSurvey($id)
{
$object = DB::table('question')
->where('survey_id', '=', $id)
->where('name', '!=', NULL)
->get();
$teamName = DB::table('survey')->where('surveyId', '=', $id)
->join('team', 'survey.teamId', '=', 'team.teamId')
->join('company', 'company.id', '=', 'team.companyID')
->get();
$company = Auth::user()->company;
$data = Survey::where('surveyId', '=', $id)->get();
$teams = Auth::user()->teams;
$checkUserTeam = DB::table('teammembersall')
->where('UserId', '=', Auth::user()->id)
->get();
$members = Survey::where('surveyId', '=', $id)
->join('team', 'team.teamId', '=', 'survey.teamId')
->join('teammembersall', 'teammembersall.TeamId', '=', 'team.TeamId')
->join('users', 'users.id', '=', 'teammembersall.UserId')
->select('users.*')
->whereNotExists(function ($query) use ($id) {
$query->select(DB::raw(1))
->from('answer')
->whereRaw('answer.answerAboutUserId = users.id')
->where('answer.surveyId', '=', $id)
->where('answer.member_id', '=', Auth::user()->id);
})
->get();
$questions = DB::table('answer')->get();
return view('survey_details', ['object' => $object, 'data' => $data, 'teams' => $teams, 'members' => $members, 'questions' => $questions, 'checkUserTeam' => $checkUserTeam, 'teamName' => $teamName, 'company' => $company]);
}
To solve your immediate issue, you could add one of these this after $object ... get() depending on your result. One of them should work.
if(empty($object)){ abort(404); }
or
if(!$object->count()){ abort(404); }
However, your code would be much simpler if you used 2 basic laravel technologies.
ORM instead of DB::...
Route model binding. (which would handle your 404 for you)
https://laravel.com/docs/5.6/eloquent
https://laravel.com/docs/5.6/routing#route-model-binding
Problem :
I want only that records where relational records [ user ] not null.
In my case I want only first record. where user is not null
Result:
User Table
id
name
email
Project Tabel
id
title
user_id [foreign key]
My code is like this
$projects = App\Project::with(['user' => function ($user) {
$user->where('name', '=', 'Ketan');
}])
->get();
foreach ($projects as $project) {
echo $project->title.' - '.$project;
}
My result is like this :
The below code is working
$projects = App\Project::whereHas('user', function ($user) {
$user->where('name', '=', 'Ketan');
}])->get();
Change 'with' to 'whereHas' and note that whereHas doesn't accept the array as the param
For reference Laracasts Link
I got solution of my question
$projects = App\Project::whereHas(['user' => function ($user) { <--Change Here
$user->where('name', '=', 'Ketan');
}])
->get();
Only Change 'with' to 'whereHas'
By this solution, I have got only those Project records in which user ( relational record ) is not null.
You can try:
$projects = App\Project::with(['user' => function ($user) {
$user->where('name', '=', 'Ketan');
}])
->get();
foreach ($projects as $project) {
if($project->user != null){
echo $project->title.' - '.$project;
}
}