im trying to download file using Laravel download response. The problem is probably the route is missing or went wrong, and it shows 404 Error Not Found. I'm using symlink on my storage file so it can be seen in public and it stores my files also.
Blade :
<div class="mb-3">
<label for="lampiran" class="form-label">Lampiran</label>
<div class="input-group mb-3">
<input type="text" class="form-control #error('lampiran') is-invalid #enderror"
value="{{ $dokumentasi->lampiran }}" readonly>
{{-- <span class="input-group-text"><i class="fa fa-download" aria-hidden="true"></i></span> --}}
<span class="input-group-text"><i class="fa fa-download" aria-hidden="true"></i></span>
</div>
</div>
Controller :
public function download($file_name)
{
$file_path = base_path($file_name);
return response()->download($file_path);
}
Route / web.php :
Route::get('/lampiran-nde/{file}', 'download')->name('history.download');
Related
I want to preload an image from my laravel project . from "public/storage/uploads/users" to my file input feild so that the user wont have to pick an image again if he/she dont need to update the image feild . I used url(0 and asset() to specify the url in the "value=" feild but its not working please help.
Below is my code i wrote but its not working
<div class="form-group">
<label class="control-label">Image <span class="text-danger"> *</span></label>
<input type="file" class="form-control #if ($errors->has('image')) is-invalid #endif" name="image" value="{{url('storage/' . $user->image)}}"
placeholder="Enter Image" required>
<img src="{{ asset('storage/' . $user->image) }}" class="img-thumbnail mt-2" width="100" alt="">
<div class="invalid-feedback">{{ $errors->first('image') }}</div>
</div>
i'm trying to upload some files via form to my db and also in the storage of my project
I did the following code on my homepage :
<x-layout>
#if (session('message'))
<div class="alert alert-success">{{session('message')}}</div>
#endif
<div class="container vh-100">
<div class="row h-100 w-100 align-items-center">
<div class="offset-3 col-6">
<form method="POST" action="{{route('transfer.submit')}}" class="card" enctype="multipart/form-data">
#csrf
<div class="border w-100" id="fileWrapper">
<div class="mb-3 w-100 h-100">
<input type="file" class="form-control w-100 h-100 fileInput" id="fileupload" name="files[]" multiple >
</div>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Invia file a </label>
<input type="email" class="form-control" id="exampleInputPassword1" name="recipient_mail">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">La tua mail</label>
<input type="email" class="form-control" id="exampleInputPassword1" name="sender_mail">
</div>
<div class="mb-3">
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="mb-3">
<textarea name="message" cols="50" rows="10"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</x-layout>
Then i done the following in my model :
protected $fillable = [
'recipient_mail',
'sender_mail',
'title',
'message',
'files[]'
];
and the following in my controller :
public function transferSubmit(TransferRequest $request){
$transfer = Transfer::create([
'sender_mail'=>$request->input('sender_mail'),
'recipient_mail'=>$request->input('recipient_mail'),
'title'=>$request->input('title'),
'message'=>$request->input('message'),
'files'=>$request->file('files[]')->store('public/files'),
]);
return redirect(route('home'))->with('message', 'File inviato con successo');
}
I havo also created the POST route and completed the migrations but, when i try to submit the form i get the following error :
Error Call to a member function store() on null
After this i tried the dd($request) ro check the data that i was actually passing to the Trasnfer class and i found that it is receiving correctly every data including the array of files.
Is there anybody that can help me to understand why i'm getting that error?
Thank you so much
You want store multiple files. And you will get an array. Then you have to iteratrate over your file array like that.
$files = [];
if($request->hasfile('files[]'))
{
foreach($request->file('files[]') as $file)
{
$files => $file->store('public/files'),
}
}
Important Note:
And don't forget the symlink before working with the Laravel storage.
php artisan storage:link
Updated
You iterate first then you have the file array which contains the paths to the images. you can then pass that to your model.
A little note: data coming from a form should always be validated.
public function transferSubmit(TransferRequest $request){
$files = [];
if($request->hasfile('files[]'))
{
foreach($request->file('files[]') as $file)
{
$files => $file->store('public/files'),
}
}
$transfer = Transfer::create([
'sender_mail'=>$request->input('sender_mail'),
'recipient_mail'=>$request->input('recipient_mail'),
'title'=>$request->input('title'),
'message'=>$request->input('message'),
'files'=> $files;
return redirect(route('home'))->with('message', 'File inviato con successo');
}
I have a basic CRUD structure for a model that works well. Now I want to redirect to the create method and populate the form with a pre-existing model. It would work by a user selecting an id and then I would select that model and redirect to the create page and populate the form.
This is what I have tried so far
$order = Orders::find($id);
$inventory = Inventory::where(['id' => $order->inventory_id])->first()->toArray();
return redirect()->route('backend.inventory.create', $inventory['bag_id'])->withInput($inventory);
In the above example, it finds the order and selects the single inventory item related to it (I have confirmed that it's definitely selecting an inventory item as expected) and redirects to the inventory creation page. However, when using the ->withInput() method this doesn't seem to populate my form with the data as I expected.
How do I pass data to a form using a redirect?
Adding the form code as requested below. This is just one column of the form as its a huge block of code
<div class="form-group row" id="item-name" v-if="type != '3'">
<label for="name" class="col-md-2 col-form-label text-md-right">Item Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required>
#if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
You can use this :
return Redirect::back()->withInput(Input::all());
If you're using Form Request Validation, this is exactly how Laravel will redirect you back with errors and the given input.
Excerpt from \Illuminate\Foundation\Validation\ValidatesRequests:
return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());
try this
<div class="form-group row" id="item-name" v-if="type != '3'">
<label for="name" class="col-md-2 col-form-label text-md-right">Item Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') ?? $inventory['name'] }}" required>
#if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
{{ old('name') ?? ($inventory['name'] ?? '') }} if old data is present it will show old data otherwise $inventory->name which is coming from withInput with is also optional that's why i put ??
ref link https://laravel.com/docs/6.x/requests#old-input
You can use compact() function that creates array containing variables and their values.
$order = Orders::find($id);
$inventory = Inventory::where(['id' => $order->inventory_id])->first()->toArray();
return view('backend.inventory.create', compact('inventory'));
In your view page you can use it like,
<input id="name" type="text" class="form-control" name="name" value="{{ $inventory['name'] }}" required>
I had a laravel system built and trying to figure out how multiple clients can login using a single URL such as www.example.com/login
As the moment each user must have a unique url like below:
www.example.com/biz1/admin/login
www.example.com/biz2/admin/login
From what i can tell the code is getting the biz name from the url and adding it into the form, please see form below:
<form action="{{ route('business.admin.authenticate', ['bzname' => $bzname]) }}" method="post" >
{{ csrf_field() }}
<div class="form-group">
<label style="color: #fff" class="label">Email</label>
<div class="input-group">
<input style="color: #fff" type="text" class="form-control" placeholder="Email" name="email">
<div class="input-group-append">
<span class="input-group-text">
<i class="mdi mdi-check-circle-outline"></i>
</span>
</div>
</div>
</div>
<div class="form-group">
<label style="color: #fff" class="label">Password</label>
<div class="input-group">
<input style="color: #fff" type="password" class="form-control" placeholder="*********" name="password">
<div class="input-group-append">
<span class="input-group-text">
<i class="mdi mdi-check-circle-outline"></i>
</span>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary submit-btn btn-block">Login</button>
</div>
</form>
This form then pasts to the function in the controller which is below:
public function authenticate(Request $request, $bzname)
{
$business = Business::where('business_name', $bzname)->first();
$business_admin = BusinessAdmin::where('business_id', $business->id)->first();
if(isset($business_admin))
{
$user = User::findOrFail($business_admin->user_id);
if($user->email === $request->input('email'))
{
if (Auth::guard('business')->attempt(['email'=>$request->input('email'), 'password'=>$request->input('password')]))
{
$permissions = [];
session(['permissions' => $permissions]);
return redirect()->route('business.admin.dashboard', ['bzname' => $bzname]);
}
else
{
return redirect()->route('business.admin.login', ['bzname' => $bzname])->with('errorLogin', 'Ooops! Invalid Email or Password')->withInput();
}
}
else
{
return redirect()->route('business.admin.login', ['bzname' => $bzname])->with('errorLogin', 'Ooops! Pls Enter Correct Business')->withInput();
}
}
}
I'm thinking somehow in the $business parameter i need to get the bzname from the email linked to the client instead of getting it from the URL.
My knowledge is very basic at the moment and any advice is appreciated.
Thanks
I am trying to submit one form and login one user and it seems to work fine in localhost, but when I upload this code onto a live server then the Token is not working.
I have checked that the CSRF token and session token value is not same.
Also the post request is not working so I have added the route in except VerifyCsrfToken now the post request is working but after login the user session is not working
if (Session::token() != $request->get('_token'))
{
echo 'Not Match';exit;
}
Above the result shows me no match .
I have regenerated the key on the server, also I had provided the permission to storage folder.
Cache data has been cleared but the authectioncation is not working it doesn't work.
<form class="login-form js-login-frm" role="form" method="POST" action="{{ route('admin.login-post') }}">
{{ csrf_field() }}
<div class="form-body">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<div class="input-group" title="{{__('username_or_email_address')}}">
<span class="input-group-addon"><i class="fa fa-envelope-o"></i></span>
<input class="form-control" autofocus type="text" id="email_address" placeholder="{{ __('username_or_email_address') }}" name="login"/>
</div>
</div>
</div>
<div class="col-xs-12">
<div class="form-group">
<div class="input-group" title="{{__('enter_password')}}">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input class="form-control" id="password" type="password" placeholder="{{ __('enter_password') }}" name="password"/>
</div>
</div>
</div>
</div>
</div>
<div class="form-actions">
<button class="btn-del btn-5 btn-5a fa fa-lock login-btn" type="submit" id="login_btn">
<span>{{ __('login') }}</span>
</button>
</div>
</form>
In My controller
$remember = $request->remember ? true : false;
$auth = Auth::guard('web')->attempt([
'email' => $request->login,
'password' => $request->password,
'is_active' => 1
], $remember);
This code is working fine but the user doesn't get redirected to the dashboard page it redirect to login page again.
Can anyone help with the login the user in the system, and redirect to proper route.
In a test environment the CSRF token is not checked in the middleware. Replace {{ csrf_field() }} with #csrf and you should be fine. Have a look at the source code of the generated login page.