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

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);

Related

Post array values in mysql database in laravel

I'm developing rest api in laravel, here im trying to post array data in database, but i could not figure out how to do it, can some one guide me in this ? i'm new to laravel
ProductdetaisController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\productdetails;
class ProductdetailsController extends Controller{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return productdetails::all();
}
/**
* 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)
{
$request->validate([
'productname' => 'required',
'productid' => 'required',
'productdescription' => 'required',
'productimage' => 'required',
'productinvoice' => 'required',
]);
return productdetails::create($request->all());
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
return productdetails::find($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)
{
$productdetails = productdetails::find($id);
return $productdetails->update($request->all());
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
return productdetails::find($id)->delete();
}
}
productdetails.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class productdetails extends Model
{
use HasFactory;
protected $fillable = ['productname', 'productid', 'productdescription', 'productimage', 'productinvoice'];
}
2021_09_25_075455_create_productdetails_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductdetailsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('productdetails', function (Blueprint $table) {
$table->id();
$table->string('productname');
$table->string('productid');
$table->string('productdescription');
$table->array('productinvoice'); <----- here i want to store array of data ------>
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('productdetails');
}
}
example for array of data
[{productquantity: '5', productprice: '5', productgst: '5', productname: 'xyz'}, {productquantity: '5', productprice: '5', productgst: '5', productname: 'ABC'}]
How to post such kind of above data in the data base ?
Model::create() doesn't work like that. You should loop over the array and create rows individually.
Laravel docs on this method
Also, your validation won't work. Docs on array validation
Try this: (save it to database)
$productinvoice =json_encode( $request->your_array_json_data );
...save it to database.
When you want to use It inside client side You should parse It to array of json data.
Like this: (use it inside client side part)
JSON.parse($productinvoice)
When You want to use it inside backend part after retrieve it from database.
This is how You could convert it to php array : (use it inside backend part)
$productinvoice = json_decode($productionDetails->productinvoice, true);

Laravel 8 Jetstream Inertia All Inertia requests must receive a valid Inertia response

I've been trying to find the correct answer for my problem I know that the question has been asked before here and actually many other places, but no answers I found could suit my problem.
The vue component
FooterNewsletter.vue
<template>
<div class="lg:w-2/4 md:w-1/2 w-full px-8 border-l-2">
<p class="font-bold text-3xl">
Don't want to miss the latest cryptocurrency news?
</p>
<p class="py-3 text-lg">
Get the latest news and updates by subscribing to our free
newsletter 📰
</p>
<form #submit.prevent="subscribeToNewsletter">
<div class="flex flex-col">
<div class="flex">
<input
v-model="form.email"
type="text"
name="post_title"
class="
border-primary
focus:border-primary
focus:ring-offset-transparent
focus:ring-transparent
"
id="exampleFormControlInput1"
placeholder="Your E-mail"
/>
<input
type="submit"
value="Subscribe"
:disabled="form.processing"
class="px-5 py-2 bg-primary text-white cursor-pointer"
/>
</div>
</div>
</form>
</div>
</template>
<script>
export default {
props: [],
components: {
},
data() {
return {
form: this.$inertia.form({
email: "",
}),
};
},
methods: {
subscribeToNewsletter() {
this.form
.transform((data) => ({
...data,
// remember: this.form.remember ? "on" : "",
}))
.post(this.route("newsletter.store"), {
onSuccess: (data) => {
console.log("data", data);
},
onError: (data) => {
console.log("data", data);
},
});
},
},
};
</script>
The controller
NewsletterController.php
namespace App\Http\Controllers;
use App\Http\Requests\NewsletterStoreRequest;
use App\Models\Newsletter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class NewsletterController 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(NewsletterStoreRequest $request)
{
return response()->json([
'message' => 'suss',
]);
}
/**
* 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)
{
//
}
}
The request file
NewsletterStoreRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class NewsletterStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'email' => ['required', 'max:150', 'email'],
];
}
}
The Route
web.php
Route::post('/newsletter', [
NewsletterController::class,
'store',
])->name('newsletter.store');
In the sample above i return a json object, which is not accepted and gives me this error
I have tried following the inertia documentation about responses here I think the correct answer is to be found there, I have not been able to solve it on my own
I have also looked into inertia Jetstream docs without any luck here
I don't want to return a new view, I actually want it to work as an ajax request I guess where a call-back is either giving me the error or success without reloading the page or anything like that.
I see that many people have this problem, does anyone know whats going wrong and what i have to return in the controller?
For me i had to change the controller to this:
namespace App\Http\Controllers;
use Inertia\Inertia;
use App\Models\Newsletter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests\NewsletterStoreRequest;
class NewsletterController 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(NewsletterStoreRequest $request)
{
Newsletter::create([
'email' => $request->email
]);
return back()->with('flash', [
'message' => 'success',
]);
}
/**
* 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)
{
//
}
}
What you should note here is this part:
return back()->with('flash', [
'message' => 'success',
]);
I found a sample where my problem was achieved with the help from a friend of mine in: vendor/laravel/jetstream/src/Http/Controllers/Inertia/ApiTokenController.php
Here they return with a flash message like my example above, the message key will be in props->components->flash->message
It won't work as I thought like ajax, because it uses Vue routes so the page will reload on submit.
I hope someone will find this useful.
Also if you get a console error because you use data in a vue component you have to make a check on the wrapper to check if the data is null like so:
<ul
v-if="assets != null"
class="
marketcap-rows
border-l border-r border-gray-200
flex flex-col
"
>
<li
v-for="asset in $props.assets.data"
v-bind:key="asset"
class="
bg-white
grid
gap-4
border-t border-gray-200
"
>
</li>
</ul>
This is the check I'm talking about: v-if="assets != null"
This is how to access jetstream initial default flash message:
session()->flash('flash', [
'bannerStyle'=> 'danger',
'banner' => 'this is the first message',
]);
return Inertia::render('Admin/Overview', [
'users'
]);
You need to add that to your Render...
doc: https://inertiajs.com/shared-data#flash-messages
class HandleInertiaRequests extends Middleware
{
public function share(Request $request)
{
return array_merge(parent::share($request), [
'flash' => [
'message' => fn () => $request->session()->get('message')
],
]);
}
}

PHPUnit always takes me to login page,. regardless of what I do

So I am having a bit of an interesting problem. I am trying to build a forum for my website. I am trying to create a test that ensures registered users can submit new forum threads. Now, regardless of what I do, the test always ends up at the login paghe, even though I am logged in.
Here is something interesting, however, when I attempt to dd() the thread immediately after creation, Laravel seems to "skip over" the dd command and I end up with the "Thread title not found on the login page" error. I can dd() the $request object and that prints out fine, however, it seems after creating a new Thread model does Laravel skip over the dd($thread) command and I end up getting the login page once again.
I am pulling my hair out. Why is this happening?
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
use App\Thread;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class CreateThreadsTest extends TestCase
{
use DatabaseMigrations;
function test_an_authenticated_user_can_create_new_forum_threads() {
$this->be(factory('App\User')->create());
$thread = factory('App\Thread')->make();
$this->post('forum/threads', $thread->toArray());
// you are missing this line
$this->get($thread->path())->assertSee($thread->title)->assertSee($thread->body);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Thread;
class ThreadsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
}
/**
* 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)
{
$thread = Thread::create([
'user_id' => auth()->id,
'title' => $request->title,
'body' => $request->body
]);
return redirect($thread->path());
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(Thread $thread)
{
return view('threads.show', compact('thread'));
}
/**
* 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)
{
//
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model
{
protected $guarded = [];
public function path() {
return "/forum/threads/" . $this->id;
}
public function replies() {
return $this->hasMany(Reply::class);
}
public function creator()
{
return $this->belongsTo(User::class, 'user_id');
}
public function addReply($reply) {
$this->replies()->create($reply);
}
}
When you make a new model using the factory helper, it doesn't have an ID
for example
So $thread->path() will return just "/forum/threads/" which is not what you want
You have to make the request to the location from the response or query the newly created Thread record
public function test_users_can_create_statuses()
{
$this->be(factory('App\User')->create());
$thread = factory('App\Thread')->make();
$response = $this->post('/forum/threads', $thread->toArray());
$this->get($response->headers->get('Location'))
->assertSee($thread->title);
->assertSee($thread->body);
}

How to Insert data through multiple forms to multiple tables in laravel

Hi i am new to laravel i have 3 forms ie customer ,service,contact all have different tables like customer ,service,contact i need to do insert for these in database in laravel.
Service Form:
<form method="POST" action="{{ route('service.store') }}">
#csrf
<div class="input-w">
<label class="label-style">Service Type:</label>
<span><select class="form-control" required >
<option type="brillare">Brillare</option>
<option type="hair">Hair</option>
</select></span>
</div>
<div class="input-w">
<label name="service_name">Service Name:</label>
<span><input id="service_name" name="service_name" class="form-control" placeholder="Service Name" required ></span>
</div>
<div class="input-w">
<label name="service_price">Service Price:</label>
<span><input type="number" id="service_price" name="service_price" class="form-control" required placeholder="0000.00"></span>
</div>
<div align="center" class="button_style">
<p style="margin:15px -40px 20px 182px"><input type="submit" name="submit" id="submit_service" value="Submit" class="btn btn-outline-success"></p>
</div>
</form>
Migration:
Route::get('/service', function () {
return view('service');
});
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ServiceTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('service', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('servicetype');
$table->string('servicename');
$table->string('serviceprice');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('service');
}
}
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ServiceController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
return view("service");
}
/**
* 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, Service $service)
{
//
$service->servicetype = $request->service_type;
$service->servicename = $request->service_name;
$service->serviceprice = $request->service_price;
$service->create();
return redirect()->route('service.show');
}
/**
* 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)
{
//
}
}
i need
service form insert to service table
customer form insert to customer table
contact form insert to contact table
in laravel 5.8
In this case you have 3 entities.
Customer
Service
Contact
For all 3 entities you would want to have a model, a controller and a migration. If the entities have relations between one and other you can specify those in the model. Take a look at the link for more information about relationships in laravel.
https://laravel.com/docs/master/eloquent-relationships
When you have made the relations in the model, made the migrations its time to move on to the controller.
In the controller you will have the following functions:
public function index(Contact $model)
{
// return index view
}
public function create()
{
// return create form view
}
// Option 1
public function store(Request $request, Service $service)
{
$service->create($request->all);
return redirect()->route('your.route');
}
// Option 2
public function store(Request $request, Service $service)
{
$service->servicetype = $request->service_type;
$service->servicename = $request->service_name;
$service->serviceprice = $request->service_price;
$service->create();
return redirect()->route('your.route');
}
public function edit()
{
// return edit view
}
public function update(Request $request, Service $service)
{
$service->update($request->all);
//return view
}
public function destory(Service $service)
{
$service->delete();
// return view
}

How to implement Abstract class in laravel 5

in App/Abstracts/ folder I have following file
1.Transformer.php
<?php
namespace App\Abstracts;
use eventsTransformer;
abstract class Transformer
{
public function transformCollection(array $item)
{
return array_map([$this ,'transform'],$item->toArray());
}
public abstract function transform($event);
}
2.eventsTransformer
<?php
namespace App\Abstracts;
class EventTransformer extends Transformer
{
public function transform($event)
{
return [
'event'=> $event['event'],
'date' => $event['date'],
'e_code'=> $event['eventcode'],
'country'=> $event['country'],
'city'=> $event['city']
];
}
}
Now In my Controller I am using them like this
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\ResponseFactory;
use App\Abstracts\eventsTransformer;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Event;
class EventsapiController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
//use EventTransformer;
protected $eventTransformer;
public function __construct(EventTransformer $EventTransformer){
$this->$eventTransformer = $EventTransformer; // replace 'collector' with whatever role you need.
}
public function index()
{
//
$events = Event::All();
$response = array();
return response()->json([
'data'=>$this->eventTransformer->transform($events)
],200);
//return $response['data']=$events;
}
/**
* 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)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
$event = Event::find($id);
if(!$event)
{
return response()->json([
'error'=> 'Event does not exist',
'code' => 'e101'
],404);
}
return response()->json([
'data'=>$event->toArray()
],200);
}
/**
* 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)
{
//
}
}
I am getting following error
ReflectionException in Container.php line 790: Class App\Http\Controllers\EventTransformer does not exist
Can any one help me out with this
MyQuestion is the same what How to include abstract in Laravel5
Thanks
You are getting this error because you misspelled namespace.
Instead of:
use App\Abstracts\eventsTransformer;
you should use:
use App\Abstracts\EventTransformer;
in your EventsapiController

Categories