How can i save Image size in database? - laravel

I need to save size by kb in database to view it in frontend, this is the controller I am using. I am using Laravel 5.8
So my question is does Laravel provide any Facade to deal with such situations? Or any other framework have more suitable capabilities for problem is?
public function addCourse(Request $request) {
if ($request -> isMethod('post')) {
$data = $request -> all();
$course = new Courses;
$course -> course_name = $data['course_name'];
$course -> category_id = $data['course_sector'];
$course -> course_code = $data['course_code'];
if (!empty($data['course_description'])) {
$course -> description = $data['course_description'];
} else {
$course -> description = "";
}
$course -> start_date = $data['start_date'];
$course -> end_date = $data['end_date'];
$course -> location = $data['course_location'];
$course -> price = $data['course_price'];
if ($request -> hasFile('course_image')) {
$courseImage = Input::file('course_image');
if ($courseImage -> isValid()) {
$extension = $courseImage -> getClientOriginalExtension();
$filename = rand(111,99999).'.'.$extension;
$large_image_path = "assets/manage_display/images/courses/large/".$filename;
$medium_image_path = "assets/manage_display/images/courses/medium/".$filename;
$small_image_path = "assets/manage_display/images/courses/small/".$filename;
Image::make($courseImage) -> save ($large_image_path);
Image::make($courseImage) -> resize (600,null, function ($constraint) {
$constraint -> aspectRatio();
}) -> save ($medium_image_path);
Image::make($courseImage) -> resize (300,null, function ($constraint) {
$constraint -> aspectRatio();
}) -> save ($small_image_path);
$course -> image = $filename;
}
}
$course -> save();
return redirect('/control/courses') -> with('flash_message_success', 'New Courses Added Successfully');
}
$coursesCategories = CoursesCategory::where(['parent_id' => 0]) -> get();
$coursesCategories_dropdown ="<option selected disabled>Select</option>";
foreach ($coursesCategories as $coursesCategory) {
$coursesCategories_dropdown .= "<option class='font-weight-bold' value='".$coursesCategory -> id."'>".$coursesCategory -> name."</option>";
$subCoursesCategories = CoursesCategory::where(['parent_id' => $coursesCategory -> id]) -> get();
foreach ($subCoursesCategories as $subCoursesCategory) {
$coursesCategories_dropdown .= "<option class='blockquote-footer' value='".$subCoursesCategory -> id."'> - ".$subCoursesCategory->name."</option>";
}
}
return view('layouts.manage_layouts.courses.add_course') -> with(compact('coursesCategories_dropdown'));
}

You can use getSize() method to get size of uploaded file and then store it in the database:
$request->file('file')->getSize();

Related

laravel caching return 0

I am trying to cache request for counting number of users in different categories
my function without caching:
public function getNumOfUsersInCat()
{
$numUsers = count($this -> users);
$otherCategories = Category::where('id', '<>', $this -> id) -> get();
foreach($otherCategories as $categ){
if($this -> isAncestorOf($categ))
$numUsers += count($categ -> users);
}
return $numUsers;
}
I was trying to cache requests like:
public function getNumOfUsersInCat()
{
$numUsers = count($this -> users);
$otherCategories = Cache::remember('cache_numuserscat)',60*60*12,function(){
return
Category::where('id', '<>', $this -> id) -> get();
foreach($otherCategories as $categ){
if($this -> isAncestorOf($categ))
$numUsers += count($categ -> users);
}
});
return $numUsers;
}
But I am getting only 0
Laravel 5.7.2
It is because you have put the calculation loop inside your cache and before reaching that part, you return with the category list. So the code you run is actually this:
public function getNumOfUsersInCat()
{
$numUsers = count($this -> users);
$otherCategories = Cache::remember('cache_numuserscat)', 60*60*12, function(){
return Category::where('id', '<>', $this -> id) -> get();
});
return $numUsers;
}
Before the correct code, there are a few things you should note:
Your cache name should only work for this model you are in, so I added $this->id to the name.
You can use withCount to get the count of all model users in a single query. Your approach have N+1 problem.
The Final code can be like this:
public function getNumOfUsersInCat()
{
$cacheName = "cache_num_users_cat_" . $this->id;
return Cache::remember($cacheName, 60 * 60 * 12, function() {
$numUsers = count($this->users);
$otherCategories = Category::query()
->where('id', '<>', $this->id)
->withCount('users')
->get();
foreach ($otherCategories as $categ) {
if ($this->isAncestorOf($categ)) {
$numUsers += $categ->users_count;
}
}
return $numUsers;
});
}

How to insert data using user role in laravel 5.2

I want to insert data by user role
Here is my controller
public function getWorks($class_id,Request $request)
{
#if(Auth::user()->role=="Teacher")
else if($validator->fails())
{
$works = new assainments();
$works -> title = $text;
//$works -> file = $fileName;
$works -> class_id = $class_id;
$works -> users_id = Auth::user()->id;
$works -> save();
}
#endif
}
I can't use here #if(Auth::user()->role=="Teacher"). How can i use this?
You are doing something wrong i think so far. Here are probability answer:
public function getWorks($class_id,Request $request)
{
if(Auth::user()->role=="Teacher")
{
// do validation
// ....
// ....
if( !$validator->fails() )
{
$works = new assainments();
$works -> title = $text;
//$works -> file = $fileName;
$works -> class_id = $class_id;
$works -> users_id = Auth::user()->id;
$works -> save();
}
}
else
{
// do return false
return redirect()->back();
}
}

View data form id column in Laravel 5.2

