updating image hasfile condition - laravel

Now i'm trying to update an image but in method update it keeps skip the hasfile condition
public function update(Request $request, $id)
{
$slider = Slider::find($id);
$slider->header = $request->header;
$slider->paragraph=$request->paragraph;
if($request->hasFile('image')){
return 'a';
// $image=$request->file('image');
// $filename=time(). '.' .$image->getClientOriginalExtension();
// $location=public_path('images/' . $filename);
// Image::make($image)->save($location);
// $oldFilename=$slider->image;
// $slider->image=$filename;
// File::delete(public_path('images/'. $oldFilename));
}else{
return 'whatever';
}
}
and here's my view
<form class="form-horizontal" action="{{ route('slider.update',$slider->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{method_field('PATCH')}}
<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-title back-change">
<h5>الغلاف </h5>
</div>
<div class="ibox-content">
<div class="row">
<div class="col-md-6">
<div class="image-crop">
<img src="{{asset('images/'.$slider->image)}}">
</div>
</div>
<div class="col-md-6">
<div class="btn-group">
<label title="Upload image file" for="inputImage" class="btn btn-primary">
<input type="file" name="image" id="inputImage" class="hide">
Upload new image
</label>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-primary pull-right" type="submit"> حفظ التغيرات</button>
</div>
</div>
</div>
</div>
</div>
Why can I not get to the condition?
The form is okay and the name of the input is okay, but it still returns to else.

Related

Insert into database postgres with october cms

Hi all I am new into october cms and I am facing trouble that makes my head spinning around. I have a form that get data from user, I am using builder.
This is my form :
{% put sudah %}{% partial "expert/yes" %}{% endput %}
{% put styles %}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Ubuntu:wght#300;400;500;600;700&display=swap">
{% endput %}
<div class="text-center bg-primary">
<h4 class="text-white my-auto py-5 title-menu">Expert Registration Form</h4>
</div>
<div class="opening" id="first-menu">
<p>Expert Registration Form</p>
<button onclick="changeMenu()" class="btn btn-primary">
Bergabung
<i class="fa fa-arrow-right" aria-hidden="true"></i>
</button>
</div>
<div class="tab-content d-none pb-4" id="second-menu">
<div class=" container">
<div class="head-menu">
<p>Have you registered yet ?</p>
</div>
<div class="p-2">
<div class="form-check my-3">
<input class="form-check-input" type="radio" name="radioPick" id="yes" value="yes" checked>
<label class="form-check-label" for="radioPick">Yes</label>
</div>
<div class="form-check my-3">
<input class="form-check-input" type="radio" name="radioPick" id="no" value="no">
<label class="form-check-label" for="radioPick">No</label>
</div>
</div>
<div class="btn-next mt-4">
<button onclick="secondMenu()" class="btn btn-primary">Next</button>
</div>
</div>
</div>
<div id="yes" class="d-none">
{% placeholder yes%}
</div>
{% put scripts %}
<script>
const changeMenu = () => {
$( "#first-menu" ).addClass('d-none');
$( "#second-menu" ).removeClass('d-none');
}
const secondMenu = () => {
let radio = $('input[name=radioPick]:checked').val()
$( "#second-menu" ).addClass('d-none');
if(radio === 'yes'){
$( "#yes" ).removeClass('d-none');
}
}
</script>
{% endput %}
Then if yes, form for yes appeared
this is yes form :
<form method="POST" action="" accept-charset="UTF-8" enctype="multipart/form-data" id="example" class="something">
<input type="hidden" name="handler" value="onSave">
{{ form_token() }}
{{ form_sessionKey() }}
<div class="tab-content py-4" id="second-menu">
<div class="container">
<div class="content-letter tab">
<p class="title-letter">Please Fill This Form</p>
<div class="content-letter tab">
<div class="mt-3">
<div class="form-group row">
<div class="col-12">
<label class="text-dark font-weight-bold">Nama</label>
<input type="text" class="form-control" placeholder="Name" name="name"
id="name" required>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label class="text-dark font-weight-bold">Phone</label>
<input type="number" class="form-control" placeholder="Phone" name="phone"
id="phone" required>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label class="text-dark font-weight-bold">Signature</label>
<div class="row">
<div class="col-9 col-md-10">
<div class="custom-file">
<input type="file" id="signature" class="custom-file input-file"
name="signature" accept="image/x-png,image/gif,image/jpeg">
<label id="label-sign" for="sign"
class="custom-file-label label-files">Upload Signature</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<button
id="btn-okay"
type="submit"
data-request="onSave"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="Creating New..."
class="btn btn-primary">
Join
</button>
</div>
</div>
</form>
and in code section I wrote this function :
function onSave() {
$expert= new Expert();
$model = new \Models\Expert;
$expert->name = Input::get('name');
$expert->phone = Input::get('phone');
$expert->sign= Input::file('signature');
$expert->save();
return Redirect::back;
//or even this one
/*$nama = Input::get('name');
$phone = Input::get('phone');
$sign = Input::file('signature');
DB::table('expert')->insert([
'name' => $name,
'phone' => $phone,
'sign' => $sign
]);
return Redirect::back;*/
}
and not forget I attach model in expert model :
public $attachOne = [
'signature' => 'System\Models\File'
];
Please help me, what is wrong with my code ? Thank you
Check the documents out on working with models. Your php function should be:
use Author\Plugin\Models\Expert;
function onSave() {
$expert= new Expert;
$expert->name = Input::get('name');
$expert->phone = Input::get('phone');
$expert->sign = Input::file('signature');
$expert->save();
return Redirect::back;
}

