Route not working properly - laravel-5

I have two route. First one not working and 2nd working. If I put 2nd route in first then its working and another not working.
Here is my route:
Route::any('/class',[
'uses'=> 'classroom#getclass',
'as'=>'class',]);
Route::any('/class',[
'uses'=> 'classroom#showclass',
'as'=>'class',]);
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('classroom'));
}
}
return view('class',[
'posts'=>status::orderBy('id','DESC')->get()
]);}
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()) );
}
What Should to do now? i think my tow url is same that is the problem.If i am right then how can i solve this problem?

The issue is that you are using Route::any().
As the name suggests, it will accept any request (POST, GET, PUT, PATCH, DELETE) to the /class URI and process that with the given controller function.
In your case, when you have this first:
Route::any('/class',[
'uses'=> 'classroom#getclass',
'as'=>'class',]);
Any request to to /class is just being processed by getClass, including your form submission.
And it works fine when you put the other first as that is the first one being used.
Try changing Route::any() to the type of request they actually get.
For example, something like this:
Route::get('/class',[
'uses'=> 'classroom#getclass',
'as'=>'class',]);
Route::post('/class',[
'uses'=> 'classroom#showclass',
'as'=>'class',]);
** NOTE: you cannot use the same URI (e.g /class) for two of the same method types (e.g GET) as the first route would always match first and be used.

Related

How to access the auto increment value in store method of resource controller in Laravel

I am making a blog. I want to concatenate the soon to be post id to the end of the post slug to make it unique but I can't find a way to access the auto increment value which was not sent through my form in request variable.
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$cleaned = preg_replace('/[^a-zA-Z0-9\s]/', '', $request -> title);
$cleaned = strtolower($cleaned);
$pieces = explode(" ",$cleaned);
$slug = implode("_", $pieces);
$slug = $slug."_". <------; //HERE IS THE PROBLEM
$post = new Post;
$post -> title = $request -> title;
$post -> body = $request -> body;
$post -> slug = $slug;
$post -> save();
Session::flash('success','Post Successful');
return redirect()->route('posts.show', $post->id);
}
You can't reliably get the AUTO_INCREMENT value before saving the $post.
You have to add it afterwards:
$slug = $slug."_";
$post = new Post;
[...]
$post->save();
$post->slug = $slug.$post->id;
$post->save();
The whole idea of including the id in the slug is not ideal. Maybe you should consider using a different approach.

How to upload pdf file in Laravel 5.2

I want to upload with this controller:
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]));
}
}
I know this controller will work for upload pdf. But i want to store another colunm pdf_url. Whats why i can view file and pdf in my blade page.
May this help
Don't forget to add 'files'=>'true' to the header of the form
{!! Form::open(array('url'=>'', 'method'=>'post', 'files'=>'true')) !!}
and then in the controller
if ($file = $request->hasFile('image')) {
$file = $request->file('image');
$filename = time() . '.' . $file->getClientOriginalExtension();
$destinationPath = public_path() . '/images/';
$file->move($destinationPath, $filename);
$support->image = $filename;
}
and I suggest you use this package
intervention/image

How to make a validation in 5.2?

Please can any one give a idea about this type validation.
public function joinclass()
{
if($class_code = $request->get('class_code');
$classroom = classroomModel::where('class_code',$class_code)->first();
{
$class = new joinclass();
$class -> user_id = Auth::user()->id;
$class -> class_code = $request['class_code'];
$class -> save();
}else if{
Flash::message('Your code is not found in databse');
}elseif($classroom = joinclass::where('class_code',$class_code)->first();){
Flash::message('You are already in this classroom');
}
}
What is the write formet for this code and this type condition.
You can use Validator class to validate the requests.
Here's how:
public function joinclass(Request $request)
{
//Create a Validator for your request
$validator = Validator::make($request->all(), [
'class_code' => 'required',// use exists validation to validate if an entry exists in the table with value provided "exists:table,column"
//other required validations
]);
//validate the request
if ($validator->fails()) {
//if validation fails return the error
return Redirect::back()
->withErrors($validator);
}
else{
//if validation passes
$classroom = classroomModel::where('class_code',$class_code)->first();
if($classroom = joinclass::where('class_code',$class_code)->first();){
Flash::message('You are already in this classroom');
}else{
$class = new joinclass();
$class -> user_id = Auth::user()->id;
$class -> class_code = $request['class_code'];
$class -> save();
}
}
}

How to return the current inserted row data in Laravel?

I have inserted a row in db using laravel eloquent method. See the code below,
public function store() {
$language = new languages;
$language -> languages = Input::get('languages');
$language -> created_by = Auth::user()->id;
$language -> updated_by = Auth::user()->id;
if( $language -> save() ) {
$returnData = languages::where("id","=",$language -> id) -> get();
$data = array ("message" => 'Language added successfully',"data" => $returnData );
$response = Response::json($data,200);
return $response;
}
}
I want the last inserted row from the table, but my response contains empty data. Please guide the right method of getting the data?
Almost there: you would need to call first() to get just one row. But, since you're using eloquent, you can call the find() method:
public function store() {
$language = new languages;
$language->languages = Input::get('languages');
$language->created_by = Auth::user()->id;
$language->updated_by = Auth::user()->id;
if($language->save()) {
$returnData = $language->find($language->id);
$data = array ("message" => 'Language added successfully',"data" => $returnData );
$response = Response::json($data,200);
return $response;
}
}

Magento how to set custom language in email Translator

when send order or invoice email,the email content language is default localcode.
so i want to set custom language by one customer's attribute.
overwrite this model:
Mage_Core_Model_Email_Template
public function sendTransactional($templateId, $sender, $emails, $name, $vars = array(), $storeId = null) {
$this -> setSentSuccess(false);
// print_r($emails);die;
foreach ((array)$emails as $key => $email) {
if (($storeId === null) && $this -> getDesignConfig() -> getStore()) {
$storeId = $this -> getDesignConfig() -> getStore();
}
if (is_numeric($templateId)) {
$this -> load($templateId);
} else {
$localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
$webSiteId = array(Mage::app()->getStore($storeId)->getWebsiteId(),1);
$webSiteId = array_unique($webSiteId);
$Customer = Mage::getModel("customer/customer");
$Customer->setWebsiteId($webSiteId);
$Customer->loadByEmail($email);
$language = $Customer->getData('language');
if (isset($language) && !empty($language)) {
$localeCode = $this -> setLanguageByAsiointikieli($language);
} else {
$localeCode = 'en_US';
}
// Mage::getSingleton('adminhtml/session')->setLocale($localeCode);
// Mage::app()->getLocale()->setLocaleCode($localeCode);
$this -> loadDefault($templateId, $localeCode);
}
....
now , i set custom language is en_US.default language is fi_FI.
The result is:
email content title is right(en_US).but email content value is wrong(fi_FI),is not en_US.
As shown below:
click this
order email image
why?? Who can help me ??

Resources