update user avatar with laravel5 - laravel

From the look of it seems to me very logic but I am missing something, it doesn't work, nothing changes!
Here is my profile controller file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Image;
class ProfileController extends Controller
{
public function profile()
{
$user = Auth::user();
return view('profile')->with('user', $user);
}
public function edit()
{
$user = Auth::user();
return view('edit')->with('user', $user);
}
public function update(Request $request)
{
if($request->hasFile('avatar'))
{
$avatar = $request->file('avatar');
$filename = time().'.'.$avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/users_avatars/'.$filename));
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return redirect('profile')->with('user', Auth::user());
}
}
and here is my edit.blade.php
#extends('layouts.app')
#section('content')
<div class="col-md-6">
{!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController#update', 'file'=>'true']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email') !!}
{!! Form::email('email', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('number', 'Phone') !!}
{!! Form::text('number', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group col-md-5">
{!! Form::label('avatar', 'Avatar') !!}
{!! Form::file('avatar', ['class'=>'form-control']) !!}
</div><br><br><br><br>
<div class="form-group">
{!! Form::submit('Update', null, ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
but when I edit the user it doesn't change..plz help

It seems like you have made a mistake while using Form from Laravel HTML Collective. You should use "files" => true instead of "file" => true
{!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController#update', 'file'=>'true']) !!}

Related

How to use $_GET variable from AJAX - LARAVEL

I need to use the $_GET variable into the blade.
The AJAX code work good, i received an alert with the correct value but i can't use it into the blade.
Route:
/*
|--------------------------------------------------------------------------
| Routes per gestire la pagina "Home"
|--------------------------------------------------------------------------
*/
Route::get('/home', 'HomeController#index');
Route::get('/home', array('as' => 'success', 'uses' => 'StatisticheController#totaleRichiesteAnnue'));
home.blade.php
#if(isset($_GET['tipologia_evento_id']))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
<script>
var tipologia_evento_id = event.tipologia_evento_id;
$.ajax({
type: "GET",
url: '/home',
data: { tipologia_evento_id: tipologia_evento_id},
success: function(msg)
{
alert(tipologia_evento_id);
$('#modal-event').modal('show');
}
});
</script>
In laravel 5 request() helper is available globaly
#if(request()->tipologia_evento_id)
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
request()->tipologia_evento_id is not set it will return null
Hope this helps
You can use the Request facade:
Request::get('tipologia_evento_id');
More details on: https://laravel.com/docs/5.6/requests
You should try below code:
#if(app('request')->input('tipologia_evento_id'))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
OR
#if(Request::get('tipologia_evento_id'))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
As you already have route and controller, why not pass it from controller and use it in view?
class HomeController extends Controller
{
public function index(Request $request)
{
$getData = $request->all();
return view('home', compact('getData'));
}
}
in your view you can easily access as $getData
So your home.blad.php will look something like this
#if($getData->tipologia_evento_id)
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
EDIT:
If you want to get json response using ajax
class HomeController extends Controller
{
public function index(Request $request)
{
$getData = $request->all();
return response()->json($getData);
}
}

Form model binding laravel 5.1 for multiple models

I want Form model binding for multiple objects in laracollective's Form package?
Something as following?
Form::model([$user,$vendors], array('route' => array('user.update', $user->id)))
Where can I request this feature?
I assume you're using Laravel-Collective, Unfortunately you cant do something like that. instead you can try something like this :
UPDATE
you can query all your model in your controller and combine them like this :
$user = User::where('id',$user_id)->get();
$vendor = Vendor::where('user_id',$user_id)->get();
//merge two model
$user = $user->merge($vendor);
// return $user;
return view('admin.users.edit', compact('user'))
->withTitle('Edit user');
and in your form call them like this :
{!! Form::model($user[1], ['route' => ['admin.users.update', $user],'method'=>'PUT']) !!}
#include('admin.users._formEdit')
<div>
{!! Form::submit('Save user', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
_formEdit.blade.php
<div class="form-group">
{!! Form::label('first_name', 'First Name : ') !!}
{!! Form::text('user[first_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name', 'Last Name : ') !!}
{!! Form::text('user[last_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('email', 'Email : ') !!}
{!! Form::email('user[email]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('vendor_name', 'vendor_name') !!}
{!! Form::text('vendor_name', null,['class' => 'form-control']) !!}
</div>
OR ANOTHER SOLUTION
create relation between model of your User and Vendor (one-to-one or one-to-many) example
User :
public function vendor(){
return $this->hasOne('App\Vendor','user_id');
}
Vendor:
public function user(){
return $this->belongsTo('App\User','user_id);
}
Build your response query like this :
$user = Vendor::with('user')->find($user_id);
and then in your view template :
{!! Form::model($user, ...) !!}
Vendor: {!! Form::text('vendor_name') !!}
User: {{ Form::text('user[username]') }}
{!! Form::close() !!}

Undefined variable: section (View: D:\xampp\htdocs\laravel5\resources\views\cms\sections.blade.php)

I try to made simple crud and found this error
Undefined variable: section
(View:D:\xampp\htdocs\laravel5\resources\views\cms\sections.blade.php)
Route:
Route::resource('sections','SectionsController');
View:
{!! Form::open(["url"=>"sections","files" => "true"]) !!}
section name: {!! Form::text("section_name")!!}
<hr/>
{!! Form::file('image',["class"=>"filestyle","data-buttonText"=>"select image","data-input"=>"false"]) !!}
<br/>
{!! Form::submit("insert#upload",["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
<div class="row">
<h3>get data from db ::</h3>
#foreach($sections as $section)
<div class="col-md-3">
<div class="thumbnail">
<p>{{$section->section_name}}</p>
<img src="/uploads/{{$section->image_name}}" width="90%" height="90%">
</div>
</div>
#endforeach
</div>
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
{!! Form::open(["url"=>"sections/$section->id","method" => "patch"]) !!}
{!! Form::text("section_name",$section->section_name)!!}
{!! Form::submit("update",["class"=>"btn btn-success"]) !!}
{!! Form::close() !!}
</div>
</div>
<div class="col-md-4">
<div class="thumbnail">
{!! Form::open(["url"=>"sections/$section->id","method" => "delete"]) !!}
{!! Form::submit("delete",["class"=>"btn btn-danger"]) !!}
{!! Form::close() !!}
</div>
</div>
</div>
Controller:
use DB;
use Input;
use Validator;
class SectionsController extends Controller {
public function index() {
$section=DB::table('sections')->get();
return view('cms.sections')->withSections($section);
}
public function create() {
return view('cms.sections');
}
public function store(Request $request) {
$this->validate($request,
['section_name' => 'required|unique:sections,section_name|max:20',
'image' => 'mimes:jpeg|max:1024']);
$section_name=$request->input('section_name');
$file=$request->file('image');
$destinationPath='uploads';
$filename=$file->getClientOriginalName();
$file->move($destinationPath,$filename);
$rules = array('image' => 'mimes:jpg,png,gif|required|max:10000');
$validator = Validator::make(Input::all(), $rules);
DB::table('sections')->insert(['section_name' => $section_name,
'image_name' => $filename]);
return redirect('sections');
}
public function update(Request $request, $id) {
$section_name=$request->input('section_name');
DB::table('sections')->where('id',$id)->update(['section_name'=>$section_name]);
return redirect('sections');
}
public function destroy($id) {
DB::table('sections')->where('id',$id)->delete();
return redirect('sections');
}
}
There is one definite problem in your View code: You use $section after you end your #foreach block that defines it, for example here:
#foreach($sections as $section)
...
#endforeach
...
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
{!! Form::open(["url"=>"sections/$section->id","method" => "patch"]) !!}
------------------------------------^^^^^^^^
I suspect you intended to place the <div class="row"> blocks inside your #foreach block.

storing data with name of author - laravel 5.2

I have hasMany relation to my model user and reports.
I want to set author name for the reports. (Like a blog-post author)
my model User:
public function reports() {
return $this->hasMany('App\Report', 'author_id');
}
model Report
public function user() {
return $this->belongsTo('App\User', 'author_id');
}
and my controller:
public function create()
{
$category = Category::lists('title','id');
return view('dash.reports.create')->with('category', $category);
}
/**
* Store a newly created resource in storage.
*
* #return void
*/
public function store(Request $request)
{
$this->validate($request, ['title' => 'required', ]);
Report::create($request->all());
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}
I'm able to set in in phpmyadmin, but how can i set it with my controller?
edit: my view:
{!! Form::open(['url' => '/dash/reports', 'class' => 'form-horizontal']) !!}
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('title', 'Servizio', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::text('title', null, ['class' => 'form-control', 'required' => 'required']) !!}
{!! $errors->first('title', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('date', 'Data lavorativa', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-2">
{!! Form::selectRange('day', 1, 31, null, ['class' => 'form-control']) !!}
{!! $errors->first('day', '<p class="help-block">:message</p>') !!}
</div>
<div class="col-sm-2">
{!! Form::selectMonth('month', null, ['class' => 'form-control']) !!}
{!! $errors->first('month', '<p class="help-block">:message</p>') !!}
</div>
<div class="col-sm-2">
{!! Form::select('year', array('2016' => '2016', '2015' => '2015'), null, ['class' => 'form-control']) !!}
{!! $errors->first('year', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : ''}}">
{!! Form::label('category_id', 'Cliente', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
{!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
{!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}
Very easy. Replace Report::create... with this.
$user = Auth::user();
$report = new Report($request->all());
$report->author()->associate($user);
$report->save();
Make sure you use Auth; up at the top.
This uses the Auth object to get the current user,
Builds a new Report using the $request data without saving,
Tells the report we're associating $user as the author for the model,
Saves the report with the authorship information.
solution:
public function store(Request $request)
{
$this->validate($request, ['title' => 'required', ]);
$user = Auth::user()->id;
$report = new Report($request->all());
$report->author_id = $user;
$report->save();
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}

Update data with Laravel Collective forms

I have an edit form with Laravel Collective but when clicking the button, the data do not update. Below are my codes.
Form:
{!! Form::model($post, ['route' => ['/post/update/', $post->id]]) !!}
{{ method_field('PATCH') }}
<div class="form-group">
<div class="row">
<div class="col-lg-6">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="col-lg-6">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'rows' => 10]) !!}
</div>
<hr/>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
</div>
{!! Form::close() !!}
Controller:
public function edit($id)
{
return \View::make('admin/post/edit')->with([
'post' => \DB::table('posts')->find($id),
'categories' => \App\Category::lists('category', 'id')
]);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return \Redirect::to('/admin/posts');
}
Routes:
Route::get('/admin/post/edit/{id}', 'Admin\PostController#edit');
Route::patch('/post/update/', [
'as' => '/post/update/',
'uses' => 'Admin\PostController#update'
]);
It's a bit different from the Laracast, and it's confusing me. Framework is new to me and the lack of code to do something is confusing.
I solved it. Mass Assignment. explains what to do if using update or create
So, the update method is:
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->category_id = $request->category_id;
$post->content = $request->content;
$post->save();
return \Redirect::to('/admin/posts');
}

Resources