Laravel - Error: 403 while trying to upload picture

In my Laravel-5.8, I want to upload picture
Http\Controllers\HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Hr\HrEmployee;
use App\Http\Requests\Hr\Employee\UploadPictureRequest;
class HomeController extends Controller
{
public function update_picture(UploadPictureRequest $request, $id)
{
DB::beginTransaction();
try{
$employee = HrEmployee::find($id);
if ($request->emp_image != "") {
$emp_image = $request->file('emp_image');
$new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
$emp_image->move(public_path('storage/employees/image'), $new_name);
$employee->emp_image = $new_name;
}
$employee->save();
DB::commit();
Session::flash('success', 'Picture Successfully Uploaded');
return redirect()->route('dashboard');
}
catch (Exception $exception)
{
DB::rollback();
Session::flash('error', 'Action failed!');
return redirect()->back();
}
}
}
view
<span data-toggle="tooltip" data-original-title="Click To Upload Picture">
<a class="btn btn-info btn-block text-white" data-toggle="modal" data-target="#upload-picture{{ $employee->id }}" data-original-title="Picture">
<b>Upload My Picture</b>
</a>
</span>
<div class="modal fade" id="upload-picture{{ $employee->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form">
{{ csrf_field() }}
<div class="modal-header">
Self-Review Comment
</div>
<div class="col-md-12">
<div class="text-center">
#if($employee->emp_image != '')
<input type="image" src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="165" disabled/>
<!--<input type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
<div class="row">
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<input type="file" name="emp_image" id="wizard-picture" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
</div>
#else
<input type="image" src="{{asset('theme/adminlte3/dist/img/default.png')}}" class="profile-user-img img-fluid img-circle" id="wizardPicturePreview" title="" width="150" height="150" disabled/>
<!--<input type="file" name="emp_image" id="wizard-picture" class="" hidden>-->
<div class="row">
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<input type="file" name="emp_image" id="wizard-picture" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
</div>
</div>
</div>
#endif
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" id="upload_pic_btn-submit" class="btn btn-success btn-ok">Save</button>
</div>
</form>
</div>
</div>
</div>
view\dashboard
The form is a modal form inside a view called dashboard
route/web
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::post('update_picture/{id}', [
'uses' => 'HomeController#update_picture',
'as' => 'update_picture'
]);
When I submited the form, I got this error:
Error: 403 while trying to upload picture
Then when I did php artisan route:list, I got:
| POST | update_picture/{id} | update_picture | App\Http\Controllers\HomeController#update_picture | web,auth |
How do I resolve it?
Thank you.
Or simply you can try this
Route::put('update_picture/{id}''HomeController#update_picture')->name('update_picture');
please add this enctype="multipart/form-data" in form tag
<form action="{{route('update_picture',['id'=>$employee->id])}}" method="post" id="update-picture-form" enctype="multipart/form-data">
and replace this line $new_name = rand() . '.' . $emp_image->getClientOriginalExtension();
with this line $new_name = rand() . '.' . $request->emp_image->getClientOriginalExtension();

