laravel 5.6 not reading variable from controller in laravel - php

I am new to laravel so pardon me if this question seems stupid. I am trying to do a basic CRUD app, the create and read seems to work fine but am having issues with editing, updating and deleting. When I click the edit button it returns undefined variable log_books but to my understanding I think I have declared it. Please help.
controller name: logbook.php
namespace App\Http\Controllers;
use Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\log_book;
use DB;
use Illuminate\Support\Facades\Auth;
class logbook extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
/**
*
*#if(Auth::check())
*/
$log_books = log_book::all();
return view('home')->with('log_books',$log_books);
/**
*
*#endif
*/
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
/**
*
* #if(Auth::check())
*/
return view('log_books.create');
/**
*
* #endif
*/
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
log_book::create(Request::all());
return redirect('home')->with('message','name has been added successfully');
}
/**
* 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)
{
//
/**
*
*#if(Auth::check())
*/
$log_books = log_book::findorFail($id);
//$logs = DB::table('log_books')->where('id', $id)->first();
return view('log_books.edit', compact($log_books))->with('id', $id);
/**
*
*#endif
*/
}
/**
* 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)
{
//
$log_books = log_books::findorFail($id);
$destroy = $log_books->delete();
if($destroy) {
return redirect('home', compact($log_books))->with('message', 'Record has been deleted');
}
}
}
My Route: web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('create', function () {
return view('create');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/create', 'logbook#create')->name('create');
Route::post('store', 'logbook#store');
Route::get('/home', 'logbook#index');
Route::get('edit/{id}', 'logbook#edit');
Route::delete('destroy/{id}', 'logbook#destroy');
My View: home.blade.php
#foreach($log_books as $row)
<tr>
<td>{{$row->name}}</td>
<td>{{$row->present_date}}</td>
<td>{{$row->time_in}}</td>
<td>{{$row->time_out}}</td>
<td><input type="button" class="btn btn-success" value="Edit"> <input type="button" class="btn btn-danger" value="Delete"></td>
</tr>
#endforeach
edit view: edit.blade.php
<form method="post" action="edit">
{{csrf_field()}}
<input type="hidden" name="_method" value="PUT">
<input required="yes" type="text" placeholder="Full name" class="form-control" name="name" value="{{$log_books->name}}"><br>
<input required="yes" onfocus="this.type= 'date'" onblur="this.type='text'" placeholder="Date" class="form-control" name="present_date" value="{{$log_books->present_date}}"><br>
<input required="yes" onfocus="this.type= 'time'" onblur="this.type='text'" placeholder="Time in" class="form-control" name="time_in" value="{{$log_books->time_in}}"><br>
<input onfocus="this.type= 'time'" onblur="this.type='text'" placeholder="Time out" class="form-control" name="time_out" value="{{$log_books->time_out}}"><br>
<input type="submit" class="btn btn-primary" value="Submit" name="submit">
</form>
Pardon my code if it looks messed up, the create and edit view are both in a folder called log_books. Thanks for your help.

Your edit method is passing an invalid value to compact:
return view('log_books.edit', compact($log_books))->with('id', $id);
Should most likely be the 'name' of the variable you want to compact, not the variable itself:
return view('log_books.edit', compact('log_books'))->with('id', $id);
To avoid these types of mistakes completely, just define the array:
return view('log_books.edit', ['log_books' => $log_books])...

Laravel has Route model binding, you may use this feature in show and edit methods as follows:
In Controller
public function edit(Log_Book $log_book)
{
return view('log_books.edit', compact($log_book));
}
In web.php
Route::get('/edit/{log_book}', 'logbook#edit');
Also you don't need to check auth in controller methods, use auth middleware.
In Controller
public function __construct()
{
return $this->middleware('auth');
}

Related

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')
],
]);
}
}

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

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

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
}

Missing required parameters for route

I want to ask how to fix this error. I have this error Missing required parameters for [Route: userprofile.edit] [URI: userprofile/{userprofile}/edit]. (View: C:\xampp\htdocs\code\task1\resources\views\userprofile\create.blade.php)
I have user informations in view userprofile\create.blade.php and after submitting the button I want to relocate the page on the userprofile\edit.blade.php
This is my userProfileController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class userProfileController 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("userprofile.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)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
/**
* 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)
{
//
}
}
Also the route for this views
Route::group(['namespace' => 'User'], function(){
Route::get('/userprofile/{id}/edit', ['as' => 'userprofile.edit', 'uses' => 'userProfileController#edit']);
});
Route::resource('userprofile','userProfileController');
And my route action in the button in create.blade.php
<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
If you have any idea why is this error, please write me!
Thank you.
You can't add action to a input field. Add action to form tag. You are not passing any id in the form action. Add id to your action like I have passed id = 1
<form method="post" action="{{ route('userprofile.edit', ['id' => 1]) }}">
Update
First of all you have to create a user with given data and after that you redirect to the edit page with the generated id.
You need to set a default value for the id passed in to the edit and other methods, like so:
public function edit($id = null)
{
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
Here, we are setting the default value of id to null. Next, check in your method if it is null and if so, return with an error or whatever you wanna do in that situation:
public function edit($id = null)
{
if(!$id)
return back()->with(['error'=>'No document selected for editing.']);
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
if(!$id)
return back()->with(['error'=>'No document selected for editing.']);
This is your check and if the id is null, we simply return back and notify the user (You must check for the error in your blade view and display it if it exists).
instead of
<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
use
<a href="{{url('/userprofile/'.PROFILE ID.'/edit')" class="btn btn-primary" >Edit profile</a>
PROFILE ID must be id

Categories