Route and controller not working properly in Laravel 5.2 - laravel-5

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.

Related

Eloquent save method not working when the data comes from Vue frontend

I have this code in a Vue Component:
sendUserData(){
axios.post('/api/saveUser', {
data: {
id: this.id,
name: this.name,
email: this.email,
password: this.password
}
},
{
headers: {
Authorization: 'Bearer ' + localStorage.getItem('token')
}
}
).then(response => {
if(response.data == 'success'){
this.$emit('userSaveSuccess')
} else {
this.$emit('userSaveError')
}
});
}
and I have this in a Laravel Controller:
public function saveUser($name = '', $email = '', $password = ''){
$id = request('data.id', 0);
$name = request('data.name', $name);
$email = request('data.email', $email);
$password = request('data.pswrd', $password);
Log::info(request());
if($id == 0){
$saveUser = new User;
} else {
$saveUser = User::find($id);
if($name == ''){
$name = $saveUser -> name;
}
if($email == ''){
$email = $saveUser -> email;
}
}
$saveUser -> name = $name;
$saveUser -> email = $email;
if($password != ''){
$saveUser -> password = bcrypt($password);
}
if($saveUser->save()){
return 'success';
} else {
return 'error';
}
}
My problem is, that it's output is success, but in the MySQL DB nothing has changed.
In the Log's I got this:
[2019-09-08 12:32:08] local.INFO: array (
'data' =>
array (
'id' => 2,
'name' => 'AdminTest',
'email' => 'admin#test.com',
),
)
(The original name is Admin -> the Laravel function got the request)
I tested it with Postman, too, and that time the save method worked.
What's the problem?
Edit:
Images
Edit2:
console.log(response) image
Replace your saveUser() method with the following and it will work.
/**
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\JsonResponse
*/
public function saveUser(Request $request){
// validate request data
$validator = \Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email',
'pswrd' => 'required' // add your custom validation rule
]);
// throw an validation error with message
if ($validator->fails())
return response()->json(['message' => 'The given data was invalid.', 'errors' => $validator->errors()], 422);
// best way to get request input values ( laravel docs )
$id = $request->get('id') ?? 0;
$name = $request->name;
$email = $request->email;
$password = $request->psswrd;
// TODO: you can replace the logic with ( updateOrCreate($saveUser) )
if($id == 0){
$saveUser = new User;
} else {
$saveUser = User::find($id);
}
$saveUser -> name = $name;
$saveUser -> email = $email;
if($password != ''){
$saveUser -> password = bcrypt($password);
}
try{
$saveUser->save();
return response()->json(['message' => 'Success.', 'data' => $saveUser], 200);
}catch (\Exception $exception){
return response()->json(['message' => 'Error.', 'data' => $exception->getMessage()], 200);
}
}

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 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?

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