storage’s server IP address could not be found - php

in Laravel 8 i was tried to show pdf file uploaded in storage folder,
and i got storage’s server IP address could not be found.
i want to uploade pdf file and show it on browser without download it
i used controlle function store to upload and show function to show pdf file
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\App;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Pdf;
class PdfController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
protected function authenticated(Request $request, $user)
{
if ( $user->isAdmin() ) {// do your magic here
return redirect()->route('pdfs');
}
return redirect('/home');
}
public function __construct()
{
$this->middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified'
])->except(['show']);
}
public function index()
{
$pdfs = Pdf::latest()->paginate(5);
return view('pdfs.index',compact('pdfs'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('pdfs.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'file' => 'required|mimes:pdf|max:2048',
]);
$fileModel = new pdf();
$pdf = $request->file;
$fileName = time() .'.pdf';
$filePath = $pdf->storeAs('uploads', $fileName, 'public');
$fileModel->name = $fileName;
$fileModel->file ='/storage/' . $filePath;
$fileModel->save();
return redirect()->route('pdfs.index')
->with('success','File has been uploaded.')
->with('pdf', $fileName);
}
/**
* Display the specified resource.
*
* #param \App\Models\Pdf $pdf
* #return \Illuminate\Http\Response
*/
public function show($ip)
{
$fileModel = Pdf::find($ip);
return view('pdfs.show', compact('fileModel'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Pdf $pdf
* #return \Illuminate\Http\Response
*/
public function edit(Pdf $pdf)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Pdf $pdf
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Pdf $pdf)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Pdf $pdf
* #return \Illuminate\Http\Response
*/
public function destroy(Pdf $pdf)
{
$pdf->delete();
return redirect()->route('pdfs.index')
->with('success','PDF deleted successfully');
}
}
this is my controller
#extends('pdfs.layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show PDF</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('pdfs.index') }}"> Back</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $fileModel->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<iframe src="/{{ $fileModel->file }}" frameborder="0"></iframe>
</div>
</div>
</div>
#endsection
and this is show.blade.php

Related

Create view in one controller and store data in other controller laravel

I'm new to Laravel and I'm trying to store a form. I created the view with the House controller but now I want to store the data in the view with the Booking controller. But when I click the button nothing happens.
My question is if it is possible to make a view with one controller and store it with another controller or maybe there is an other solution.
I also want to use the id of the house to store. How do I get that in the other controller as well?
Web Route
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', [\App\Http\Controllers\HouseController::class, 'index']);
Route::get('house/{house}', [\App\Http\Controllers\HouseController::class, 'show']);
Route::post('house/{house}', [\App\Http\Controllers\BookingController::class, 'store']);
Route::get('rental', [\App\Http\Controllers\HouseController::class, 'getUserHouses']);
Route::get('rental/new', [\App\Http\Controllers\HouseController::class, 'create']);
Route::post('rental/new', [\App\Http\Controllers\HouseController::class, 'store']);
Route::get('rental/edit/{house}', [\App\Http\Controllers\HouseController::class, 'edit']);
Route::put('rental/edit/{house}', [\App\Http\Controllers\HouseController::class, 'update']);
Auth::routes();
Booking controller
<?php
namespace App\Http\Controllers;
use App\Models\Booking;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BookingController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$newBooking = Booking::create([
'user_id' => Auth::id(),
'house_id' => $request->id,
'begin' => $request->begin,
'end' => $request->end,
'status' => 0
]);
return redirect('/');
}
/**
* Display the specified resource.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function show(Booking $booking)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function edit(Booking $booking)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Booking $booking)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function destroy(Booking $booking)
{
//
}
}
House controller
<?php
namespace App\Http\Controllers;
use App\Models\house;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Helper\Imageable;
use DB;
class HouseController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$houses = House::all();
return view('/home', [
'houses' => $houses
]);
}
/**
* Display a listing of the houses the owner has
*
* #return \Illuminate\Http\Response
*/
public function getUserHouses()
{
$houses = DB::table('houses')
->where('user_id', '=', Auth::id())
->get();
return view('/rental/rental', [
'houses' => $houses
]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('rental/new');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$path = Imageable::storeMedia($request);
$request->online === 'on' ? $online = 1 : $online = 0;
$newHouse = House::create([
'title' => $request->title,
'price_per_night' => $request->price,
'summary' => $request->summary,
'place' => $request->place,
'country' => $request->country,
'user_id' => Auth::id(),
'online' => $online,
'image' => $path,
]);
return redirect('rental');
}
/**
* Display the specified resource.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function show(house $house)
{
return view(
'/house',
[
'house' => $house
]
);
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function edit(house $house)
{
return view(
'rental/edit',
[
'house' => $house
]
);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function update(Request $request, house $house)
{
$path = Imageable::storeMedia($request);
$request->online === 'on' ? $online = 1 : $online = 0;
$house->update([
'title' => $request->title,
'price_per_night' => $request->price,
'summary' => $request->summary,
'place' => $request->place,
'country' => $request->country,
'online' => $online,
'image' => $path,
]);
return redirect('rental/edit/' . $house->id);
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function destroy(house $house)
{
//
}
}
View
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-12">
<h1 class="display-one ">{{ $house->title }}</h1>
<p class=".text-light">{{ $house->place }}, {{ $house->country }}</p>
</div>
</div>
<div class="row mt-5">
<div class="col-sm-6">
<img src="{{ asset("img/houses/$house->image") }}" alt="{{ $house->title }}" class="img-fluid" />
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="exampleFormControlSelect1">Kies een datum en reserveer direct</label>
<form method="POST" action="">
#csrf
<input type="date" name="begin">
<input type="date" name="end">
<div class="col-md-12 bg-light mt-3">
<button type="button" class="btn btn-warning ml-2">Vraag aan</button>
</div>
</form>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-sm-6">
<p class="display-one ">{{ $house->summary }}</p>
</div>
<div class="col-sm-6">
<h2 class="display-one ">Aangeboden door</h2>
<p>Prijs per nacht €{{ $house->price_per_night }}</p>
</div>
</div>
</div>
#endsection
First of all, nothing happens when you click the form submit button because it is currently type="button" and in order this button to play role of submission button it must be type="submit". You can do whatever you want with Laravel. If you want your form to hit another controller method you can simply specify that in your form tag. Like so:
Imagine this is a form inside a view that is rendered by HouseController
<form method="POST" action="{{ url('/save/from/booking/controller') }}">
// ....
</form>
And now on form submission inside a view that is rendered by HouseController, you will actually hit a route that is BookingController responsive for. And here is your route that is being hit by the form
Route::post('/save/from/booking/controller', [BookingController::class, 'store']);

Laravel File Uploading | Fetching Files from MySQL Database to view in laravel

I do not now how to show my files from laravel. the database contains path for the files and now I need to fetch my files and display them in laravel.
files.blade.php
#extends('dashboard.layouts.dashboard-layout')
#push('css')
#endpush
#section('content')
<div class="d-flex vw-100 vh-100 justify-content-center align-items-center">
<img src="storage/{{$savedfile->filename}}" alt="">
</div>
<div class="d-flex vw-100 vh-100 justify-content-center align-items-center">
<form method="POST" enctype="multipart/form-data" action="{{route('dashboard.files')}}">
#csrf
<div class="form-group">
<label for="exampleFormControlFile1">Example file input</label>
<input type="file" class="form-control-file" name="uploadedfile" id="exampleFormControlFile1">
</div>
<div class="form-group"><button class="btn btn-success">Upload the file</button></div>
</form>
</div>
#endsection
#push('script')
#endpush
The <img src="storage/{{$savedfile->filename}}" alt="" /> is the file needs to be shown
Fileupload Controller
<?php
namespace App\Http\Controllers;
use App\Models\Fileupload;
use Illuminate\Http\Request;
class FileuploadController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function files(Request $request)
{
$file = $request->file('uploadedfile');
$filename = $file->getClientOriginalName();
$filename = time(). '.' .$filename;
$path = $file->storeAs('public/docs', $filename);
Fileupload::create(['filename' => $path]);
return view('dashboard.files')->with('savedfile', $savedfile);
}
/**
* Display the specified resource.
*
* #param \App\Models\Fileupload $fileupload
* #return \Illuminate\Http\Response
*/
public function show(Fileupload $fileupload)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Fileupload $fileupload
* #return \Illuminate\Http\Response
*/
public function edit(Fileupload $fileupload)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Fileupload $fileupload
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Fileupload $fileupload)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Fileupload $fileupload
* #return \Illuminate\Http\Response
*/
public function destroy(Fileupload $fileupload)
{
//
}
}
This is the controller for posting and returning the files.
The return view('dashboard.files')->with('savedfile', $savedfile) is the code to return and view my files.
However...
This is the error I'm getting
Error Image
I do not know why but its stressing already
The error is clear.$savedFile is undefined .so you have change it like below
public function files(Request $request)
{
$file = $request->file('uploadedfile');
$filename = $file->getClientOriginalName();
$filename = time(). '.' .$filename;
$file->storeAs('public/docs', $filename);
Fileupload::create(['filename' => $path]);
$path ="docs/".$filename;
return view('dashboard.files')->with('savedfile', $path);
}
or
public function files(Request $request)
{
$file = $request->file('uploadedfile');
$filename = $file->getClientOriginalName();
$filename = time(). '.' .$filename;
$file->storeAs('public/docs', $filename);
Fileupload::create(['filename' => $path]);
$path = \Illuminate\Support\Facades\Storage::url("docs/".$filename)
return view('dashboard.files')->with('savedfile', $path);
}
so in your view
<img src="{{asset($savedfile)}}" />
Updated
Also better way is to create separate file system for doc like below
In config/filesystem.php
'docs' => [
'driver' => 'local',
'root' => storage_path('app/public/docs'),
'url' => env('APP_URL').'/storage/docs',
'visibility' => 'public',
],
and in your controller
$path=Storage::disk('docs')->url($filename);
so in your blade file
<img src="{{$savedfile}}" />

