I am currently doing an instagram clone project, and I have encountered a problem where the profile image is appearing broken. I have tried to echo out the path, and the problem is, despite this being the path specified:
/storage/app/public/profile/9UHe7CSRK4V9SNESMz0BAYggmQOp2G04J3Ygcgtl.png
The browser is returning this path
/storage/app/public/profile//9UHe7CSRK4V9SNESMz0BAYggmQOp2G04J3Ygcgtl.png
So you can see that the browser is adding a second '/' after profile, I don't know why.
Here is my index.blade
<div class="col-3 p-5">
<img src="{{ dd($user->profile->profileImage()) }}" class="rounded-circle w-100">
</div>
My Profile model
class Profile extends Model
{
protected $guarded = [];
public function profileImage()
{
$imagePath = ($this->image) ? $this->image : 'profile/9UHe7CSRK4V9SNESMz0BAYggmQOp2G04J3Ygcgtl.png';
return '/storage/app/' . $imagePath;
}
Profiles controller
class ProfilesController extends Controller
{
public function index(User $user)
{
return view('profiles.index', compact('user'));
}
public function edit(User $user)
{
$this->authorize('update', $user->profile);
return view('profiles.edit', compact('user'));
}
public function update(User $user)
{
$this->authorize('update', $user->profile);
$data = request()->validate([
'title' =>'required',
'description' =>'required',
'url' =>'url',
'image' =>'',
]);
if(request()->hasfile('image')){
$imagePath = request()->file('image')->store('profile', 'public');
$image = Image::make(public_path("storage/{$imagePath}"))->fit(1200, 1200);
$image->save();
}
auth()->user()->profile->update(array_merge(
$data,
['image' => $imagePath]
));
return redirect("/Profile/{$user->id}");
}
}
Routes
Route::get('/p/create', [App\Http\Controllers\PostsController::class, 'create'])->name('posts.create');
Route::get('/p/{post}', [App\Http\Controllers\PostsController::class, 'show']);
Route::post('/p', [App\Http\Controllers\PostsController::class, 'store']);
Route::get('/Profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('Profile.show');
Route::get('/Profile/{user}/edit', [App\Http\Controllers\ProfilesController::class, 'edit'])->name('profiles.edit');
Route::patch('/Profile/{user}', [App\Http\Controllers\ProfilesController::class, 'update'])->name('profiles.update');
Thanks in advance
You could load it into base64 with this PHP code:
$type = pathinfo(dd($user->profile->profileImage()), PATHINFO_EXTENSION);
$data = file_get_contents(dd($user->profile->profileImage()));
$imgbase64 = "data:image/" . $type . ";base64," . base64_encode($data);
then use
<img src="<?php echo $imgbase64; ?>" class="rounded-circle w-100">
This will then display the base64 image, which doesn't store the URL in plain text.
Related
This is my banners structure in home page :
As you can see I have 4 section for banners - small banners | medium banners | news banners | large banners
And I have one model called Banner , 4 controllers to manage this banners and 4 tables to save data.
This is Banner model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model
{
protected $fillable = [
'title', 'image', 'url', 'image_title', 'image_alt'
];
}
And Controllers :
SmallController :
class SmallController extends Controller
{
public function small_list()
{
$smallBanners = DB::table('small_banner')->get();
return view('admin.banners.small.list', compact('smallBanners'));
}
public function small_create()
{
return view('admin.banners.small.add');
}
public function small_store(Request $request)
{
$data = $request->validate([
'title' => 'required',
'url' => 'required',
'image' => 'required',
'image_title' => 'max:255',
'image_alt' => 'max:255'
]);
DB::table('small_banner')->insert($data);
return redirect(route('admin.banners.small.index'));
}
public function small_edit($id)
{
$small = DB::table('small_banner')->where('id', $id)->first();
return view('admin.banners.small.edit', compact('small'));
}
public function small_update(Request $request, $id)
{
$small = DB::table('small_banner')->where('id', $id)->first();
if ($request->has('image')) {
if (file_exists($small->image)) {
unlink($small->image);
}
DB::table('small_banner')->where('id', $id)->update([
'image' => $request['image']
]);
}
DB::table('small_banner')->where('id', $id)->update([
'title' => $request['title'],
'url' => $request['url'],
'image_title' => $request['image_title'],
'image_alt' => $request['image_alt']
]);
return redirect(route('admin.banners.small.index'));
}
public function small_delete($id)
{
$small = DB::table('small_banner')->where('id', $id)->first();
DB::table('small_banner')->where('id', $id)->delete();
if (file_exists($small->image)) {
unlink($small->image);
}
return redirect(route('admin.banners.small.index'));
}
}
Other Controllers are like SmallController
And this is how I show this banners :
#foreach($smallBanners as $small)
<div class="col-6 col-lg-3">
<div class="widget-banner card">
<a href="{{ $small->url }}" target="_blank" rel="noopener">
<img class="img-fluid w-100" loading="lazy"
src="{{ $small->image }}" title="{{ $small->title }}"
alt="{{ $small->image_alt }}" width="350" height="200">
</a>
</div>
</div>
#endforeach
Other views like small banner.
But in this case, for example in small banners, if we upload 5 images instead 4 images, the structure will be messed up.
What is the best way to manage this banners and optimize codes ?
let's back to the concept, starting from reducing table usage, or you can stay with your concept
lets's change the structure into below
table : banners
columns :
$table->increments('id');
$table->string('title');
$table->string('image');
$table->string('url');
$table->string('image_title')->nullable(); //guessing from validator that it can be null
$table->string('image_alt')->nullable();
//extra columns
$table->enums('banner_type', ['small', 'medium', 'large', 'news']);
//or
$table->string('banner_type');
$table->boolean('isActive')->default(0);
you have model, but not using it
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model
{
protected $table = 'banners'; //add this line to define table name, make sure you have set the database config in .env
protected $fillable = [
'title', 'image', 'url', 'image_title', 'image_alt', 'banner_type', 'isActive'
];
}
now reducing the controller used to manage banners into just 1 Controller
use Banner;
class BannerController extends Controller
{
public function index()
{
$banners = Banner::get();
return view('admin.banners.index', compact('banners'));
}
public function create()
{
return view('admin.banners.create');
}
public function store_count($request, $type)
{
//using array limit
return Banner::where('banner_type', $type)
->where('isActive', 1)->count() < $this->limits[$type] && $request->isActive == 1;
}
public function update_count($banner, $type)
{
return Banner::whereNotIn('id', [$banner->id])
->where('isActive', 1)
->where('type', $banner->banner_type)->count() < $this->limits[$type] && $banner->isActive == 1;
}
public function store(Request $request)
{
//validating form data
$data = $request->validate([
'title' => "required",
'url' => "required",
'image' => "required",
'image_title' => "max:255",
'image_alt' => "max:255",
'banner_type' => "required|in:small,medium,large,news",
'isActive' => "nullable|in:0,1" //active or not
]);
//validating images active count
if (!$this->store_count($request, $request->banner_type)) {
return redirect()->back()->withInput($request->all())
->withErrors(['isActive' => ' نمیتوان بیشتر از ' . $this->limits[$request['banner_type']] . ' عکس برای این بنر آپلود کرد! ']);
}
Banner::create($data);
return redirect(route('admin.banners.index'));
}
public function show($id)
{
$banner = Banner::findOrFail($id);
return view('admin.banners.edit', compact('banner'));
}
public function update(Request $request, $id)
{
$banner = Banner::findOrFail($id);
//validate update form data here
//your validation
//validating images active count
if(!$this->update_count($banner, $request->banner_type)){
return redirect()->back()
->withInput($request->all())
->withErrors(['isActive' => 'There cant be more than '.$this->limits[$request['banner_type']].' images active');
}
$banner = $banner->fill([
'title' => $request['title'],
'url' => $request['url'],
'image_title' => $request['image_title'],
'image_alt' => $request['image_alt'],
'banner_type' => $request['banner_type'],
'isActive' => $request['isActive'] ?? 0
]);
if ($request->has('image')) {
if (file_exists($banner->image)) {
unlink($banner->image);
}
$banner->image = $request['image'];
}
$banner->update();
return redirect(route('admin.banners.index'));
}
public function delete($id)
{
$banner = Banner::findOrFail($id);
if (file_exists($banner->image)) {
unlink($banner->image);
}
$banner->delete();
return redirect(route('admin.banners.index'));
}
}
now we setup code to choose which images are active, you can use ajax method or use controller above
public function set_active($id)
{
$banner = Banner::findOrFail($id);
$this->validate_count((new Request([])), $banner->banner_type);
$banner->update(['isActive' => 1]);
return redirect(route('admin.banners.index'));
}
//you can use array if want to set different limit of banner type, put it as public variable inside controller class
public $limits = [
'small' => 4,
'medium' => 4,
'large' => 4,
'news' => 4
];
load the data resource into view
public class home()
{
$small = Banner::where('banner_type', 'small')
->where('isActive', 1)->get();
$medium = Banner::where('banner_type', 'medium')
->where('isActive', 1)->get();
$large = Banner::where('banner_type', 'large')
->where('isActive', 1)->get();
$news = Banner::where('banner_type', 'news')
->where('isActive', 1)->get();
return view('home', compact('small', 'medium', 'large', 'news'));
}
I am using Laravel for my web app and I want to associate files to my posts in indepent way with his own form, but I have some problems
My routes (I am using a auth control package, but actually I am admin):
Route::post('file', 'fileController#store')->name('file.store')
->middleware('permission:file.create');
Route::get('file', 'fileController#index')->name('file.index')
->middleware('permission:file.index');
Route::get('file/create/', 'fileController#create')->name('file.create')
->middleware('permission:file.create');
Route::put('file/{id}', 'fileController#update')->name('file.update')
->middleware('permission:file.edit');
Route::get('file/{id}', 'fileController#show')->name('file.show')
->middleware('permission:file.show');
Route::delete('file/{id}', 'fileController#destroy')->name('file.destroy')
->middleware('permission:file.destroy');
Route::get('file/{id}/edit', 'fileController#edit')->name('file.edit')
->middleware('permission:file.edit');
Route::get('download/{filename}', 'fileController#download')->name('file.download')
->middleware('permission:file.download');
My migration:
Schema::create('files', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('files_id')->unsigned();
$table->string('filenames');
$table->integer('fileable_id')->unsigned();
$table->string('fileable_type');
$table->timestamps();
});
My File Model:
class File extends Model
{
protected $fillable = [
'filenames', 'project_id'
];
public function user()
{
return $this->belongsTo(User::class);
}
My Project Model:
public function files()
{
return $this->morphMany(File::class, 'fileable')->whereNull('files_id');
}
My Controller to store:
class FileController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'filenames' => 'required',
'project_id' => 'required',
// 'filenames.*' => 'mimes:doc,pdf,docx,zip'
]);
if($request->hasfile('filenames'))
{
foreach($request->file('filenames') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/files/', $name);
$data[] = $name;
}
}
$file= new File();
$file->filenames = $request->get('filenames');
$file->filenames= $name;
$file->user()->associate($request->user());
$project = Project::findOrFail($request->get('project_id'));
$project->files()->save($file);
$file->save();
return back();
}
public function download( $filename = '' ) {
// Check if file exists in storage directory
$file_path = public_path() . '/files/' . $filename;
if ( file_exists( $file_path ) ) {
// Send Download
return \Response::download( $file_path, $filename );
} else {
return back()->with('info', 'Archivo no existe en el servidor');
}
}
The Form in blade:
<form method="post" action="{{ route('file.store') }}" enctype="multipart/form-data">
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control">
<input type="hidden" name="project_id" value="{{ $project->id }}" />
<div class="input-group-btn">
<button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
Foreach to download files:
#foreach($project->files as $file)
<li>{{ $file->user->name }}: <a href="{{ url('/download/')}}/{{$file->filenames}}" download> {{$file->filenames}}</a></li>
#endforeach
I send files from Project Controll
The reason you are getting the first error message is because the Project with the id you get from Request is not found in the Database and returns null instead of an object. That would mean you are indeed calling files() method on null. To resolve this there are multiple steps.
1.) Make sure project_id is inside the Request at all times:
$this->validate($request, [
'filenames' => 'required',
'project_id' => 'required',
// 'filenames.*' => 'mimes:doc,pdf,docx,zip'
]);
2.) Make sure to check for project if it exists after retrieving it from database, this can be done in two ways.
a) You can either find the project or throw an Exception if it's not found:
$project = Project::findOrFail($request->get('project_id');`
b) You can check with a simple if statement if it does not exist and do something
$project = Project::find($request->get('project_id');
if (!$project) {
// Project not found in database
// Handle it
}
ERORR Undefined variable: listings
Users create a for sale advertising listing:
$listing->user_id = auth()->user()->id;
$listing->email= auth()->user()->email;
$listing->phone_number= auth()->user()->phone_number;
$listing->package = $request->session()->get('package');
$listing->save();
return view('user.dashboard');
After they get transferred to a dashboard page
#if($listings->isEmpty())
<div class="padding-top">
<h1 class="centre">None</h1>
</div>
#else
#endif
#foreach ($listings as $listing)
<div class="row">
<div class="container">
<div class="card">
<div class="card-body row">
<h5 class="card-title cardtitle col-lg-12 centre">{{$listing->address}}</h5>
incomplete html but just is an extract
EDIT ADDED FULL CONTROLLER Using return view('user.dashboard', ['listings' => $listing]); still throws an error
public function store(Request $request)
{
$this->validate($request, [
'image1' => 'image|nullable|max:1999',
'image2' => 'image|nullable|max:1999'
]);
// Handle File Upload
if($request->hasFile('image1')){
// Get filename with the extension
$filenameWithExt = $request->file('image1')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('image1')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('image1')->storeAs('public/cover_images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$listing = new Listings;
$listing->user_id = auth()->user()->id;
$listing->email= auth()->user()->email;
$listing->phone_number= auth()->user()->phone_number;
$listing->package = $request->session()->get('package');
$listing->save();
$listing->image1 = $fileNameToStore;
return view('user.dashboard', ['listings' => $listing]);
}
Change
return view('user.dashboard');
into
return view('user.dashboard', compact('listing'));
I think the reason is pretty clear.
You need to pass $listing to your view like this:
return view('user.dashboard', compact('listing');
Edit:
Of course your variables must be named the same in your controller and view. You now have a mismatch as you name your variable $listing in your controller but call it as $listings in your view. Your code should be like this:
$listings->user_id = auth()->user()->id;
$listings->email= auth()->user()->email;
$listings->phone_number= auth()->user()->phone_number;
$listings->package = $request->session()->get('package');
$listings->save();
return view('user.dashboard', compact('listings');
Then you can call the variable as $listings in your view, so your view code doess not have to change.
EDIT 2:
$listing is ONE instance of the Listings model. So, you cannot call the empty() method on the object. I think you want to do something like this:
$listing = new Listings;
$listing->user_id = auth()->user()->id;
$listing->email= auth()->user()->email;
$listing->phone_number= auth()->user()->phone_number;
$listing->package = $request->session()->get('package');
$listing->save();
$listing->image1 = $fileNameToStore;
$listings = Listings::all();
return view('user.dashboard', compact('listings'));
Does this work out for you?
Ps. Do you want to set the image1 attribute AFTER you save it? This will get lost.
I think you are not importing model in the controller using namespace.
try to check using "Use auth" namespace
use App\Listing_Model;
use auth;
class DashboardController extends Controller {
public function index() {
$listing = new Listing_Model;
$listing->user_id = auth()->user()->id;
$listing->email= auth()->user()->email;
$listing->phone_number= auth()->user()->phone_number;
$listing->package = $request->session()->get('package');
$listing->save();
return view('user.dashboard');
}
}
I try to use dropzoneJS in order to upload multiple image for my products and so far I can save images in database, also in images folder but I have problem with getting product id to relate each image to products.
Here is what I have:
Databases
Products (where my products including info will save)
Images (where my images including product id will save screenshot provided )
Models
Product:
public function images()
{
return $this->morphMany(Image::class, 'imageable');
}
Image:
class Image extends Model
{
protected $fillable = ['name'];
public function imageable()
{
return $this->morphTo();
}
public function product()
{
return $this->belongsTo(Product::class);
}
}
Image Schema
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->integer('imageable_id')->nullable();
$table->string('imageable_type')->nullable();
$table->string('name');
$table->timestamps();
});
}
ImageController
class ImageController extends Controller
{
public function dropzone()
{
return view('dropzone-view');
}
public function dropzoneStore(Request $request)
{
// works
$file = $request->file('file');
$filename = 'product' . '-' . time() . '.' . $file->getClientOriginalExtension();
$filePath = public_path('images/');
$request->file('file')->move($filePath, $filename);
return Image::create([
'name' => $filename,
'imageable_id' => $request->input('imageable_id'),
])->id;
}
}
Product Create (Blade)
// Form
{!! Form::open([ 'route' => [ 'dropzone.store' ], 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'dropzone mt-20', 'id' => 'my-awesome-dropzone' ]) !!}
<div class="fallback">
<input name="file" type="file" multiple />
</div>
<input type="hidden" name="imageIds[]" value="">
{{Form::close()}}
// Javascript
<script type="text/javascript">
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("form#my-awesome-dropzone", {
headers: {
"X-CSRF-TOKEN": $("meta[name='csrf-token']").attr("content")
},
acceptedFiles: ".jpeg,.jpg,.png,.gif",
dictDefaultMessage: "Drag an image here to upload, or click to select one",
maxFiles: 15, // Maximum Number of Files
maxFilesize: 8, // MB
addRemoveLinks: true,
});
myDropzone.on("success", function (response) {console.log(response.xhr.response); });
</script>
Any idea?
Code for controller:
class ImageController extends Controller
{
public function dropzone($id)
{
$product = Product::find($id);
return view('dropzone-view')
->withProduct($porduct);
}
public function dropzoneStore(Request $request)
{
// works
$file = $request->file('file');
$filename = 'product' . '-' . time() . '.' . $file->getClientOriginalExtension();
$filePath = public_path('images/');
$request->file('file')->move($filePath, $filename);
return Image::create([
'name' => $filename,
'imageable_id' => $request->input('imageable_id'),
])->id;
}
}
Code on blade view:
{!! Form::open([ 'route' => [ 'dropzone.store' ], 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'dropzone mt-20', 'id' => 'my-awesome-dropzone' ]) !!}
<div class="fallback">
<input name="imageable_id" type="hidden" value="{{$product->id}}" />
<input name="file" type="file" multiple />
</div>
{{Form::close()}}
Try this hope this should get you going. This is one of many way to make this thing work.
this is a morpic relation and this is how you get morphic relation
$post = App\Post::find(1);
foreach ($post->comments as $comment) {
//
}
read about it here polymorphic-relations
What i normally do is,
After you upload a picture, return the ID of the newly created image (you already do that in ImageController)
On your product page, in the dropzone 'success' callback you can read the the image ID and add it to an hidden array input field
In your controller you have to create the new product, and then after saving it , you can attach the images to the correct product, because now you have the id's of the images + the product instance has been made.
I'm trying to display an image stored in my Storage folder but it showing a broken link icon.
Here's the controller
`public function getUserImage($filename)
{
$file = Storage::disk('public')->get($filename);
return new Response($file, 200);
}`
Here is the img src in my view
<img src="{{ route('account.image', ['filename' => $user->name . '-' . $user->id . '.jpg']) }}" alt="" class="img-responsive">
Here is my route code
Route::get('/user/{filename}',[
'uses' => 'UserController#getUserImage',
'as' => 'account.image'
]);
You should send a proper 'Content-Type' header for the response in case of sending images. The code may look like:
public function getUserImage($filename)
{
$response = Response::make(Storage::disk('public')->get($filename));
$response->header('Content-Type', 'image/png');
return response;
}