I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
Related
I'm trying to setup a simple button to update a db column value when clicked. I can't seem to figure out why my route isn't getting passed my value however?
HTML:
<form method="post" action="{{ route('approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
Controller:
public function approveResturant($request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
Route:
Route::post('approveResturant'[ResturantController::class,'approveResturant'])->middleware(['auth'])->name('approveResturant');
And finally, the error itself:
Any help appreciated!
Add the Request type-hint to your function:
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
The difference here is that Laravel understands the Request type-hint and knows that it should inject the Request object from the pre-defined services it has in its service container. Otherwise, Laravel doesn't know where that parameter is coming from so assumes you will provide it. Simply naming your parameter $request is insufficient.
Update
Do you know why the function would still not be saving the new approved value to the DB?
A few potential reasons:
You have not removed the dd($request->all()); statement
$resturant = Resturant::find($id); failed to find a record in the database
save is a function not a property so $resturant->save; should be $resturant->save();
To isolate the exact issue you will need to perform some debugging (e.g. either using xdebug or dd statements).
Use Request Class
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
<form method="post" action="{{ route('restaurant.approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
Route::post("/restaurant/store", [RestaurantController::class, "approveResturant"])->name("restaurant.approveResturant");
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
$restaurant = Restaurant::where("id", $request->input("id"))->update([
"approved" => 1
]);
return redirect()->back()->with('message', 'Restaurant Approved Successfully!');
}
I have a small problem with my Controller action. I can't update my "link" in Database, bt dd method work is correctly when I'm try to check data.
Form
<form class="col-lg-push-6" action="/admin/links/{{$link->id}}/update" method="POST">
#csrf
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">New Link Value</label>
<input type="text" class="form-control" size="100" name="value">
<button class="btn btn-primary" type="submit">Update</button>
</div>
</form>
Controller
public function update(Request $request, $id)
{
$this->validate($request, [
'value' => 'required'
]);
$link=RedirectUrl::AllLinks()->where('id', $id);
$link->value = $request->input('value');
return redirect()->back()->with('message', 'Link Updated!');
}
Model
public function scopeAllLinks($query){
return $query->get();
}
Route
Route::prefix('admin')->middleware('auth')->group(function(){
Route::get('/', 'Admin\IndexController#index');
Route::get('dashboard', 'Admin\IndexController#index')->name('dashboard');
Route::get('links', 'Admin\LinkController#index')->name('links');
Route::get('links/{id}', 'Admin\LinkController#linkById');
Route::post('links/{id}/update', 'Admin\LinkController#update');
});
Few things here:
Your scopeAllLinks scope is incorrect, you don't call get inside a scope, instead you return the query builder instance.
You can use find since you're passing in a record id:
$link = RedirectUrl::find($id);
You never call save or update on the record:
$link->value = $request->input('value');
$link->save(); // <--- add this
I have an edit form with a text field and an image field where a user can edit and upload new text or a image if he/she wants to. But if the user does not upload a new image I just want to keep the old image in the database.
My Problem
The problem is when I hit upload button, I'm redirected to the article page and no error is shown but the DB has not been updated what so ever.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
web.php
Route::get('/tests/edit/{id}', 'TestController#edit');
Route::patch('/tests', 'TestController#update');
TestController.php
public function edit(Request $request)
{
$test = Test::findOrFail($request->id);
return view('test.edit', ['test' => $test]);
}
public function update(Request $request, Test $test)
{
$test->user_id = $request->user()->id;
$test->name = $request->name;
if($request->hasFile('image')) {
Storage::delete('public/image/' . $test->image); //Delete old image
$path = $request->file('image')->store('public/image');
$test->image = basename($path);
}
$test->update();
return redirect('/tests')
}
edit.blade.php
#extends('layouts.app')
#section('content')
<div>
<form action="/tests" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('patch') }}
name:<input type="text" name="name" value='{{ $test->name }}'><br>
image: <input type="file" name="image"><br>
<input type='submit' value='upload'>
</form>
</div>
#endsection
edit.blade.php add this :
<input type="hidden" name="id" value="{{ $test->id }}">
TestController.php
use $test->save(); instead of $test->update();
I had the same problem as you.I work on a ordering website with laravel that you can get data from laravel and show it to user and also he can order something from the website and the database will be updated.But, I cannot neither get or update data in database.I work with wampserver, mysql and the problem is solved once I switched to MariaDB instead of mysql in phpmyadmin: http://localhost/phpmyadmin/
I'm learning Laravel from Lacast and i'm trying to create a CRUD application.I've implemented the index,show,create and store correctly,but with the edit form when i try to submit data it's throwing BadMethodCallException.
Here are my routes
Route::get('/projects','ProjectsController#index');
Route::get('/projects/{id}','ProjectsController#show')->where('id','[0-9]+');
Route::get('/create','ProjectsController#create');
Route::post('/projects','ProjectsController#store');
Route::get('/projects/{id}/edit','ProjectsController#edit')->where('id','[0-9]+');
Route::put('/projects/{id}','ProjectsController#update')->where('id','[0-9]+');
Route::delete('/projects/{id}','ProjectsController#destroy');
Here is the edit form:
#extends('template');
#section('content')
<h2>Create new project</h2>
<p>/projects/{{ $project->id }}</p>
<form method="POST" action="/projects/{{ $project->id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div>
<input value="{{ $project->title }}" type="text" name="title" id="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Enter the project description">{{ $project->description }}</textarea>
</div>
<div>
<button type="submit">Update project</button>
</div>
</form>
#endsection
The controller code:
public function edit($id){
$project= Project::find($id);
return view('projects.edit',compact('project'));
}
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description');
$project->save();
return redirect('/projects');
}
The edit form displays as expected with data coming from the database,after submit i get the following error page:
With the example of the instructor the code has worked perfectly,and before using the PUT either on my form and in my controller i used PATCH like the instructor but always the same result.
Here is the instructor video link: Faking PATCH and DELETE Requests
The error is pretty straightforward.
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description'); // You don't have a method description()
$project->save();
return redirect('/projects');
}
This is what you probably meant to do:
public function update($id)
{
$project= Project::find($id);
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
This is my html form
<form class="form-horizontal" action="{{action('BlogController#update',[$blog->id]) }}" method="post">
<input name="method" type="hidden" value="patch"/>
<div class="form-group">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
Here is route:
Route::patch('blog/{id}','BlogController#update');
Controller :
public function update(Request $request,$id){
$input = $request->all();
$blog =findOrFail($id);
Blog::update($input);
//var_dump($input);
return back();
}
Can you please show me where is the issue?
In your code you have write $blog = findOrFail($id); to get blog which is not correct. You can do it using
$blog = Blog::findOrFail($id);
Now you have the blog, you need to update the blog. So, the update code should be
$blog->update($input);
To make this update method works, you need to make the fields(the fields you are updating) fillable in Blog model.
You're using the wrong syntax. Do something like this to make it work:
public function update(Request $request, $id)
{
Blog::where('id', $id)->update($request->all());
return back();
}
give the name whatever you wish say blog:
Route::patch('blog/{id}','BlogController#update')->name('blog');
your HTML code
<form class="form-horizontal" action="{{route('blog', $blog->id)}}" method="post">
hope this help you!!
you have many syntax problems!
try it this way:
form:
<form class="form-horizontal"
action="{{ route('blog.update', ['id' => $blog->id]) }}"
method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="patch"/>
<!-- other inputs -->
</form>
Route:
Route::any('blog/{id}','BlogController#update')->name('blog.update');
Controller:
public function update(Request $request, $id){
$blog = Blog::findOrFail($id);
$blog->update([
'key' => 'value'
]);
// never use $request->all() because of security issues!
return back();
}
<form class="form-horizontal" action="{{route('blog.update',[$blog->id]) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
Your Route Like This
Route::resource('blog', 'BlogController');
Your Controller
public function update(Request $request,$id){
$blog =Blog::findOrFail($id);
$blog->database_fieldname1=$request->value1;
$blog->database_fieldname2=$request->value2;
$blog->database_fieldname3=$request->value3;
$blog->save();
return back();
}