No response after clicking on Create Gallery button - php

Route::group(['middleware' =>'auth'], function(){
Route::prefix('/user/')->group(function(){
Route::get('galleries/create', [GalleryController::class, 'gallerycreate'])->name('galleryCreate');
Route::post('galleries/store', [GalleryController::class, 'gallerystore'])->name('galleryStore');
});
<div class="card-header">Create New Gallery</div>
<div class="card-body">
<form action=" {{ route('galleryStore') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
#csrf
<div class="row">
<div class="col-md-12">
<label for="description">Gallery Description</label>
<textarea name="description" rows="3" class="form-control"></textarea>
</div>
</div>
<br>
<button class="btn btn-primary" type="submit">Create Gallery</button>
</form>
</div>

From what I can see it looks like you have included two csrf fields. {{ csrf_field() }} and #csrf This shouldn't be a big issue but I would expect this to throw an error.
Secondly the GalleryStore action in the GalleryController does not return anything. The action should include something like return view('some.view')->with(['data' => $data]);

Related

The GET method is not supported for this route. Supported methods: POST Laravel 8

home.blade.php file
'''
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
{{ __('You are logged in!') }}
</div>
<div class="card-body">
#csrf
<form action="/upload" method="post">
<input type="file" name="image">
<input type="submit" name="upload">
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
'''
My routes
'''
Route::post('/upload',function (){
return('Halu');
});
I don't know where the error is help out as I am a beginner on laravel 8. The route is defined with the function and I don't know where the get or post goes wrong
Pass the url in action attribute. and pass #csrf token inside the form.
<form action="{{ url('/upload')}}" method="post">
#csrf
</form>
you try to upload a file you have to set enctype="multipart/form-data" to your form
it should look like this
<form action="{{ url('/upload')}}" method="post" enctype="multipart/form-data">
#csrf
</form>
this may solve your issue

How to fix 'call to a member functuin delete() on null' in Laravel 5.7

I'm making a form delete request, and every time I try to delete some content I get an error 'call to a member function delete on null'. I didn't used Laravel for a very long time, and I have forgotten how things work, and now I'm learning from beginning, following tutorial from Laracast. I compared the code and everything is good, but I get always some errors, and I cannot find a problem...
web.php
Route::resource('projects', 'ProjectsController');
ProjectsConroller
public function destroy($id)
{
$project = Project::find($id);
$project->delete();
return redirect('/projects');
}
edit.blade.php
#extends('layout')
#section('content')
<h1>Edit Project</h1>
<form method="POST" action="{{ url('/projects/$project->id') }}" >
{{ method_field('PATCH') }}
{{ csrf_field() }}
<div class="field">
<label class="label" for="title">Title</label>
<div class="control">
<input type="text" class="input" name="title" placeholder="Title" value="{{ $project->title }}">
</div>
</div>
<div class="field">
<label class="label" for="description">Description</label>
<div class="control">
<textarea name="description" class="textarea">{{ $project->description }}</textarea>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link">Update Project</button>
</div>
</div>
</form>
<form method="POST" action="{{ url('/projects/$project->id') }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<div class="field">
<div class="control">
<button type="submit" class="button">Delete Project</button>
</div>
</div>
</form>
#endsection()
This part of your code is wrong:
<form method="POST" action="{{ url('/projects/$project->id') }}"
It's passing $project->id as the ID instead of evaluating. You have 2 ways:
<form method="POST" action="{{ url('/projects/'.$project->id) }}"
<form method="POST" action="{{ url("/projects/$project->id") }}"
As an addition,
In your controller method, you might want to use findOrFail() instead of find() so it returns 404 when the model is not found. find() will return null if model is not found, and you're already trying to call ->delete() on null.

The GET method is not supported for this route. Supported methods: PUT. but i'm using PUT

I'm trying to update my post table, yet it keeps giving me a method error, saying that I should be using PUT instead of GET.
The thing is I'm using PUT. I've tried to change the #crsf and change the #method('PUT') several times, putting a hidden input included. When I call route:list, it shows that I'm using the PUT method, yet it still gives me the same error. I have no idea what it could be, does anyone have any idea?
<section id="index" class="container">
<div class="col-md-12 d-flex justify-content-center mt-5">
<article class="new-post col-md-6 py-2">
<header class="col-12 row d-flex justify-content-center">
<div class="add-header p-1">
<h2 class="m-2">Editar Post</h2>
</div>
</header>
<form method="POST" class="mt-5" action= "/edit_post/{id}" enctype="multipart/form-data">
#method('PUT')
#csrf
<div class="add-photo form-group d-flex justify-content-center p-5">
<label for="file-input">
<img src="{{ url($post->imagem) }}" alt="imagem do post">
</label>
<input id="file-input" name="imagem" type="file">
</div>
// rotinas pagina index
Route::get('/index', 'PostsController#posts');
// crud posts
Route::get('/add_post', 'PostsController#adicionandoPost');
Route::post('/add_post', 'PostsController#salvandoPost');
Route::put('/edit_post/{id}', 'PostsController#alterarPost');
Route::delete('/edit_post{id}', 'PostsController#deletarPost');
//crud usuario
Route::get('/profile/{id}', 'UserController#profile');
Route::put('/profile/{id}', 'UserController#alterarUsuario');
Route::delete('/index', 'UserController#deletarUsuario')->name('delete');
});
PostsController:
public function alterarPost($id, Request $request) {
$post = Post::findOrFail($id);
form:
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="add-photo form-group d-flex justify-content-center p-5">
<label for="file-input">
<img src="{{ url($post->imagem) }}" alt="imagem do post">
</label>
<input id="file-input" name="imagem" type="file">
</div>
<input type="hidden" name="user_id" value="{{$post->id}}">
<div class="add-input form-group mt-2">
<input class="col-12 p-1" name="descricao" type="text" placeholder="Descrição" value="{{ $post->descricao }}">
</div>
<div class="add-input form-group">
<input class="col-12 p-1" name="tags" type="text" placeholder="Tags" value="{{ $post->tags }}">
</div>
<div class="add_photo d-flex justify-content-end p-3">
<button class="btn btn-danger m-1" type="button">Cancelar</button>
<button class="btn btn-success m-1" type="submit">Publicar</button>
</div>
</form>
Your form looks like.
<form method="post" class="mt-5" action= "{{ route('editpost',['id'=>$post->id]) }}" enctype="multipart/form-data">
{{ method_field('PUT') }}
{{ csrf_field() }}
</form>
your web.php
Route::put('/edit_post/{id}', 'PostsController#alterarPost')->name('editpost');
See your Web.php you have an error in your route Change this:
Route::delete('/edit_post{id}', 'PostsController#deletarPost');
To
Route::delete('/edit_post/{id}', 'PostsController#deletarPost');
You have forgot to add /
In your form change the enctype to be :
application/x-www-form-urlencoded
Put and Patch actions don't use the typical form-data of post.
Check your .htaccess file once it should have this line
Header set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"

error while retrieving data from the database

i was trying to display news with its comments, but one of the headlines is giving me an error,but others are working, i have a table of news and i have a table of news_comments, some of the news are being retrieved perfectly with their comments but this one is giving me an error.
#extends('layouts.app')
#section('content')
<div class="container">
{{$news->title}}<br/>
#foreach($news->news_pictures as $news_picture)
<img src="{{asset($news_picture->pictures)}}" width="200"><br/>
#endforeach
{!! $news->body !!} <br/>
<small>written on{{$news->created_at}} by {{$news->user->name}} </small>
</div>
#foreach($news->news_comments as $news_comment)
#if(!Auth::guest())
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#else
<div class="well">
{{$news_comment->commentor}}<br/>
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
#endforeach
#if(!Auth::guest())
<form action="{{action('NewsController#AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
#else
<form action="{{action('NewsController#AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<input type="text" class="form-control" placeholder="your name" name="commentor">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
#endif
#endsection
This error occur when the property you want to access that doesn't exist so you should check property isset or not
Here is the example
#if (!is_null($news_comment->user))
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
OR
#if (isset($news_comment->user->id) && isset($news_comment->user->name))
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
Try to use: with () on user
{!! $news_comment->user()->name!!}
Instead of:
{!! $news_comment->user->name!!}

MethodNotAllowedHttpException this wont let me insert the data

I'm just trying to insert title and body using eloquent on database and MethodNotAllowedHttpException appears wont let me do it cause of that error
Here's the controller
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
Here's my view create.blade.php
<form action="post.store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
i am assuming your are using resource route.
i think this <form action="post.store" method="POST"> should be <form action="{{ route('post.store') }}" method="POST"> and use this too {{ csrf_field() }} inside form otherwise you will get tokenmismatch error.
<form action="{{ route('posts.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
You may use command php artisan route:list and in column Name you will see route name and you will be able to call in view by {{ route('routename') }}
Example:
In route list is shown posts.store, so you will call in view {{ route('posts.store') }}
I think you may call incorrect route by above route view is posts.show but when insert you called post.store maybe posts.store
use in form action {{ route('posts.store') }}

Categories