Laravel - Can't load picture to database - php

I made a profile image upload for users and tried to copy the code and make the same for the custom icons users have on their links. The problem occurs when the name of the uploaded image needs to update in the database.
This is code I copied from UserController:
public function update_avatar(Request $request){
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->fit(300, 300)->save( public_path('/uploads/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return redirect()->back()
->with(['success' => 'Profile picture updated!']);
}
Here is code I am trying, but failing in LinkController:
public function custom_icon(Request $request){
if($request->hasFile('icon')){
$icon = $request->file('icon');
$filename = time() . '.' . $icon->getClientOriginalExtension();
Image::make($icon)->fit(300, 300)->save( public_path('/img/icons/' . $filename ) );
$icon->icon = $filename;
$icon->save();
}
return redirect()->back()
->with(['success' => 'Custom icon uploaded!']);
}
And here is part of my web.php
Route::group(['middleware' => 'auth', 'prefix' => 'dashboard', ], function() {
Route::get('/links', [LinkController::class, 'index']);
Route::get('/links/new', [LinkController::class, 'create'])->middleware('verified');
Route::post('/links/new', [LinkController::class, 'store']);
Route::get('/links/{link}', [LinkController::class, 'edit']);
Route::post('/links/{link}', [LinkController::class, 'update']);
Route::delete('/links/{link}', [LinkController::class, 'destroy']);
Route::post('/links/{link}', [LinkController::class, 'custom_icon']);
Route::get('/qrcode', [UserController::class, 'qrcode']);
Route::get('/settings', [UserController::class, 'settings']);
Route::get('/settings/edit', [UserController::class, 'edit']);
Route::get('/settings/profile', [UserController::class, 'profile']);
Route::get('/settings/qr', [UserController::class, 'qr']);
Route::post('/settings/edit', [UserController::class, 'update']);
Route::post('/settings/profile', [UserController::class, 'update_avatar']);
Route::post('/settings/qr', [UserController::class, 'qr']);
Route::get('/settings/vcard', [UserController::class, 'vcard']);
Route::get('/settings/vcard/name', [UserController::class, 'vcard_name']);
Route::get('/settings/vcard/contact', [UserController::class, 'vcard_contact']);
Route::get('/settings/vcard/info', [UserController::class, 'vcard_info']);
Route::get('/settings/help', [UserController::class, 'help']);
Route::get('/settings/help/devices', [UserController::class, 'devices']);
Route::get('/settings/help/account', [UserController::class, 'account']);
Route::get('/settings/help/terms', [UserController::class, 'terms']);
Route::get('/settings/help/contact', [ContactController::class, 'contact']);
Route::post('/settings/help/contact', [ContactController::class, 'send_email']);
});
Any help would be appreciated.

you can update user table directly for update user table by directly by eloquent
user::where('id',Auth::id())->update(['avatar'=>$filename])

Related

How can I fix laravel named routes to get the proper result

I'm a beginner with laravel and trying to figure out the routes. I searched the web but provided solutions don't help, so maybe you guys will be able to shed some light.
So below is my routes file (web.php)
Route::group(['middleware' => 'auth'], function () {
Route::get('/', [HomeController::class, 'home']);
Route::get('dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('billing', function () {
return view('billing');
})->name('billing');
Route::get('profile', function () {
return view('profile');
})->name('profile');
Route::get('rtl', function () {
return view('rtl');
})->name('rtl');
Route::get('user-management', function () {
return view('laravel-examples/user-management');
})->name('user-management');
Route::get('tables', function () {
return view('tables');
})->name('tables');
Route::get('virtual-reality', function () {
return view('virtual-reality');
})->name('virtual-reality');
Route::get('static-sign-in', function () {
return view('static-sign-in');
})->name('sign-in');
Route::get('static-sign-up', function () {
return view('static-sign-up');
})->name('sign-up');
Route::get('/logout', [SessionsController::class, 'destroy']);
Route::get('/user-profile', [InfoUserController::class, 'create']);
Route::post('/user-profile', [InfoUserController::class, 'store']);
Route::get('/login', function () {
return view('dashboard');
})->name('sign-up');
Route::get('/register', [RegisterController::class, 'create']);
Route::get('/user-list', [RegisterController::class, 'index'])->name('user-list');
Route::post('/register', [RegisterController::class, 'store']);
//Route::delete('user-destroy', [RegisterController::class, 'destroy'])->name('user-destroy');
Route::resource('users', RegisterController::class);
//Route::get('/viehicle-types', [ViehicleTypeController::class, 'index']);
// Route::resource('viehicle-types', ViehicleTypeController::class);
Route::group(['middleware' => ['role:admin|manager']], function () {
Route::resource('viehicle-types', ViehicleTypeController::class);
Route::resource('addon-types', AddonTypeController::class);
Route::resource('addons', AddonController::class);
Route::resource('job-types', JobTypeController::class);
Route::resource('jobs', JobController::class);
Route::resource('viehicles', ViehicleController::class);
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
Route::resource('finished-jobs', FinishedJobController::class);
});
});
My problem is that some of the name routes won't work.
Mainly these 2 don't work and display a 404 Error:
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
What is strange that when I check the routes:
$url=route('xxxx');
echo "The url is : " .$url;
I get the proper URL that works when entered in the web browser.
So I can't figure it out if anyone can help I will be greatful.
Cheers
HT

The POST method is not supported for this route. - Laravel

I have a problem with my create post page. When I submit I get this error:
The POST method is not supported for this route. Supported methods: GET, HEAD.
I have no clue where it comes from as I am pretty new to Laravel.
routes(web.php):
<?php
Route::get('/', [PagesController::class, 'index']);
Auth::routes();
Route::get('/blog', 'App\Http\Controllers\PostsController#index');
Route::get('/blog/create', 'App\Http\Controllers\PostsController#create');
Route::get('/logout', 'App\Http\Controllers\Auth\LoginController#logout', function (){
return abort(404);
});
Route::get('/home', 'App\Http\Controllers\HomeController#index');
Controller:
public function index()
{
return view('blog.index')
->with('posts', Post::orderBy('updated_at', 'DESC')->get());
}
public function create()
{
return view('blog.create');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' .
$request->image->extension();
$request->image->move(public_path('images'), $newImageName);
Post::create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'slug' => SlugService::createSlug(Post::class, 'slug',
$request->title),
'image_path' => $newImageName,
'user_id' => auth()->user()->id
]);
return redirect('/blog')
->with('message', 'Je post is toegevoegd!');
}
image for reference
You are using get for all your routes... You have to use Route::post... Read the documentation again about this topic so you can understand it better.
Remember that using Route::post, Route::get, Route::delete and more, is the method you are allowing the route to be accessed with.
It comes from your Routes:
Route::get('/', [PagesController::class, 'index']);
Auth::routes();
Route::get('/blog', 'App\Http\Controllers\PostsController#index');
Route::get('/blog/create', 'App\Http\Controllers\PostsController#create');
Route::get('/logout', 'App\Http\Controllers\Auth\LoginController#logout', function (){
return abort(404);
});
Route::get('/home', 'App\Http\Controllers\HomeController#index');
You have GET routes, but no POST routes. You need Route::post in order to support POST.
You syntax for registering the route for post method is incorrect.
It should be: Route::post($uri, $callback); where callback can be any function in controller layer or service layer.
If you want to register your route for multiple http verbs, you can do so using any of the below methods.
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
Refer to this documentation. It explains the concept in simple terms.

