Why my controller not working in laravel - laravel-5

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.

Related

Laravel: Divide into monthly installments

On a controller store function, I have this code:
public function store(RegisterAccountsRequest $request){
if($request->installment > 1){
foreach(range(1, $request->installment) as $installment) {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name . " ( {$installment} / {$request->installment} )";
$data->value = ( $request->value / $request->installment );
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = $installment;
$data->due_date = $request->due_date;
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
} else {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name;
$data->value = $request->value;
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = 1;
$data->due_date = $request->due_date;
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
return redirect()->route('admin.financials.index')
->with('success', "Conta cadastrada com sucesso!", $data);
}
When $request->installment is bigger than 1, then runs the first part of the code e it should do some calculations which I'm not figuring out how to store each installment for each month, like:
due_date
________
30/10/2020
30/11/2020
30/12/2020
etc.
I hope I made myself clear enough.
Help, please!
I think you need something like this:
public function store(RegisterAccountsRequest $request){
if($request->installment > 1){
$dueDate = Carbon::createFromFormat('d/m/Y', $request->due_date);
foreach(range(1, $request->installment) as $installment) {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name . " ( {$installment} / {$request->installment} )";
$data->value = ( $request->value / $request->installment );
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = $installment;
$data->due_date = $dueDate->format('d/m/Y');
$dueDate->addMonth();
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
} else {
$data = new Financial;
$data->type = $request->type[0];
$data->payment = $request->payment[0];
$data->description = $request->description;
$data->status = $request->status;
$data->name = $request->name;
$data->value = $request->value;
if ($request->amount_paid == '0.00') {
$data->effective = 0;
} else {
$data->effective = 1;
}
$data->installment = 1;
$data->due_date = $request->due_date;
$data->save();
$cf = new ClientFinancial;
$cf->client_id = Auth::user()->id;
$cf->financial_id = Financial::orderBy('id', 'desc')->first()->id;
$cf->save();
}
return redirect()->route('admin.financials.index')
->with('success', "Conta cadastrada com sucesso!", $data);
}
In this case I make a Carbon(carbon docs) instance with date due_date.
After using the value like so $data->due_date = $dueDate->format('d/m/Y'); and then adding one month like so $dueDate->addMonth(); I have a new month available each iteration.

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?

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.

Resources