Facing Error in updating value in two table in laravel - laravel

public function update(Request $request, $user)
{
$user= User::find($user);
$user->name = $request->input('name');
$mobile= Mobile::find($user);
$mobile->id = $request->input('id');
$mobile->country_code = $request->input('country_code');
$mobile->phone_number = $request->input('phone_number');
$user->save();
return redirect('/dashboard')->with('alert-success', 'updated');
}
this code is not working for me ... i want to update the value in two table one table is (user) and second table is (mobile)..

You are not committing the changes to the Mobile model to your database.
Calling $model->save() commits any changes to the that instance of that model object, to the database.
However, it looks like you're using the wrong value to 'find' the mobile model. If you're using Laravel relationships correctly, my edit to that part should help.
I tend to handle changes to my models individually in my code, so it is more readable. Here's how I would do it:
$user= User::find($user);
$user->name = $request->input('name');
$user->save();
$mobile = $user->mobile //this part relies on you having set up the relationship correctly.
$mobile->id = $request->input('id');
$mobile->country_code = $request->input('country_code');
$mobile->phone_number = $request->input('phone_number');
$mobile->save();

Edit A Form
<form class="" action="{{ route('user.update',$user->id) }}" method="POST" name="">
<input type="hidden" name="_method" value="PATCH">
<input type="text" name="id" value="{{$user->id}}">
{{ csrf_field() }}
<Input type="text" name="name" value="{{$user->name}}" >
<Input type="text" name="email" value="{{$user->email}}" >
#if($user->mobile)
<Input type="text" name="country_code" value="{{$user->mobile->country_code}}">
<Input type="text" name="phone_number" value=" {{$user->mobile->phone_number}}">
#else
<Input type="text" name="country_code" value="{{$user->mobile->country_code}}">
<Input type="text" name="phone_number" value=" {{$user->mobile->phone_number}}">
#endif
<button type="submit" class="btn btn-danger btn-sm" value="" >update</span></button>
<span class="pull-right">
</div>
its my edit.blade.php
controller for edit
$editinfo = Mobile::find($user);
return view('edit',compact('user'));

You are not performsave() on $mobile instance
are you forgot add $mobile->save()

you could use tap()function
$user= User::findOrFail($id);
$user_update = tap($user)->update([
'country_code' => $request->input('country_code'),
'phone_number' => $request->input('phone_number'),
]);
further read: https://laravel.com/docs/5.6/helpers#method-tap

Related

data isn't saving into db using eloquent relation

hi m trying to save data into db using Eloquent ORM one-to-one but it is not saving and not showing any error to fix it (m unable to find any solution about it because it is not showing any error),
public function store(Request $request)
{
$request->validate([
'owner_id' => 'required',
'phone_name' => 'required|unique:phones'
]);
$phone = new Phone;
$phone->owner_id = $request->owner_id;
$phone->phone_name = $request->phone_name;
$phone->save();
return redirect()->route('phone.index')->with('flash_messages_success', 'Phone has been added successfully');
}
protected $fillable = [
'pphone_name', 'owner_id'
];
public function owner()
{
return $this->belongsTo('App\Owner');
}
<form method="POST" action="{{ route('phone.store') }}">
#csrf
<div class="form-group">
<label for="">Phone Title</label>
<input type="text" name="phone_name" class="form-control" id="" placeholder="Phone Name">
</div>
<div class="form-group">
<label>Select Owner</label>
<select class="form-control" name="category_id">
<option selected="">Under Owner</option>
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->owner_name }}</option>
#endforeach
</select>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</form>
The issue in select name you named category_id so the view should be:
<select class="form-control" name="owner_id">

How to edit image and update in laravel

I have a table called Services, now this table has the following -
-id
-title
-body
-image
-slug
-timestamps
And in administrator, services page I created for Add, Edit, Delete it.
My problem is edit and update a image. A strange problem is that, when I change the of image to aaaaa field in the service table (database), Nothing happens. should happen. because I changed rename of image to aaaaa.
web.php
Route::resource('services', 'ServiceController');
ServiceController.php
public function edit(Service $service)
{
return view('Admin.services.edit', compact('service'));
}
public function update(Request $request, Service $service)
{
$service->title = $request->title;
$service->body = $request->body;
if($request->has('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/services'), $filename);
$service->image = $request->file('image')->getClientOriginalName();
}
$service->update();
return redirect()->route('services.index');
}
edit.blade.php
<form class="form-horizontal" action="{{ route('services.update', $service->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('PATCH') }}
#include('Admin.layouts.errors')
<div class="form-group">
<label for="title">عنوان</label>
<input type="text" class="form-control" id="title" name="title" placeholder="عنوان" value="{{ $service->title ? : old('title') }}">
</div>
<div class="form-group">
<label for="body">متن</label>
<textarea class="form-control" rows="10" id="body" name="body" placeholder="متن">{{ $service->body ? : old('body') }}</textarea>
</div>
<div class="form-group">
<label for="images">تصویر</label>
<div class="custom-file">
<input type="file" class="custom-file-input" id="images" name="images">
<label class="custom-file-label" for="images">تصویر محصول</label>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">ذخیره</button>
</div>
</form>
Service.php
protected $fillable = [
'title',
'body',
'image',
'slug',
];
I even changed the controller in the update method as follows, nothing happened.
$service->save();
I think one of efficient way to edit image in laravel.
add this at top of the controller
use File;
First of get your bannerId in a variable
$bannerId = $request->banner_id;
get object of that id by find ().
$bannerData = FrontEndBanner::find($bannerId);
check if file exist and check if file already exist then delete that file else same name will inserted in imageName variable
if ($request->hasFile('banner_image')){
$image_path = public_path("/uploads/resource/".$bannerData->banner_name);
if (File::exists($image_path)) {
File::delete($image_path);
}
$bannerImage = $request->file('banner_image');
$imgName = $bannerImage->getClientOriginalName();
$destinationPath = public_path('/uploads/resource/');
$bannerImage->move($destinationPath, $imgName);
} else {
$imgName = $bannerData->banner_name;
}
$bannerData->title = $request->banner_title;
$bannerData->banner_name = $imgName;
$bannerData->save();
If anyone have issue then comment
In the html you have:
<input type="file" class="custom-file-input" id="images" name="images">
and in Controller: $request->file('image').
Replace
<input type="file" class="custom-file-input" id="images" name="images">
with
<input type="file" class="custom-file-input" id="images" name="image">

