Laravel 7: User Profile causing bug when uploading img and bio description - php

In a laravel 7 app I'm working on a feature where a user can upload their image profile and bio description. A problem that I'm facing is that there are certain cases when a user sets their profile image, the bio description would be gone. For example: If a user set their profile img and puts a description, but if the user sets another img then the description would be gone. Not quite sure what's the bugs that's causing it. (I'm using bootstrap modal for the user to edit their profile.)
controller.php
public function modalEditPost(Request $request)
{
$user = Auth::user();
$avatarName = "";
$userBio = "";
$request->validate([
'bio' => 'nullable|string|max:255',
'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->has('bio')) {
$userBio = $request->bio;
$user->bio = $userBio;
}
if ($request->hasFile('image')) {
if ($request->file('image')->isValid()) {
$extension = $request->image->extension();
$avatarName = $user->id.'_avatar'.time().'.'.$extension;
$request->image->move(public_path('/uploads/avatars'), $avatarName);
$user->avatar = $avatarName;
}
}
$user->save();
return back()
->with('success','You have successfully edited your bio.')
->with('bio', $userBio)
->with('image', $avatarName);
}
index.blade.php
#include('partials.popup')
<div class="col-md-4">
<div class="card card-user">
<div class="card-body">
<p class="card-text">
<div class="author">
<div class="block block-one"></div>
<div class="block block-two"></div>
<div class="block block-three"></div>
<div class="block block-four"></div>
<img class = "avatar" src="/uploads/avatars/{{ Auth::user()->avatar}}" alt="">
<h4 class="title">{{ auth()->user()->first_name }} {{ auth()->user()->last_name }}</h5>
</div>
<div class="card-description">
{{ Auth::user()->bio }}
</div>
<br>
<div class="text-center">
<button class="btn btn-success"
style="cursor: pointer"
data-toggle="modal"
data-target="#popupModal">{{ __('Edit Profile') }}
</button>
</div>
</div>
</div>
popup.blade.php
<div class="modal-body">
<!-- (Image upload) Start -->
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
#endif
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<!-- (Image upload) Start -->
<form action="{{ route('modal.upload.post') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control-file">
</div>
</div>
<br>
<div class="row">
<div class="col">
<p>Change Bio</p>
</div>
</div>
<div class="row">
<div class="col">
<input type="text" style="color:black" name="bio" class="form-control">
</div>
</div>
<br>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
<!-- (Image upload) End -->
</div>

Related

Php search filter. Array returns NULL

This is Search Controller
if ($request->has('query')) {
$search = $request->get('query');
$search = addslashes(str_replace('"', '', json_encode($search)));
$solution = Service::all();
$solutions = array();
foreach ($solution as $id => $val) {
if (strpos(mb_strtolower($val['title']), mb_strtolower($search))) {
$solutions[] = $solution[$id];
}
}
}
When I run the dd command, 10 pieces of data appear.
For example when I search for 'customer' the array returns null even though this data is exist.
This is search.blade.php
<div class="header-search-form default-search">
<form action="{{ route('search') }}" method="GET" class="search-form-top">
<input class="search-field" type="text" name="query" id="query" value="{{ request()->input('query') }}" placeholder="{{ trans('general.search') }}">
<button class="search-submit">
<i class="search-btn-icon fa fa-search"></i>
</button>
</form>
</div>
This is search-result.blade.php
<div class="row row--35">
#foreach ($solutions as $solution)
<div class="col-lg-4 col-md-6 mt-10" >
<a href="{{route('service_detail',$solution->slug)}}" class="box-large-image__wrap wow move-up">
<div class="box-large-image__box">
<div class="box-large-image__midea">
<div class="images-midea">
<img src="{{ coverImg('service',$solution->id) }}" class="img-fluid" alt="">
<div class="button-wrapper">
<div class="btn tm-button">
<span class="button-text">Detayları Gör</span>
</div>
</div>
<div class="heading-wrap">
<h5 class="heading">{{ $solution->title }}</h5>
</div>
</div>
</div>
<div class="box-large-image__content mt-30 text-center"></div>
</div>
</a>
</div>
#endforeach
</div>

how to get URL value in laravel?

So, i tried laravel and i got stuck when i want to get URL value as a value for my database. Here is the code in my controller.
public function store(Request $request, $shoe_id)
{
//Insert Data
$cart = new Cart;
$cart->user_id = Auth::user()->id;
$cart->shoe_id = $shoe_id;
$cart->quantity = $request->quan;
$cart->save();
return redirect()->back();
}
and here is the problem i've encountered:
it says that the {shoe_id} is string where i wanted to be an integer from the URL
Here what my website looks like:
My blade
Here is my web.php:
Route::get('/','homeController#main')->name('home');
Route::get('/home','homeController#main')->name('home');
Route::get('/detail/{shoe_id}', 'homeController#detail');
Route::get('/cart/{shoe_id}','CartController#show');
Route::post('/cart/{shoe_id}','CartController#store');
Here is my blade:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div style="text-align:center;">
<img src="{{ asset("$cart->shoe_image") }}" alt="" style=" width:350px; height:350px;">
<div class="card-header">{{$cart->shoe_name}}</div>
<div class="card-header">${{$cart->shoe_price}}</div>
<div class="card-header">{{$cart->shoe_description}}</div>
</div>
<div class="card-header form-group">
<form method="POST" action="/cart/{shoe_id}" enctype="multipart/form-data">
#csrf
<div class="form-row">
<div class="col-md-2">
<label for="quantity">Quantity:</label>
</div>
<div>
<input class = "form-control" type="text" id="quantity" name="quan"><br><br>
</div>
<button class="btn btn-primary" type="submit" name="addcart">Add to Cart</button>
</div>
</form>
{{-- <a class="btn btn-primary" href="#">Add to Cart</a> --}}
</div>
</div>
</div>
</div>
</div>
#endsection
what i want to do: input the inputted data to database. My database has table called cart with attribute of id, user_id, shoe_id.
In your form action you should provide the actual value for the shoe_id then it will work as expected. action="/cart/{{ $cart->shoe_id }}"
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div style="text-align:center;">
<img src="{{ asset("$cart->shoe_image") }}" alt="" style=" width:350px; height:350px;">
<div class="card-header">{{$cart->shoe_name}}</div>
<div class="card-header">${{$cart->shoe_price}}</div>
<div class="card-header">{{$cart->shoe_description}}</div>
</div>
<div class="card-header form-group">
<form method="POST" action="/cart/{{ $cart->shoe_id }}" enctype="multipart/form-data">
#csrf
<div class="form-row">
<div class="col-md-2">
<label for="quantity">Quantity:</label>
</div>
<div>
<input class = "form-control" type="text" id="quantity" name="quan"><br><br>
</div>
<button class="btn btn-primary" type="submit" name="addcart">Add to Cart</button>
</div>
</form>
{{-- <a class="btn btn-primary" href="#">Add to Cart</a> --}}
</div>
</div>
</div>
</div>
</div>
#endsection

Display another laravel blade view as a modal in a page

Good day everyone,
please, I am new to Laravel, I am creating a project management app, I created an index page for projects and I have a button where I can be able to create new projects, it is entirely another blade file, I need to display the blade as a modal, I tried using #include and bootstrap modal, but the index page is the one displaying in the modal popup instead of the create page, I don't know where I am getting it wrong, I need help because there is a lot of places I need to use the modal.
This is my index page
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-right">
<a class="btn btn-success" data-toggle="modal" href="{{ route('projects.create') }}" data-target="#createModal"> New Project</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered table-hover">
<thead class="text-primary">
<tr>
<th>No</th>
<th>Name</th>
<th>Description</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</thead>
#foreach ($projects as $project)
<tbody class="table-striped">
<tr>
<td>{{ ++$i }}</td>
<td>{{ $project->name }}</td>
<td>{{ $project->introduction }}</td>
<td>{{ $project->created_at->format('d/m/Y') }}</td>
<td>
<form action="{{ route('projects.destroy',$project->id) }}" method="POST">
<a class="fas fa-eye fa-lg text-warning mr-1" href="{{ route('projects.show',$project->id) }}"></a>
<a class="fas fa-edit fa-lg text-primary mr-1" href=" {{ route('projects.edit',$project->id) }}"></a>
#csrf
#method('DELETE')
<button type="submit" class="fa fa-trash fa-lg text-danger" style="border: none; background-color:white;"></button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $projects->links() !!}
#endsection
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal" data-attr="{{ route('projects.create') }}">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
#include('projects.create')
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
while this is the create page
#extends('layouts.modal')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>New Project</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projects.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
Thank you in advance
I later figure the answer, the mistake I was doing is including the
#extends('layouts.app')
#section('content')
So the page should just be the form or what you want to display without the #extends('layouts.app')
<form action="{{ route('projects.store') }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction" placeholder="Introduction"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>location:</strong>
<textarea class="form-control" style="height:150px" name="location" placeholder="Location"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>

Data retrieving issue in Laravel

I'm trying display some data in my blade. Following is my current code for the controller's(ActivateController.php) index function,
public function index($id)
{
//echo $id;
$datas = Website::WHERE('appId','=',$id);
//dd($datas);
return view('activities.index',compact('datas','id'));
}
And my view url is something like,
TEST.site/activities/index/12
this is my blade, index.blade.php
#extends('layouts.admin')
#section('content')
<div class="container">
<div class="row">
<div class="col-sm">
<div class="pull-left">
<h2 class="mt-2">Activate Website</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('websites.index') }}"> Back</a>
</div>
</div>
</div>
</div>
#if (count($errors) > 0)
<div class="container ">
<div class="row">
<div class="col-sm">
<div class="alert alert-danger mt-2">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
</div>
</div>
</div>
#endif
<div class="container mt-3">
<div class="row">
<div class="col-sm">
#foreach ($datas as $object)
<form id="w1" action="{{ route('activities.update',$object->appId) }}" method="POST">
#csrf
#method('PUT')
<strong>Select Package Type:<span class="star">*</span></strong>
<input type="text" name="app_domain" value="{{$object->domain}}">
<strong>Select Package Type:<span class="star">*</span></strong>
<select id="app-packagetype" class="form-control" name="package_type" aria-required="true">
<option value="{{$object->payment_option }}">{{$object->payment_option }}</option>
<option value="monthly">Monthly [12]</option>
<option value="yearly">Yearly [99]</option>
</select>
<br>
<div class="form-group text-right">
<button type="submit" class="btn btn-success">{{ __('Activate') }}</button>
</div>
</form>
#endforeach
</div>
</div>
</div>
#endsection
But now my issue is , when I try to retrieve the data,
{{$object->appId}}
It retrieves empty....
Where I'm doing wrong and how to retrieve the data properly to the view?
you are missing get() at the end
change this:
$datas = Website::where('appId','=',$id);
to
$datas = Website::where('appId','=',$id)->get();
Thanks..
You use just like this
$datas = Website::where('appId',$id)->get();