Controller is returning a blank data

the controller is returning a blank data/view and I think something is wrong with my routes. if I remove {locale}, the data is retrieved.
Can anyone help with returning the data properly while my routes have {locale} in it? Here are my related code:
Web.php
Route::get('{locale}/projects/{id}/billings', 'ProjectController#showbilling')
->name('showbilling');
Route::post('{locale}/projects/{id}', 'ProjectController#addbilling')
->name('addbilling');
ProjectController.php
public function showbilling($id)
{
$billings = Project::find($id);
$locale = app()->getLocale();
return $billings;
//return view('admin.addbillings', compact('billings'));
}
Edit: Here's my full web.php
web.php
Route::get('/', function() {
return redirect(app()->getLocale());
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function () {
Route::get('/', function () {
return view('welcome');
})->name('main');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
//Customers
Route::get('/customers', 'CustomerController#showcust')->name('customers');
Route::post('/sendcust', 'CustomerController#sendcust')->name('sendcust');
//Items
Route::get('/items', 'ItemController#showitems')->name('items');
Route::post('/senditem', 'ItemController#senditem')->name('senditem');
//Projects
Route::get('/projects', 'ProjectController#showprojects')->name('projects');
Route::post('/sendproj', 'ProjectController#sendproj')->name('sendproj');
//ProjectBillings
Route::get('/projects/{id}/billings', 'ProjectController#showbilling')->name('showbilling');
Route::post('/projects/{id}', 'ProjectController#addbilling')->name('addbilling');
//Invoices
Route::get('/invoices', 'InvoiceController#showinvoice')->name('invoices');
Route::post('/sendinvoitem', 'InvoiceController#sendinvoitem')->name('sendinvoitem');
Route::get('/invoices/{id}/details', 'InvoiceController#showdetails');
Route::post('/updateitem','InvoiceController#updatedetail')->name('updateitem');
Route::get('invoices/{id}/generate', 'InvoiceController#generate');
Route::post('/updatestatus', 'InvoiceController#changestatus')->name('updatestatus');
});
You are passing 2 params in your route but accepting only 1 in the controller. Add locale.
public function showbilling($locale, $id)

Laravel 5.6 distribute routes by user params

In the routes app/routes/web.php I have a group with a bunch of routes for resources:
Route::group(['middleware' => 'auth'], function()
{
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
});
Auth::routes();
Route::get('/logout', 'Auth\LoginController#logout')->name('logout');
And now I want to distribute routes in this group by user parameter:
use App\UserGroup;
$access = UserGroup::find( auth()->user()->group_id )->access;
Route::group(['middleware' => 'auth'], function()
{
if ($access == 1) {
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
}
});
But I can't get user params right in the Route::group because an error occurs: Trying to get property 'group_id' of non-object
How to get auth()->user()->group_id in app/routes/web.php before middleware?
Use $access param in callback
$access = UserGroup::find( auth()->user()->group_id )->access
Route::group(['middleware' => 'auth'], function() use ($access)
{
if ($access == 1) {
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
}
});
U can create middleware 'GroupAccess'. Then assign it to alias, i.e.
'groupAccess' => \App\Http\Middleware\GroupAccess::class
Finally in it's handle method check your condition
public function handle($request, Closure $next)
{
if (UserGroup::find( auth()->user()->group_id )->access === 1)
return $next($request);
else
return back();
}
Then just specify this middleware in route group
Route::group(['middleware' => ['auth','groupAccess']], function()
{
Route::get('/points', 'PagesController#points');
Route::get('/users', 'PagesController#users');
Route::get('/users/groups', 'PagesController#user_groups');
Route::resource('point', 'PointController');
Route::resource('user', 'UserController');
Route::resource('users/group', 'UserGroupController');
});

Laravel 5.4 : Translate routes

is there a simple way to translate my routes in Laravel 5.4. My translation files located in here:
/resources
/lang
/en
routes.php
/de
routes.php
In my web.php i define my routes like this:
Route::get('/{locale}', function ($locale) {
App::setLocale($locale);
return view('welcome');
});
Route::get('{locale}/contact', 'ContactController#index');
I have found very elaborate solutions or solutions for Laravel 4. I am sure that Laravel has also provided a simple solution. Can someone explain to me the best approach?
Thanks.
we usually do it like this
to get the current language:
$request = request()->segment(1);
$language = null;
if (!empty($request) && in_array($request,config('translatable.locales'))) {
$language = $request;
App::setLocale($language);
} else {
$language = 'nl';
}
routes:
Route::group(['prefix' => $language], function () {
Route::get(trans('routes.newsletter'), array('as' => 'newsletter.index', 'uses' => 'NewsletterController#index'));
I created a file translatable.php in my config folder:
<?php
return [
'locales' => ['en', 'de'],
];
web.php:
$request = request()->segment(1);
$language = null;
if (!empty($request) && in_array($request,config('translatable.locales'))) {
$language = $request;
App::setLocale($language);
} else {
$language = 'de';
}
Route::get('/', function() {
return redirect()->action('WelcomeController#index');
});
Route::group(['prefix' => $language], function () {
/*
Route::get('/', function(){
return View::make('welcome');
});
*/
Route::get('/',
array( 'as' => 'welcome.index',
'uses' => 'WelcomeController#index'));
Route::get(trans('routes.contact'),
array('as' => 'contact.index',
'uses' => 'ContactController#index'));
});
Works fine - Thanks. Is the redirect also the best practice?
Best regards

Categories