View data form id column in Laravel 5.2 - laravel-5

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?

Related

How can i save Image size in database?

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

how to stop giving reward point after 1 commenting

5
i want to if user comment 1st time grant reward +5 after another comment should not get reward
here is my controller logic
public function comment($automobile, Request $request){
$automobile = Automobile::where('slug', $automobile)->firstorfail();
$comment = new Automobilecomment;
$comment->user_id = Auth::user()->id;
$comment->automobile_id = $automobile->id;
$comment->comment = $request->comment;
$comment->save();
$user = User::find(Auth::user()->id);
$reward = 5;
$currentreward = $user->reward;
$user->reward = $currentreward + $reward;
$user->save();
return Redirect::back()->with('status', 'Comment Success');
}
if u need more information let me know because i never did this before like this
Try this:
`
public function comment($automobile, Request $request){
$check = Automobile::where('user_id', Auth::user()->id)->where('automobile_id', $automobile->id)->get()->first();
$automobile = Automobile::where('slug', $automobile)->firstorfail();
$comment = new Automobilecomment;
$comment->user_id = Auth::user()->id;
$comment->automobile_id = $automobile->id;
$comment->comment = $request->comment;
$comment->save();
if(!$check)
{
$user = User::find(Auth::user()->id);
$reward = 5;
$currentreward = $user->reward;
$user->reward = $currentreward + $reward;
$user->save();
}
return Redirect::back()->with('status', 'Comment Success');
}`

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

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