Update Logged in user data in Laravel5 - laravel

I want to update the profile of logged in users in laravel5. I have the name of the logged in user through the session but then when I want to put the new input data to the old user data, it didn't work. I think I am having a problem in getting input user data into the controller function.
Controller:
public function updateProfile(Request $req) {
$name = Session::get('admin-name');
$user = DB::table('admin')->where('name',$name)->first();
return dd($req->input('admin-name'));
if($req->input('admin-name')!= null) {
$user->name = $req->input('admin-name');
}
if($req->input('admin-email')!= null) {
$user->email = $req->input('admin-email');
}
if($req->input('admin-address')!= null) {
$user->email = $req->input('admin-address');
}
if($req->input('admin-mobile')!= null) {
$user->mobile = $req->input('admin-mobile');
}
if($req->input('admin-dob')!= null) {
$user->dob = $req->input('admin-dob');
}
$user->save();
return view('admin-profile')->with('update-response','Profile Updated successfully');}
View:
<form method="post" action="{{route('admin-edit-profile')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="admin-name">Name:</label>
<input type="text" class="form-control" name="admin-name" id="admin-name" placeholder="Enter your name"/>
</div>
<div class="form-group">
<label for="admin-email">Email:</label>
<input type="text" class="form-control" name="admin-email" id="admin-email" placeholder="Enter your email"/>
</div>
<div class="form-group">
<label for="admin-address">Address:</label>
<input type="text" class="form-control" name="admin-address" id="admin-address" placeholder="Enter your address"/>
</div>
<div class="form-group">
<label for="admin-mobile">Mobile:</label>
<input type="text" class="form-control" name="admin-mobile" id="admin-mobile" placeholder="Enter your mobile number"/>
</div>
<div class="form-group">
<label for="admin-dob">Date of Birth:</label>
<input type="date" class="form-control" name="admin-dob" id="admin-dob" placeholder="Enter your Date of Birth"/>
</div>
<div class="form-group">
<label for="admin-pic">Your Profile pic:</label>
<input type="file" class="form-control" name="admin-pic" id="admin-pic" placeholder="Your Profile pic"/>
</div>
<input type="submit" value="Save" id="submit" class="btn btn-info"/> <input type="reset" value="Reset" id="reset" class="btn btn-info"/>
</form>
Routing is working fine. But I think the problem is with the controller function.I am getting an error:
Call to undefined method stdClass::save()

Getting the error: Call to undefined method stdClass::save()
save() in Eloquent method, so use Eloquent:
$user = Admin::where('name', $name)->first();

in your case you should use eloquent by replace
$user = DB::table('admin')->where('name',$name)->first();
with
$user = Admin::where('name', $name)->first();
if(!$user){ return "can't find this admin the name is null or not saved in DB"; }
Certainly you should import your Admin model
note: be sure if admin-name is Session key

Related

Message sending through session isn't showing

In my laravel-7 application I am sending custom message for invalid login in blade through controller. but message is not showing in blade. also tail me if I am doing anything wrong.
in blade
<div class="card-body">
#if(session()->has('message'))
<div class="alert alert-{{session('type')}}">
<li>{{session('message')}}</li>
</div>
#endif
<form action="{{route('login')}}" method="post" class="form">
#csrf
<div class="form-group">
<label for="email">Email address</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Enter email" value="{{old('email')}}">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Login</button>
</div>
</form>
in auth controller
public function processlogin(Request $request)
{
$this->validate($request,[
'email'=>'required|email',
'password'=>'required|min:6',
]);
$credentials=$request->except(['_token']);
if (Auth::attempt($credentials)){
return redirect()->route('index');
}
$this->setErrorMessage('Invalid Credentials.');
return redirect()->back();
}
in controller
public function setErrorMessage($message):void
{
session()->flash($message);
session()->flash('type','danger');
}
You are not naming the flashed variable message. You are passing the value of $message as the first argument to flash which is the name of the key.
session()->flash('message', $message);

Create not saving to database laravel

For a school assignment we are to have a site that lets you Create Update Edit and Delete a player and Add a Country.
I am having trouble with my create as it is not saving to the database and returns no error. I have a feeling it is because of my foreign key and I've been looking all over stackoverflow and laravelforums as to how to do this or why it isn't saving to my database.
(As a note all my inputs are text for now until I get it working or get an error i can work with)
Player Model
protected $primaryKey = 'Id';
protected $fillable =['name','age','role','batting','bowling','image','odiRuns','countries_id'];
public function country()
{
return $this->belongsTo('App\Country','countries_id');
}
Store Fuction
public function store(Request $request)
{
//
$player = new Player;
$player->name = $request->name;
$player->age = $request->age;
$player->role = $request->role;
$player->batting = $request->batting;
$player->bowling = $request->bowling;
$player->image = $request->image;
$player->odiRuns = $request->odiRuns;
$player->countries_id = $request->countries_id;
$player->save();
return redirect('index');
}
Form
<form action="{{ route('player.store') }}" method=“post”>
#csrf
<div class="form-group">
<label for="name">Name </label>
<input type="text" class="form-control" name="name" id="name" placeholder="First and Last" >
</div>
<div class="form-group">
<label for="age">Age </label>
<input type="text" class="form-control" name="age" id="age" placeholder="Age" >
</div>
<div class="form-group">
<label for="role">Role </label>
<input type="text" class="form-control" name="role" id="role" placeholder="Role" >
</div>
<div class="form-group">
<label for="batting">Batting </label>
<input type="text" class="form-control" name="batting" id="batting" placeholder="Batting">
</div>
<div class="form-group">
<label for="Bowling">Bowling</label>
<input type="text" class="form-control" name="bowling" id="bowling" placeholder="Bowling">
</div>
<div class="form-group">
<label for="odiRuns"> OdiRuns </label>
<input type="number" class="form-control" name="odiRuns" id="odiRuns" value="odiRuns" placeholder="OdiRuns" required>
</div>
<div class="form-group">
<label for="image">Add Image</label>
<input type="file" name="image" class="form-control-file" id="InputFile" value="image">
</div>
<div class="form-group">
<label for="Country">Country</label>
<input type="text" class="form-control" name="countries_id" id="countries" placeholder="country">
</div>
<button type=“submit” class=“btn btn-primary”>Create</button>
</form>
Player Database
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age');
$table->string('role');
$table->string('batting');
$table->string('bowling');
$table->string('image');
$table->string('odiRuns');
$table->integer('countries_id')->unsigned();
$table->foreign('countries_id')->references('id')->on('countries');
$table->timestamps();
});
}
Your form is posting a GET request instead of POST request
It's a bit difficult to notice but method=“post” should be method="post"
double quotes instead of that MS word weird character
Specify that your form can post files such as images like so
<form action="{{ route('player.store') }}" method="post" enctype="multipart/form-data">
Otherwise it won't post the image and it's not nullable in your migration
change:
$player = new Player();
and why you don't use 'select' for countries_id
like :
<select name="countries_id">
<option value=""></option>
</select>
method="post"

