How to get last_inserted_id with laravel Request - php

I used the function below to insert my users data to database from a form.
public function store(UsersRequest $request, Users $user)
{
Users::create($request->all());
echo $user->id;
}
I tried to get the last inserted id but nothing is showing. What am I getting wrong?

try
public function store(UsersRequest $request, Users $user)
{
$user = Users::create($request->all());
echo $user->id;
}
Or
public function store(UsersRequest $request)
{
$user = new Users($request->all());
$user->save($user);
echo $user->id;
}

Related

Laravel: How can I edit user information and upload profile image at the same time?

I've been trying to edit user information and upload profile image from the same form.
I was able to edit user information using the code below,
ProfileController.php
public function index()
{
$user = Auth::user();
return view('profile.profile', ['user' => $user]);
}
public function edit()
{
$user = Auth::user();
return view('profile.editprofile', ['user' => $user]);
}
public function update(Request $request)
{
$user_form = $request->all();
$user = Auth::user();
unset($user_form['_token']);
$user->fill($user_form)->save();
return redirect('/profile');
}
however, when I tried to upload profile image too(I added the profile_image column to the DB) and changed the code like this
ProfileController.php
public function index()
{
$user = Auth::user();
return view('profile.profile', ['user' => $user]);
}
public function edit()
{
$user = Auth::user();
return view('profile.editprofile', ['user' => $user]);
}
public function update(Request $request, User $user)
{
$user->id = $request->user()->id;
$user->name = $request->name;
$user->email = $request->email;
if ($request->hasFile('image')) {
if (is_file($user->image)) {
Storage::delete('public/image/' . $user->image);
}
$path = $request->file('image')->store('public/image');
$user->image = basename($path);
}
$user = Auth::user();
$user->update();
return redirect('/tests');
}
Nothing gets edited or uploaded. I will just be redirected and no errors are shown.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
you get redirected because of this lines:
$user = Auth::user();
$user->update();
return redirect('/tests');
first line is setting a new value into $user variable and next line is updating that with no new value! and then redirect to previous url
check if your image moved to 'public/image' or not
$user->name = $request->name;
if (is_file($user->image)) {
Storage::delete('public/image/' . $user->image);
}
This code was unnecessary. Thank you!

Laravel 5 modal form data not updating

My route:
Route::post('/edit/{id}',['as'=>'edit','uses'=>'datacontroller#update']);
My controller:
public function update(Request $request, $id)
{
$task = veriler::findOrFail($id);
$input = $request->all();
$task->fill($input)->save();
return redirect()->back();
}
My modal form page:
Only the last data updating. I could not solve the problem.
public function update(Request $request, $id)
{
$task = veriler::findOrFail($id);
$input = $request->all();
$task->fill($input);
$task->save();
return redirect()->back();
}
Fill function takes the input, and save function saves to the databaes.

Arguement 1 must be instance of Illuminate\Http\Request integer given

When I am trying to send mail, everytime a new member is added to the user table, so that they can get a setup password link. I have been trying to get this to work but seem not to be.
public function store(AddUser $request)
{
$user = $request->all();
$user['activate'] = $this->active();
$user['guid'] = $this->guid();
$user['accountno'] = $this->generateAndValidateAccountno();
$check = User::find($user['phone']);
if(!$check) {
$id = User::create($user);
$this->sendEmail($user['accountno']);
}
return redirect('employee');
}
public function sendEmail(Request $request, $id)
{
$user = User::find($id);
Beautymail::send('emails.welcome', [], function($message)
{
$message
->to('$id->email', '$id->fname')
->subject('Welcome!');
});
}
}
Not sure what am doing wrong
Just use the same request class in the controller and the model. In your user model, add use Illuminate\Http\Request at the top of the class to tell it which Request class to use.
Just change:
public function sendEmail(Request $request, $id){...}
to
public function sendEmail($id){...}

ModelNotFoundException - No query results for model [App\User]

From the form in my edit view, I'm calling the update function:
public function update(Request $request)
{
$id = Auth::user();
$user = User::findOrFail($id);
$user->update($request->all());
return redirect('user');
}
but I'm getting the following error:
ModelNotFoundException in Builder.php line 125: No query results for model [App\User].
I'm not passing the $id because i'm updating the Auth Id.
Not sure what's wrong and how to debug
Following is just enough in case you want to update logged in User.
public function update(Request $request)
{
Auth::user()->update($request->all());
return redirect('user');
}
#pinkalvansia has a great answer and solution for you.
However, I also wanted to note that I believe you would need to use:
Auth::user()->id to get the currently logged in user's ID. Your code would like this:
public function update(Request $request)
{
$user_id = Auth::user()->id;
$user = User::findOrFail($user_id);
$user->update($request->all());
return redirect('user');
}
Hope this is helpful!

action.class for current user

i learn Symfony 1.4 with Jobeet. I made Jobeet and system login for user. Now i would like add possibility edit own affiliate.
<?php
class jobActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
$this->jobeet_job_list = Doctrine::getTable('JobeetJob')
->createQuery('a')
->execute();
}
public function executeShow(sfWebRequest $request)
{
$this->jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id'));
$this->forward404Unless($this->jobeet_job);
}
public function executeNew(sfWebRequest $request)
{
$this->form = new JobeetJobForm();
}
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod('post'));
$this->form = new JobeetJobForm();
$this->processForm($request, $this->form);
$this->setTemplate('new');
}
public function executeEdit(sfWebRequest $request)
{
$this->forward404Unless($jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id')), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id')));
$this->form = new JobeetJobForm($jobeet_job);
}
public function executeUpdate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
$this->forward404Unless($jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id')), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id')));
$this->form = new JobeetJobForm($jobeet_job);
$this->processForm($request, $this->form);
$this->setTemplate('edit');
}
public function executeDelete(sfWebRequest $request)
{
$request->checkCSRFProtection();
$this->forward404Unless($jobeet_job = Doctrine::getTable('JobeetJob')->find($request->getParameter('id')), sprintf('Object jobeet_job does not exist (%s).', $request->getParameter('id')));
$jobeet_job->delete();
$this->redirect('job/index');
}
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()));
if ($form->isValid())
{
$jobeet_job = $form->save();
$this->redirect('job/edit?id='.$jobeet_job['id']);
}
}
}
In actions.class executeIndex i can add where:
public function executeIndex(sfWebRequest $request)
{
$this->jobeet_job_list = Doctrine::getTable('JobeetJob')
->createQuery('a')
->where('id = ?', $id) //$id i have in session, this working OK
->execute();
}
how can i make similarly with executeEdit? in database i have field user_id, which added a news. I would like to edit can only author this news. thanks for help!
If its editing the job what you are trying to do, you have to find the job you want to edit and create a form initialized with the object. Something like (i'm skipping all the parameter checking bits, obviously you have to make sure that the id is in the request, the user is logged and the job retrieved exists and is owned by the user):
$user_id = $this->getUser()->getId(); // if you are logged and getId is defined in your myUser class
$job_id = $request->getParameter('id', false);
$job = Doctrine::getTable('JobeetJob')->find($job_id);
if ($job['owner_id'] == $user_id)
{
$this->form = new JobeetJobForm($job);
}

Categories