Method [vlidate] does not exist error in laravel 5.3 while trying to upload image

I am developing a project in laravel 5.3 where I have to create a 1 feild form to change logo of website my form look like this.
I do not need to save path in database. I just need to upload file in \public\images\ with name logo and only png files are allowed. so it will be \public\images\logo.png
following is the form code.
<div class="col-md-8 col-md-offset-2">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
<img src="/images/{{ Session::get('path') }}">
#endif
<div class="box box-info">
<div class="box-header with-border text-center">
<h3 class="box-title">Basic Info</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form class="form-horizontal" action="{{ url('/') }}/admin/change-site-logo" enctype="multipart/form-data" method="POST">
<div class="box-body">
{{ csrf_field() }}
<div class="form-group">
<label for="logo" class="col-sm-3 control-label">Logo</label>
<div class="col-sm-9">
<input type="file" class="form-control" id="logo" name="logo" placeholder="Logo Image">
</div>
</div>
<div class="form-group">
<label for="logo" class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<img style="width: 200px; height: 50px;" src="https://www.google.com.pk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-info pull-right">Save</button>
</div>
<!-- /.box-footer -->
</form>
</div>
<br /><br />
</div>
this is Route Route::post('/admin/change-site-logo', 'adminController#logo_change');
and controller is as following
class adminController extends Controller
{
public function logo_change(Request $request)
{
$this->vlidate($request, [
'logo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = 'logo.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);
return back()
->with('success', 'Image Uploaded Successfully.')
->with('in path', $imageName);
}
I am writing first time this kind of code so dont know what to fix. I just know that the error is Method [vlidate] does not exist.

Categories