I'm using Laravel 8, now im creating dashboard.blade to display the login user details. Im facing issue as that the query result array is not display in my blade page.
My controller code is,
public function student_board(){
//dashboard session...
if(session()->has('studid')){
$data = session()->get('studid');
$st_sel= DB::table('studs')
->join('users', 'studs.semail', '=', 'users.email')
->select('studs.sname','studs.semail')
->where('users.email','=',$data)
->get();
return view('account')->with('stud', $st_sel);
}
else{
return redirect('sreg')->with('fail','Login/Reg to Access..');
}
}
My blade code is,
<div class="col-sm-4 invo-col">
From
#foreach($st_sel as $data)
<address>
<strong>Name: {{$data->sname}}</strong><br>
Email: {{$data->semail}}
</address>
#endforeach
</div>
My route is,
Route::get('/student-board',[usercontrol::class,'student_board'])->name('student-board');
Im getting st_sel is undefined, but im using with() function properly i think.
dont know where i made mistake.
Related
I have a problem to use password reset view which demanding an token to create the view in laravel breeze combine with inertiajs and reactjs.
my question is :
Q1. Where is the token generated? or where do i retrieve the token?
//route
Route::get('reset-password/{token}', [NewPasswordController::class, 'create'])
->name('password.reset');
//controller
public function create(Request $request)
{
return Inertia::render('Auth/ResetPassword', [
'email' => $request->email,
'token' => $request->route('token'),
]);
}
as you can see this route demanding a token to create the view but i dont know how to generate the token. and i cant see any page access this route so there is no example about this.
Q2. How do i pass the token to controller
<Link method="get" href={route('password.reset')} data={{ token: token }} as="button">
Reset
</Link>
im planing to do like code above which i can click from user data grid based on user id so only admin can reset your password. or any better idea to implement this?.
thankyou in advance
If you installed breeze/laravel fresh installation you can simply generate the token using:
$token = $request->user()->createToken($request->token_name);
return ['token' => $token->plainTextToken];
What you need to make sure of is that you are using the trait HasApiTokens in the User Model:
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
so im currently creating my first Website with Next.js.
Im using Amplify as a host for my website and my database.
However im having trouble to display the response im getting from my database.. I managed to log the response in my console which looks like this:
[
Profile {
name: 'firstActualUser',
position: 'VIP',
createdAt: '2022-02-08T15:39:01.527Z',
updatedAt: '2022-02-08T15:39:07.527Z',
_version: 1,
_lastChangedAt: 6781253702551,
_deleted: null
}
]
My question is how do I render this response into my actual page? Currenty I log my response like this:
const externUsers = DataStore.query(Profile).then(users => console.log(users));
I thought I can simply access this response by just accessing the array like the following:
return (
<div className={styles.container}>
<h1 className={styles.title}>Home</h1>
<p>{externUsers[0].name}</p>
<p>{externUsers[0].position}</p>
</div>
)
However that doesnt work and wouldnt be a nice solution anyways, especially if my response doesnt just contain 1 Profile.
Any Help is appreciated, watched countless tutorials but I just get more and more confused..
Thanks!
I think you are close. You just have to cover the situation where externUsers has not been loaded. (Keep in mind that everything is asynchronous in react.)
Assuming you calling the DB in a useEffect hook
const [externUsers , setExternUsers]= useState();
useEffect(()=>{
DataStore.query(Profile).then(users => setExternUsers(users));
},[]);
then do some handling before returning Jsx:
if(!externUsers) return null;
Else if(externUsers.length===0) return (<>no user</>);
Else
return (
<div className={styles.container}>
<h1 className={styles.title}>Home</h1>
<p>{externUsers[0].name}</p>
<p>{externUsers[0].position}</p>
</div>
)
I am trying to work through the MVC client quickstart but running into an "object reference not set ..." error at the line - "#foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)" from inside my Index.cshtml that I modified following the quickstart doc. Below is the complete Index.cshtml. Could somebody tell me what I am doing wrong here?
#using Microsoft.AspNetCore.Authentication
<h2>Claims</h2>
<dl>
#foreach (var claim in User.Claims)
{
<dt>#claim.Type</dt>
<dd>#claim.Value</dd>
}
</dl>
<h2>Properties</h2>
<dl>
#foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
{
<dt>#prop.Key</dt>
<dd>#prop.Value</dd>
}
</dl>
You are missing .RequireAuthorization(); on this part of code:
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute()
.RequireAuthorization();
});
The RequireAuthorization method disables anonymous access for the entire application.
Here I have a working example to follow: https://github.com/nahidf/IdentityServer4-adventures/tree/master/src/MvcClient
I recently started picking up on CakePHP and now I'm trying to build a contact form for my site.
It seems to work in validating the forms, but there's something I want to do now.
I want it to show any errors that occur (like missing a required field).
However, I want it to show 2 different possible states:
- A user error (user forgot a field or the field doesn't meet the requirements), show this below the field (using Bootstrap 4)
- A server error (mail couldn't send from the server - for example, the SMTP server is down), shown using the Flash
I've been searching far and wide for this, but I couldn't get any further on this without posting a question myself.
Below is all the code that I'm using (running CakePHP 3.6)
src/Template/Pages/contact.ctp (controlled by the PagesController):
<div class="container">
<div class="row no-banner">
<div class="col-md-6">
<h4>Contact Form</h4>
<div style="padding-bottom:25px;">
Got a question? we'd love to hear it from you!<br />
Send us a message and we'll respond as soon as possible!
</div>
<?= $this->Flash->render(); ?>
<?= $this->Form->create("Contact",array("url"=>"/contact","class"=>"contact-form","id"=>"contact-form")); ?>
<?= $this->Form->control("name",array("placeholder"=>"Your Name","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->control("email",array("placeholder"=>"Your Email","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->control("subject",array("placeholder"=>"The Subject","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->textarea("message",array("placeholder"=>"Your Message","label"=>false,"class"=>"form-control")); ?>
<?= $this->Form->button('Submit',array("class"=>"btn")); ?>
<?= $this->Form->end(); ?>
</div>
<div class="col-md-6">
<h4>Social Media</h4>
<div style="padding-bottom:25px;">
We are active on a variety of of social media, feel free to like and follow us!
</div>
<i class="fab fa-facebook social-media-icon"></i>
<i class="fab fa-discord social-media-icon"></i>
<?= $this->Form->errors; ?>
</div>
</div>
</div>
src/Controller/ContactController.php:
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class ContactController extends AppController {
public function add() {
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success('We will get back to you asap!');
$this->redirect($this->referer());
} else {
$this->Flash->error('There was an issue sending your mail. Please try again later!');
$this->redirect($this->referer());
}
}
$this->set('contact', $contact);
}
}
src/Form/ContactForm.php:
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class ContactController extends AppController {
public function add() {
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success('We will get back to you asap!');
$this->redirect(array('controller' => 'Pages','action' => 'display','contact'));
} else {
$this->Flash->error('There was an issue sending your mail. Please try again later!');
$this->redirect($this->referer());
}
}
$this->set('contact', $contact);
}
}
Don't redirect, because you don't need to. Instead of using the Pages controller to display the form put the form in your controllers view: /Contacts/add.ctp. No need to call redirect() then.
The errors are already added to the form and the form helper will find them because you're passing the form object to Form->create().
If you want to make it more complicated than needed stick to your implementation and simply write the errors to the session and in the action that receives it read them from the session and set them to the form object via setErrors().
I'm new to CakePHP and OOP in general. I'm following this tutorial from the official website that teaches me how to make a blog : https://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html
Problem is, I'd like to be able to make users log in using only an ID field, instead of the classic username-password duo.
But I can't find a way to do it... From what I saw, Auth Component always requires two fields. How should I proceed ? I'm lost.
Thanks
I think its not a good idea, but way to do this is simple.
Used version: CakePHP v3.4.5
Templates\Users\login.ctp:
<form method="post" action="<?= $this->Url->build(['controller'=>'Users', 'action' => 'login']) ?>">
<input type="text" name="id" value="">
<button type="submit">login</button>
</form>
Controller\UsersController.php:
public function login()
{
if ($this->request->is('post')) {
$user = $this->Users->findById($this->request->getData()['id'])->first();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'MyController', 'action' => 'MyAction']);
}
$this->Flash->error('Id not found.');
return $this->redirect($this->referer());
}
}
Dont forget about loading AuthComponent in AppController and allow login method for non-authorized users - what was described here.