Data Not Submitting to Database

I'm making a contact page but the form data is not saving to the database. What's the solution?
ContactController.php
public function contact()
{
if($request->isMethod('post'))
{
$data = $request->all();
}
$contact = new Contact;
$contact->name = $data['contact_name'];
$contact->email = $data['contact_email'];
$contact->subject = $data['contact_subject'];
$contact->body = $data['description'];
$category->save();
return redirect()->back()->with('flash_message_success',
'Your message has been sent successfully');
}
contact.blade.php
<form action="{{ url('/contact') }}" id="main-contact-form" class="contact-form row" name="contact-form" method="post">
{{ csrf_field() }}
<div class="form-group col-md-6">
<input type="text" name="contact_name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group col-md-6">
<input type="email" name="contact_email" class="form-control" required="required" placeholder="Email">
</div>
<div class="form-group col-md-12">
<input type="text" name="contact_subject" class="form-control" required="required" placeholder="Subject">
</div>
<div class="form-group col-md-12">
<textarea name="description" id="message" required="required" class="form-control" rows="8" placeholder="Your Message Here"></textarea>
</div>
<div class="form-group col-md-12">
<input type="submit" name="submit" class="btn btn-primary pull-right" value="Submit">
</div>
</form>
Routes:
Route::get('contact', function(){
return view('contact');
});
Route::post('contact', function(){
return view('contact');
});
Use $contact->save(); not $category->save(); and also remove the if statement (for now): if($request->isMethod('post')) {
Your route should be:
Route::post('contact', 'ContactController#contact')->name('contact');

How to autofill Edit form with old Information from two different tables?

I have this form "Edit tache". Editing one "tache" affects two tables user and technicien, when i choose to edit tache the edit form is autofilled with information from the technicien table but the fields related to the table user are not autofilled. How could i autofill the form with information from the two tables? Thank you.
controller
public function edit($id)
{
$technicien=technicien::find($id);
$user = user::orderBy('id', 'asc')->get();
return view('technicien.edit',['moyenne_avis'=>$technicien],['actif'=>$technicien],['user_id'=>$technicien])->with('users', $user );
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
return redirect('technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Tache</h1>
<form action="{{ route('technicien.update', $actif->id , $moyenne_avis->id ) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">Nom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">Prenom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-control"value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name ="actif" class="form-control"value ="{{$actif->actif}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
route
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::put('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
$user = user::find($id);
if they are retrieved using one id on the controller, else please tell the relation between them.
and in your view, i don't see a reason to check old since its not updated yet! use
{{$user->nom)}}

laravel edit and update doesn't work

I have a CRUD with simple date. But I can't get the update method to work.
This is my controller
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, Customer
$customer,$id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
and that is my view
<form method="POST" action="{{route('customer.update',$customer->id ) }}">
{{--{{dd($customer)}}--}}
{{method_field('PUT')}}
{{ csrf_field() }}
<div class="form-group">
<label for="firstName">Voornaam</label>
<input type="text" class="form-control" name="firstName" value="{{$customer->firstName}}">
</div>
<div class="form-group">
<label for="lastName">Achternaam</label>
<input type="text" class="form-control" name="lastName" value="{{$customer->lastName}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" value="{{$customer->email}}">
</div>
<div class="form-group">
<label for="phone">Telefoonnummer</label>
<input type="text" class="form-control" name="phone" value="{{$customer->phone}}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
I can go to the edit page. But, after changing the data nothing change it look like that I miss save somewhere but I don't know, now I get that that error too few arguments to function New Controller ::update () 2 passed and exactly 3 expected.
any help will be appreciated.
Change:
public function update(CustomerRequest $request, Customer
$customer,$id)
To:
public function update(CustomerRequest $request, $id)
Or:
public function update(CustomerRequest $request, Customer
$customer)
With the last one you can remove Customer::find($id) and just use $customer
EDIT
If you look at the update route: route('customer.update',$customer->id ) you see it only takes 1 argument. The controller expects 2 because one is the request and the other is the ID.

Resources