Laravel 5.7 using same url action for insert and update with #yield in blade template

I am using laravel 5.7 and I am using one form for inserting and updating. In form action I want to use #yield() for laravel route to get the id for updation. Every thing is fine but I can't use #yield() method. Here is my code, problem is only in action of the form.
<form class="form-horizontal" action="{{ url('/todo/#yield('editId')') }}" method="post">
{{csrf_field()}}
#section('editMethod')
#show
<fieldset>
<div class="form-group">
<div class="col-lg-10">
<input type="text" name="title" placeholder="Title" value="#yield('editTitle')" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-10">
<textarea class="form-control" placeholder="Body" name="body" rows="5" id="textarea">#yield('editBody')</textarea>
<br>
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</fieldset>
</form>
I have also checked with single and double quotes.
action="/todo/#yield('editid')"
When I use simple this method then after submitting it redirects me to localhost and with an error page not found. In laravel 5.4 it works. but not in laravel 5.7. Any help would be appreciated Thanks
Here is my edit.blade.php from where I am using the #section and #yield
#extends('Todo.create')
#section('editId',$item->id)
#section('editTitle',$item->title)
#section('editBody',$item->body)
#section('editMethod')
{{ method_field("PUT") }}
#endsection
Controller store edit and update methods are
public function store(Request $request)
{
$todo = new todo;
$this->validate($request,[
'body'=>'required',
'title'=>'required|unique:todos',
]);
$todo->body = $request->body;
$todo->title = $request->title;
$todo->save();
return redirect("todo");
}
public function edit($id)
{
$item = todo::find($id);
return view("Todo.edit",compact('item'));
}
public function update(Request $request, $id)
{
$todo = todo::find($id);
$this->validate($request,[
'body'=>'required',
'title'=>'required',
]);
$todo->body = $request->body;
$todo->title = $request->title;
$todo->save();
return redirect("/todo");
}
To answer the OP actual question you would need to do
#section('editId', "/$item->id") or #section('editId', '/'.$item->id')
{{ url('/todo') }}#yeild('editId')
But much better to do
{{ url('/todo/'.(isset($item) ? $item->id : '')) }}
Or for PHP >= 7
{{ url('/todo/'.($item->id ?? '')) }}
As apokryfos already mentioned - #yield is thought to make reusing your templates easier.
If you simply want to determine (for example) which action should be called afterwards you should better do something like that:
#extends('Todo.create')
<form class="form-horizontal" action="/todo/{{ isset($item) ? $item->id : '' }}" method="post">
#if( ! isset($item))
{{ method_field("PUT") }}
#else
{{ method_field("PATCH") }}
{{csrf_field()}}
<fieldset>
<div class="form-group">
<div class="col-lg-10">
<input type="text" name="title" placeholder="Title" value="{{ isset($item) ? $item->title : '' }}" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-lg-10">
<textarea class="form-control" placeholder="Body" name="body" rows="5" id="textarea">{{ isset($item) ? $item->body : '' }}</textarea>
<br>
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</fieldset>
</form>
Also as I remember the method field should always come first to make sure it's recognized correctly. Additionally you shouldn't need url() to generate the url I think.
No need for a second blade. Simply inject the variables directly into the template and make sure they are set before you access them. I didn't try it but I'd think that it should work.

Update Logged in user data in Laravel5

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

Storing registration data in database using laravel 5.4

Have been trying to create a user registration/login page following laracast video.
But when I try to register a user, it return to the same page without given any error as to why. I also noticed that the supplied info is not stored in the database.
Am so confused I don't know what am doing wrong you help will be really appreciated
Here is my view:
<div class="col-md-offset-1 col-md-8">
<h1 class="text-center ">Register</h1>
<form method="POST" action="register" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" email="email" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<div class="form-group">
<label for="password_confirmation">Password Confirmation</label>
<input type="password" class="form-control" name="password_confirmation" required>
</div>
<button type="submit" class="btn btn-primary"><i class="fa fa-reply"></i>Register</button>
</form>
</div>
This is my controller
public function create()
{
return view('registration.create');
}
public function store(Request $request)
{
//validate form
$this->validate(request(),[
'name' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed',
]);
//create and save user
$user = new User; //create new Post model object
$user->name = $request->name; //adding thing to the the Post object
$user->email = $request->email;
$user->password = $request->password;
//save user
$user->save(); //to save the new item into the DB
//$user = User::create(request(['name', 'email', 'password']));
//sign them in
auth()->login($user);
//redirect to the admin
return redirect('/admin');
}
While this is my route file:
Route::get('register', 'RegistrationController#create');
Route::post('register', 'RegistrationController#store');
Thanks a lot in advance
Check your form, there's an error in the email field. It says:
and should be:
<input type="email" class="form-control" name="email" required>
So, replace email="email" with name="email"
first you add this error block. so, you can find error easily.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Then, your mistake is in form action. please replace it to below code.
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">

Resources