how to get information from another controller in laravel

I would like to save information using another controller to store it in the database.
in my situation, there are two controllers:
PostingsController
CommentsController
I could make the create function for comment using CommentsController, but I could not save post_id in the database from CommentsController.
I would like to save post_id using PostingsController because it has:
//if user did not fill the form properly
//error messages will popup
$this->validate($request, [
'comment' => 'required',
]);
//if user filled the form properly
//store the form
$comment = new Comment();
//for connecting both user and comment
$comment->user_id = Auth::id();
//to store comment
$comment->post_id = //I want to save post_id from PostingsController
//to store comment
$comment->comment = $request->input('comment');
//save all user input
$comment->save();
dd($comment);
I have set $comment = Posting::id();, but it could not work. the error is:
BadMethodCallException
Call to undefined method App\Models\Posting::id()
my PostingsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Posting;
use App\User;
use Illuminate\Support\Facades\Auth;
class PostingsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//this is how to get all posting, but this shows only all posting
// $postings = Posting::all();
// $postings->load('user');
//this is how to get all posting with created_at and descending order.
$postings = Posting::orderBy('created_at', 'desc')->get();
//should be function name
$postings->load('user');
return view('index')->with('postings', $postings);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//if user did not fill the form properly
//error messages will popup
$this->validate($request, [
'posting' => 'required',
]);
//if user filled the form properly
//store the form
$posting = new Posting();
//for connecting both user and posting
$posting->user_id = Auth::id();
//to store posting
$posting->post = $request->input('posting');
//save all user input
$posting->save();
return redirect()->to('/home')->with('success', 'Posting created successfully!');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$posting = Posting::find($id);
return view('show')->with('posting', $posting);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//this is the way how to find posting of authenticated user
$posting = Posting::find($id);
return view('edit')->with('posting', $posting);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//if user did not fill the form properly
//error messages will popup
$this->validate($request, [
'posting' => 'required',
]);
//if user filled the form properly
//store the form
$posting = Posting::find($id);
//to store posting
$posting->post = $request->input('posting');
//save all user input
$posting->save();
return redirect()->to('/home')->with('success', 'Posting edited successfully!');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$posting = Posting::find($id);
$posting->delete();
return redirect()->to('/home')->with('success', 'Posting deleted successfully');
}
}
my CommentsController:
<?php
namespace App\Http\Controllers;
use App\Models\Posting;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CommentsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('comments.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//if user did not fill the form properly
//error messages will popup
$this->validate($request, [
'comment' => 'required',
]);
//if user filled the form properly
//store the form
$comment = new Comment();
//for connecting both user and comment
$comment->user_id = Auth::id();
//to store comment
$comment->post_id = //problem
//to store comment
$comment->comment = $request->input('comment');
//save all user input
$comment->save();
dd($comment);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
create.comments in view:
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<div class="float-left m-2">Create new comment</div>
<span class=" ">Go back</span>
</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<form method="post" action="/comments" >
#csrf
<input type="hidden" name="post_id" id="post_id" value="{{ $post->id }}" />
<div class="form-group">
<input type="text" class="form-control" name="comment" id="comment" placeholder="Comment what you think!">
</div>
<button type="submit" class="btn btn-primary float-right">Comment</button>
</form>
</div>
</div>
</div>
</div>
#endsection
Following the principle of Separation of Concerns you can move the logic into another module such as services.
I would suggest adding a new folder app/services and adding a service-class for each logic that needs to be implemented, eg. app/services/AddCommentToPostService.php.
This will also make testing of the system easier.
you can use app method to call controller method of another controller in laravel app('App\Http\Controllers\ControllerName')->functionName();

How to autofill fields in laravel?

I am trying to make the password of the user the same as the user id. What i don't want is to copy and paste the user id from the user id field into the password field. Instead, what i want to achieve is that as soon as i enter the user id, the same values get input into the password field. how can i achieve this? Below is my code:
Blade :
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-7">
<div class="card">
<div class="card-header">Add New User</div>
<div class="card-body">
{!! Form::open(['action'=>'AccountsController#store', 'method'=>'POST']) !!}
<div class="row form-group justify-content-center">
{{Form::label('name','Name',['class'=>'col-md-2'])}}
<div class="col-md-4">
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Name'])}}
</div>
</div>
<div class="row form-group justify-content-center">
{{Form::label('email','Email',['class'=>'col-md-2'])}}
<div class="col-md-4">
{{Form::text('email','',['class'=>'form-control', 'placeholder'=>'Email'])}}
</div>
</div>
<div class="row form-group justify-content-center">
{{Form::label('userid','User I.D',['class'=>'col-md-2'])}}
<div class="col-md-4">
{{Form::text('userid','',['class'=>'form-control', 'placeholder'=>'User I.D'])}}
</div>
</div>
<div class="row form-group justify-content-center">
{{Form::label('password','Password',['class'=>'col-md-2'])}}
<div class="col-md-4">
{{Form::password('password',['class'=>'form-control', 'placeholder'=>'Password'])}}
</div>
</div>
<div class="row form-group justify-content-center">
{{Form::label('cpassword','Confirm Password',['class'=>'col-md-2'])}}
<div class="col-md-4">
{{Form::password('cpassword',['class'=>'form-control', 'placeholder'=>'Confrim Password'])}}
</div>
</div>
<div class="row form-group justify-content-center">
{{Form::submit('Submit',['class'=>'btn btn-primary'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
#endsection
controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Account;
use App\User;
class AccountsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// $accounts = Account::all();
$accounts = User::all();
return view('accounts.create');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('accounts.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required',
'email'=>'required',
'userid'=>'required',
'password'=>'required'
]);
// $account = new Account;
$account = new User;
$account->name = $request->input('name');
$account->email = $request->input('email');
$account->userid= $request->input('userid');
$account->password = bcrypt($request->input('password'));
$account->save();
return redirect('/accounts')->with('success', 'User has been successfully added!');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
model :
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email','userid', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
database :
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('userid')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
To achive such a dynamic operation you need JavaScript and I suggest to use JQuery for this. Here you can read more about it.
So first add JQuery to your project as described here.
Then use this code to achieve your goal:
<script type="text/javascript">
$(document).ready( function() {
$('#userid').on('change', function() {
$('#password').val($(this).val());
});
});
</script>
The code will listen for changes of the #userid element and then insert the value of it into the #password element.
The code needs to be on the same page as your form is.
If you're using jquery for further stuff I suggest to add those scripts by using the stacks mechanism of blade. It's described here. In my opinion this would be a bit better than entering the code directly in your view.

Method App\Http\Controllers\Elibrary::save does not exist

I try to make library of pdf files. which i want to store pdf title-name and file name also upload this pdf in project storage. but server show me this error.I can't understand what can I do.
Method App\Http\Controllers\Elibrary::save does not exist.
my error message
this is my elibrary controller file which i chech filename and store file name in database also stored in public/images location
I find this code on this linkuload file tutorial
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Elibrary;
class ElibraryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request){
$elibrary = Elibrary::orderBy('id','DESC')->paginate(5);
return view('e-library',compact('elibrary'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'efile' => 'required|max:4000',
]);
if($file= $request->file('file')){
$name = $file->getClientOriginalName();
if($file->move('images', $name)){
$elibrary = new Post;
$elibrary->efile = $name;
$elibrary->save();
return redirect()->route('e-library');
};
}
$elibrary = new Elibrary([
'title' => $request->get('title'),
'efile' => $request->file('file'),
]);
$elibrary->save();
return redirect()->route('e-library');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
This is my route file code
Route::post('/store', 'Elibrary#store')->name('store');
this is e-library.blade.php file from
<form action="/store" method="post" enctype="multipart/form-data">
#csrf()
<div class="form-group">
<input type="text" class="form-control"name="title" placeholder="Name">
</div>
<div class="form-group">
<input type="file" class="form-control"name="efile" >
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-send-message" >
</div>
</form>
this is my model file of Elibrary.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Elibrary extends Model
{
public $fillable = ['title','efile'];
}
this is my migration file
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateElibrariesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('elibraries', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('efile');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('elibraries');
}
}
How i can show this pdf with the help show function in show.blade.php
You're creating new instances of Elibrary in your controller methods. Elibrary is a controller class but it looks like you're treating it as a model.
Maybe try changing all of your new Elibrary() to new Post since it looks like that might be what you're trying to accomplish.
If thats the case, you will also need to make efile fillable in your Post model.
$elibrary = Post::orderBy('id','DESC')->paginate(5);

Categories