Understanding method in laravel/php - php

I am working on laravel/php in which I am checking how the method that is being used in the controller to return the view.
public function client($uuid) {
$client = clients::with([
'client_details' => function($q){
$q->with([
'client_ratings' => function($q){
$q->with(['rating_review',
'rating_user' => function($q){
$q->select('user_id','first_name','last_name');
},
'rating_user_media' => function($q) {
$q->select('client_id','owner_id','url')->where('media_type','image')->where('context','user');
}]);
$q->orderBy('created_at', 'desc');
$q->take(2);
}]);
},'owner_details','media','cancellation_policy'])->where('uuid',$uuid)->first();
$groups = groups::getgroups(NULL);
$data = [
'groups' => $groups,
'client' => $client
];
//echo '<pre>';var_dump(json_decode(json_encode($data)));exit;
return View::make('client',['data'=>$data]);
}
Problem Statement:
The above php code is creating the JSON but I am not sure how is this JSON getting created.

Related

hide records with controller

How can I hide records from my controller? i need to hide the records if the column 'retiro' is not null.
Controller:
public function retiro (Request $request){
$data = [
'category_name' => 'datatable',
'page_name' => 'miscellaneous',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$records = Registro::where('retiro', '<>', 'on')->get();
return view('retiro',compact('records'))->with($data);
}
if 'retiro' == 'on' should hide the record
the problem is that when printing the records it does not show anything.
help pls
You can pass all the variables as the second parameter on the view() using compact instead of doing it separately using the function with.
public function retiro (Request $request){
$data = [
'category_name' => 'datatable',
'page_name' => 'miscellaneous',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
];
$records = Registro::whereNotNull('retiro')
->where('retiro','=','on')
->get();
return view('retiro',compact('records','data'));
}

How to select field name using with relation in laravel

I am trying to get field name using with relation:
My Code:
public function getAllUsers()
{
$users = User::with('userBasicInfo','roles:name')->get();
return response([
'status' => true,
'message' => "All Users",
'total' => count($users),
'data' => $users
], 200);
}
when i simply use $users = User::with('userBasicInfo','roles)->get();
it returns my record accurately and when I used $users = User::with('userBasicInfo','roles:name')->get();
It returns my null array of roles I also want to display name using roles: name but it returns nothing.
You should try this:
public function getAllUsers()
{
$users = DB::table('users')
->select('users.name','roles.name')
->join('userBasicInfo', 'users.userbasicinfo_id', '=', 'userBasicInfo.id')
->join('roles', 'users.roles_id', '=', 'roles.id')
->get();
OR
$users = User::with(['userBasicInfo','roles:name'])->get();
return response([
'status' => true,
'message' => "All Users",
'total' => count($users),
'data' => $users
], 200);
}

can't access variable in view from controller laravel

I can't access $prospectus in the function show() but works well in the function store() in laravel version 5.6.27
public function store(Request $request) {
$course = Course::create([
'name' => $request['name'],
'title' => $request['title'],
'division_id' => $request['division_id'],
]);
$prospectus = Prospectus::create([
'years' => $request['years'],
'name' => $course->name,
'user_id' => null,
'course_id' => $course->id,
]);
return view('courses.show', compact('course', 'prospectus'));
}
public function show(Course $course) {
$prospectus = Prospectus::where('course_id', $course->id)->get();
//return $prospectus;
return view('courses.show', compact('course', 'prospectus'));
}
the data is passed when i use return $prospectus; but not in return view('courses.show', compact('course', 'prospectus'));
here are my routes
Route::resource('courses', 'CourseController');
Route::post('courses', 'CourseController#store')->name('courses.store');
Route::get('courses/{course}', 'CourseController#show')->name('courses.show');
I supose you want a single Prospectus object, get() will give you a collection of objects.
Use the first() function to get only the first match from the database as a single object.
$prospectus = Prospectus::where('course_id', $course->id)->first();
Confirm that $prospectus query don't return NULL
Try this:
$prospectus = Prospectus::where('course_id', $course->id)->first();

why did't work one controller function route in same controller functions in laravel

I have three TaskController function methods like this in My laravel application,
public function show($project_id,$task_id)
{
$project = Project::find($project_id);
$task = Task::find($task_id);
view('tasks.show')->withProject($project)->withFiles($files)->withTask($task);
return view('tasks.show', ['task' => $task, 'project' => $project]);
}
public function show1($project_id,$task_id)
{
$project = Project::find($project_id);
$task = Task::find($task_id);
view('tasks.show')->withProject($project)->withFiles($files)->withTask($task);
return view('tasks.show1', ['task' => $task, 'project' => $project]);
}
public function show2($project_id,$task_id)
{
$project = Project::find($project_id);
$task = Task::find($task_id);
view('tasks.show')->withProject($project)->withFiles($files)->withTask($task);
return view('tasks.show2', ['task' => $task, 'project' => $project]);
}
and my routes for this is like this,
Route::get('projects/{projects}/tasks/{tasks}',[
'uses' => '\App\Http\Controllers\TasksController#show',
]);
Route::get('collaborators/projects/{projects}/tasks/{tasks}', [
'uses' => '\App\Http\Controllers\TasksController#show1',
]);
Route::get('collaborators/projects/{projects}/tasks/{tasks}', [
'uses' => '\App\Http\Controllers\TasksController#show2',
]);
but among above three routes show1 route is not working. but if I delete show2 route then show1 is working. how can I fix this problem?

Can't transform results of Eloquent query

I'm building an API in Laravel to learn how to do such a thing. I'm following a Laracasts course to do this, but I'm having some troubles with the parts I want to do for myself.
Currently, I have this function in my controller. It fetches data from two tables and then returns it.
public function lesson($userid)
{
$lessons = DB::table('lessons')
->join('userlessons', 'lessons.id', '=', 'userlessons.lessonsid')
->select('lessons.name', 'lessons.seen')
->where('userlessons.userid','=', $userid)
->get();
return $this->respondWithPagination($lessons, [
'data' => $this->LessonTransformer->transformCollection($lessons)
]);
}
And LessonTransformer is this:
class LessonTransformer extends Transformer
{
public function transformCollection($items)
{
return array_map([$this, 'transform'], $items);
}
public function transform($item)
{
return [
'name' => $item['name'],
'seen' => (bool) $item['seen']
];
}
}
I tried a lot of solutions, some smart, some stupid. But I keep getting this error: Cannot use object of type stdClass as array
If you get such error, probably you need to change:
return [
'name' => $item['name'],
'seen' => (bool) $item['seen']
];
into:
return [
'name' => $item->name,
'seen' => (bool) $item->seen
];
but you haven't showed in which line error appear so it's only a guess

Categories