How to fix query in edit view?

i'm setting up a new project to perform multi language form but i'm stuck in edit form i don't know how to handle that
I created my controller and create view the only thing i need is edit view
so you can check my create view in bellow that work fine :
<div class="card-body text-center">
{!! Form::open(['route' => 'content.store', 'method' => 'Post']) !!}
<div class="card">
<div class="card-body">
<div class="form-group">
<label class="mx-4" for="my-input">{{ __('content/form.country_t') }}:</label>
<input id="my-input " type="text" name="country" placeholder="{{ __('content/form.country') }}">
<label class="mx-4" for="my-input">{{ __('content/form.city_t') }}:</label>
<input id="my-input" type="text" name="city" placeholder="{{ __('content/form.city') }}">
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="card col-xs-12 p-0">
<nav>
<div class="nav nav-pills nav-fill card-header" id="nav-tab" role="tablist">
#foreach (config('translatable.locales') as $la=>$desc)
<a class="nav-item nav-link" id="nav-home-tab" data-toggle="tab" href="#{{ $la }}" role="tab" aria-controls="nav-home" aria-selected="true">{{ $desc }}</a> #endforeach
</div>
<div class="tab-content py-3 px-3 px-sm-0 card-body" id="nav-tabContent">
#foreach (config('translatable.locales') as $la=>$desc)
<div class="tab-pane fade px-4" id="{{ $la }}" role="tabpanel" aria-labelledby="nav-home-tab">
<div class="form-group">
<label for="my-input" class="">{{ __('content/form.title') }}</label>
<input id="my-input" class="form-control" type="text" name="translations[{{ $la }}][title]">
</div>
<div class="form-group">
<label for="my-input" class="">{{ __('content/form.body') }}</label>
<input id="my-input" class="form-control" type="text" name="translations[{{ $la }}][body]">
</div>
</div>
#endforeach
</div>
</nav>
</div>
<button type="submit" class="row col-12 mt-2 mx-auto btn btn-primary">{{ __('content/form.submit') }}</button>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
and this is my controller :
public function store(Request $request)
{
$contents = new Content;
// $contents->fill($request->all());
$this->fillRequest($request,$contents);
$contents->User()->associate(\Auth::user());
$contents->saveOrFail();
return redirect()->route('content.index')->with('success','با موفقیت ساخته شد');
}
private function fillRequest(Request $request, Content $model)
{
//fill model on fillable variables
$model->fill($request->only($model->getFillable()));
$model->saveOrFail();
foreach ($request->translations as $la => $desc) {
//if title field is null ignore the translations
// in case of there is a translation... delete it
if (!$desc["title"]) {
if ($model->hasTranslation($la)) {
$model->deleteTranslations($la);
}
continue;
}
//create new translation if not exists
$model->translateOrNew($la)->fill($desc);
$model->saveOrFail();
}
return $model;
}
I need to know how can i create edit view exactly same as my create view above

Getting error when click in my second post for the single page

When I am trying to open first post in single page, it's opening and when trying to open my second post in single page it;s showing "Trying to get property 'title' of non-object"
Here is code
FrontendController
public function singlePost($slug)
{
$post= Post::where('slug', $slug)->first();
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::take(4)->get());
}
single.blade.php
in that I am using same frontend controller for same page
#extends('layouts.frontend')
#section('content')
<div id="product-post">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="heading-section">
<img src="{{$post->featured}}" alt="" />
</div>
</div>
</div>
<div id="single-blog" class="page-section first-section">
<div class="container">
<div class="row">
<div class="product-item col-md-12">
<div class="row">
<div class="col-md-8">
<div class="product-content">
<div class="product-title">
<h3>{{$post->title}}</h3>
<span class="subtitle">4 comments</span>
</div>
<p>
{!! $post->content!!}
</p>
</div>
<div class="leave-form">
<form action="#" method="post" class="leave-comment">
<div class="row">
<div class="name col-md-4">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="email col-md-4">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
<div class="subject col-md-4">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row">
<div class="text col-md-12">
<textarea name="text" placeholder="Comment"></textarea>
</div>
</div>
<div class="send">
<button type="submit">Send</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
You have to check whether the value is coming from table or not before getting exact column value. In that case if your table return empty result you can redirect it to 404 page.
Please refer the code below :
public function singlePost($slug)
{
$post= Post::where('slug', $slug)->first();
if($post) {
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::take(4)->get());
} else {
// You can redirect to 404 page
}
}