I have two table in database classrooms and status. classroom fields are id,subject, class_code, user_id. I make a link for every class. now i want to show data in my class view page for every class. If my class link class?class_id=47 then its must be show only class?class_id=47 data and if class?class_id=55 its must be show class?class_id=55 data but in my class?class_id=55 class its shows class?class_id=47 class data.
My controller:
public function getclass(Request $request)
{
$id = $request->get('class_id'); if(Input::has('status-text'))
{
$text=e(Input::get('status-text'));
$rules = [
'status_text'=>'required|string',
];
$validator = Validator::make($request->all(), $rules);
if(Input::hasFile('status_image_upload'))
{
$rules['status_image_upload'] = 'image';
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
$image = $request->file('status_image_upload');
$imageName = str_random(8).'_'.$image->getClientOriginalName();
$image->move('status_images', $imageName);
$userStatus = new Status();
$userStatus -> class_id = $id;
$userStatus -> status_text = $text;
$userStatus -> image_url = $imageName;
$userStatus -> type = 1;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class',['class_id'=>$id]));
}
}
else if ($validator->fails())
{
$userStatus = new Status();
$userStatus -> class_id = $id;
$userStatus -> status_text = $text;
$userStatus -> video_url = $request['video_url'];
$userStatus -> type = 2;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class',['class_id'=>$id]));
}
}return view('class',[
'posts'=>status::orderBy('id','DESC')->get()
])->with('class_id',$request->get('class_id'))
->with('classroom',classroomModel::find($id)->first());}
My view
<li>Subject-->{{$classroom->subject_name}}</li>
<li>Section-->{{$classroom->section}}</li>
What should to do for that?

Route and controller not working properly in Laravel 5.2

I am working in a classroom project in Laravel. When I insert data that time my another route not working. but when I changed route to reverse that time my another route works and first one not work.
Here is my route:
Route::get('/class',[
'uses'=> 'classroom#getclass',
'as'=>'class',]);
another one:
Route::post('/class', [
'uses' => 'classroom#showclass',
'as' => 'classdata,]);
and i also give the controller:
public function getclass(Request $request)
{
if (Input::has('post_comment'))
{
$status=Input::get('post_comment');
$commentBox=Input::get('comment_text');
$selectedStatus=Status::find($status);
$selectedStatus->comments()->create([
'comment_text'=>$commentBox,
'user_id'=>Auth::user()->id,
'status_id'=>$status
]);
Flash::message('Your comments has been posted');
return redirect(route('class'));
}
if(Input::has('status-text'))
{
$text=e(Input::get('status-text'));
$rules = [
'status_text'=>'required|string',
];
$validator = Validator::make($request->all(), $rules);
if(Input::hasFile('status_image_upload'))
{
$rules['status_image_upload'] = 'image';
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
$image = $request->file('status_image_upload');
$imageName = str_random(8).'_'.$image->getClientOriginalName();
$image->move('status_images', $imageName);
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> image_url = $imageName;
$userStatus -> type = 1;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
}
else if ($validator->fails())
{
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> video_url = $request['video_url'];
$userStatus -> type = 2;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
another one:
public function showclass(Request $request)
{
$randomnumber = rand(50001,1000000);
$classrooms = new Classrooms();
$classrooms->class_name = $request['class_name'];
$classrooms->subject_name = $request['subject_name'];
$classrooms->section = $request['section'];
$classrooms->class_code = $randomnumber;
$classrooms -> user_id = Auth::user()->id;
$classrooms -> save();
return view('class', array('class' => Auth::user()) );
}
Where is my problem? I tried it many different way. But not fixed the problem.

Why my controller not working in laravel

I am working like a social media application... but in my controller last else if not working for status upload. but file and video uload working nicely. what is the correct format for this. here is my controller
public function getclass(Request $request)
{
if (Input::has('post_comment'))
{
$status=Input::get('post_comment');
$commentBox=Input::get('comment_text');
$selectedStatus=Status::find($status);
$selectedStatus->comments()->create([
'comment_text'=>$commentBox,
'user_id'=>Auth::user()->id,
'status_id'=>$status
]);
Flash::message('Your comments has been posted');
return redirect(route('class'));
}
if(Input::has('status-text'))
{
$text=e(Input::get('status-text'));
$rules = [
'status_text'=>'required|string',
];
$validator = Validator::make($request->all(), $rules);
if(Input::hasFile('status_image_upload'))
{
$rules['status_image_upload'] = 'image';
$validator = Validator::make($request->all(), $rules);
if($validator->fails())
{
$image = $request->file('status_image_upload');
$imageName = str_random(8).'_'.$image->getClientOriginalName();
$image->move('status_images', $imageName);
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> image_url = $imageName;
$userStatus -> type = 1;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
}
else if ($validator->fails())
{
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> video_url = $request['video_url'];
$userStatus -> type = 2;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
else if($validator->fails())
{
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> type = 3;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
}
return view('class',[
'posts'=>status::orderBy('id','DESC')->get()
]);
}
In this controller this code not working
else if($validator->fails())
{
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> type = 3;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
That portion of code will never get executed. Because the else if block above your code i.e this part
else if ($validator->fails())
{
$userStatus = new Status();
$userStatus -> status_text = $text;
$userStatus -> video_url = $request['video_url'];
$userStatus -> type = 2;
$userStatus -> users_id = Auth::user()->id;
$userStatus -> save();
Flash::success('Your status has been posted');
return redirect(route('class'));
}
has kind of block it. You are using the same condition in both the else if block. This means if the above else if condition fails then the other will also fail. And the validation rules you are using in both conditions are also same.

Resources