Store and update method together using modal view laravel

I've a store and update method that I would like to use the same modal box to prompt, however I notice that the store method takes the syntax of
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>
whereas my update method,
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
Is there a way that I can specify which method to use depending on the option chosen, I have two buttons created, each respectively.
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<button class="btn btn-sm btn-warning" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
Is there a way I can call separately using the same modal box or I have to create two duplicate modal box, one for store, the other for update?
My partial code is shown below ..
blade.php
<div class="well col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
<div class="row user-row">
<div class="col-xs-2 col-sm-3 col-md-4 col-lg-4">
<h5 style="font-weight: bold">{{ $user->name }}</h5>
</div>
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 dropdown-user" data-for=".{{ $user->id }}">
<h5 class="glyphicon glyphicon-chevron-down text-muted pull-right"> </h5>
</div>
</div>
<div class="row user-infos {{ $user->id }}">
<div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">User Information</h2>
</div>
<div class="panel-body">
<div class="row">
<div class=" col-md-10 col-lg-10 hidden-xs hidden-sm">
<div class="col-xs-5">User level:</div><div class="col-xs-5"> {{ $user->role->role_description }}</div>
<div class="col-xs-5">Email:</div> <div class="col-xs-5"> {{ $user->email }}</div>
<div class="col-xs-5">Phone number: </div> <div class="col-xs-5"> {{ $user->mobile }} </div>
<div class="col-xs-5">Office extension: </div> <div class="col-xs-5"> [ TO IMPLEMENT ]</div>
</div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-sm btn-warning btn--edit" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
<span class="pull-right">
<button class="btn btn-sm btn-danger" type="button">Inactive <i class="glyphicon glyphicon-remove"></i></button>
</span>
</div>
</div>
</div>
</div>
<input type="hidden" name="user_id" value="{{ $user->id }}" />
#endforeach
</div>
#if(Session::has('flash_message'))
<div class="alert alert-success col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
{{ Session::get('flash_message') }}
</div>
#endif
<div class="col-sm-offset-1 col-sm-2">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<!-- Modal -->
<div id="form" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User Information</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-3" for="name">Username:</label>
<div class="col-sm-5 #if ($errors->has('name')) has-error #endif">
<input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password">Password:</label>
<div class="col-sm-5 #if ($errors->has('password')) has-error #endif">
<input type="password" class="form-control" type="hidden" id="password" name="password" placeholder="Enter login password">
#if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> #endif
</div>
</div>
...
controller.php
class ManageAccountsController extends Controller
{
public $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function index()
{
$users = User::orderBy('name')->get();
$roles = Role::all();
return view('manage_accounts', compact('users', 'roles'));
}
public function store(StoreUserRequest $request)
{
// validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
$this->userRepository->upsert($request);
Session::flash('flash_message', 'User successfully added!');
return redirect()->back();
}
public function update(StoreUserRequest $request, $id)
{
// validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
$this->userRepository->upsert($request, $id);
Session::flash('flash_message', 'User successfully updated!');
return redirect()->back();
}
}
class UserRepository {
public function upsert($data, $id)
{
// You will also need something like this
if(isset($id))
{
$user = User::find($id);
}
else {
$user = new User;
}
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = Hash::make($data['password']);
$user->mobile = $data['mobile'];
$user->role_id = $data['role_id'];
// save our user
$user->save();
return $user;
}
}

Resources