Laravel 5.8 request data is empty - laravel

router.php
Route::get('/p00001/edit/{center}/{id}',
'Dashboard\Programs\P00001Controller#edit');
Route::put('/p00001/update/{center}/{id}',
'Dashboard\Programs\P00001Controller#update');
form.blade.php
<!DOCTYPE html>
<html dir="ltr" lang="zh-TW">
<body>
<form id="mainform" action="/dashboard/programs/p00001/update/JPN/3121-111907230007" method="post" class="form-horizontal">
{{ csrf_field() }}
{{ method_field('put') }}
<input type="text" value='1234' id="inpTest" name="inpTest">
<input type="submit" name="inpSubmit">
</form>
</body>
</html>
P00001Controller.php
public function __construct(Request $request, P00001Service $P00001Service )
{
$this->request = $request;
}
public function update($center, $id)
{
$data = $this->request->all();
echo "<pre>", print_r($_POST, 1), "</pre>";
echo "<pre>", print_r($this->request->all(), 1), "</pre>";
}
I got empty array.
I created a test.php
<?php echo "<pre>", print_r($_POST, 1), "</pre>"; ?>
If I change the action to test.php, it's ok, I can get $_POST data.
I searched for hours, no solution can fix my problem.

Delete the Request object from construct and add it into the update function.
public function update(Request $request, $center, $id) {
$data = $request->all();
//code
}
Edit:
I added the link of laravel documentation for you.
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller method.
Visit this to explore more.
Thanks

Make route as named route
Route::get('/p00001/edit/{center}/{id}','Dashboard\Programs\P00001Controller#edit')->name('edit');
Route::put('/p00001/update/{center}/{id}', 'Dashboard\Programs\P00001Controller#update')->name('update');
In view
<form id="mainform" action="{{route('update',['center' => 'JPN','id' => '3121-111907230007'])}}" method="post" class="form-horizontal">
{{ csrf_field() }}
{{ method_field('put') }}
<input type="text" value='1234' id="inpTest" name="inpTest">
<input type="submit" name="inpSubmit">
</form>
In controller
public function update(Request $request,$center, $id)
{
$data = $request->all();
echo "<pre>", print_r($_POST, 1), "</pre>";
echo "<pre>", print_r($request->all(), 1), "</pre>";
}
Don't create request object in constructor...Create in function definition.

I find out the problem. I use LaravelLocalization package. So if the locale is not provided, it redirects! After redirect, there are only url parameters!!
The action should be
/ja-JP/dashboard/programs/p00001/update/JPN/3121-111907230007

Check if you have the DB facade:
use Illuminate\Support\Facades\DB;

Related

why Resource Controllers not working in laravel 8?

route link
Delete
define route
Route::resource('users', UserController::class);
controller function
public function destroy($id)
{
$delete = DB::table('users')->delete($id);
if ($delete) {echo 'success';}
}
in the delete route, the method should be delete, you can write it in this way
<form action="{{ route('users.destroy', $user->id) }}" method="post">
#csrf
#method('delete')
<button type="submit">Delete</button>
</form>
first of all you need to find specific id then delete the same one
$user = User::findOrFail($id);
$user ->delete();

404 not found on laravel post request

I'm making a post request in my form. The csrf token and the method are there.
<div class="flex">
<form action="{{ route('profile.store.follower', $user) }}" method="post">
#csrf
<input value="follow" type="hidden" name="follow" value="{{$user->id}}" >
<button type="submit" class="bg-green-700">Follow</button>
</form>
</div>
The route:
Route::get('/usersprofile/{user:name}/index', [ProfileController::class, 'index'])->name('profile.index');
Route::post('/usersprofile/{user:name}/index', [ProfileController::class, 'store'])->name('profile.store.follower');
The controller:
public function store(User $user, Request $request) {
dd($user);
$user = user::findOrFail($request->follow);
Auth::user()->following->attach($user->id);
return back();
}
The following function in the user model:
public function following() {
return $this->belongsToMany(User::class, 'followers', 'user_id', 'following_id');
}
For some reason, it's not going through. I've tried php artisan route:cache and config:cache but the error persists. I've checked the route list and the route exists.
Your hidden input has two value attributes, that's probably why it doesn't find a user and fails.
Remove the hidden input and the line $user = user::findOrFail($request->follow); - the user you want to follow already exists in $user via your route. Then use Auth::user()->following->attach($user); to follow.
<div class="flex">
<form action="{{ route('profile.store.follower', $user) }}" method="post">
#csrf
<button type="submit" class="bg-green-700">Follow</button>
</form>
</div>
public function store(User $user, Request $request)
{
Auth::user()->following->attach($user);
return back();
}

Update data in laravel 6

I try to create crud in laravel 6. Create, Read and Delete process is running well. But when Update process, the data in table not change. Could anyone help me to find the problem ? The following my code.
Route
Route::get('/blog', 'BlogController#index');
Route::get('/blog/add','BlogController#add');
Route::post('/blog/store','BlogController#store');
Route::get('/blog/edit/{id}','BlogController#edit');
Route::post('/blog/update','BlogController#update');
Controller
public function index()
{
$blog = DB::table('blog')->get();
return view('blog',['blog' => $blog]);
}
public function edit($id)
{
$blog = DB::table('blog')->where('blog_id', $id)->get();
return view('edit', ['blog'=>$blog]);
}
public function update(Request $request)
{
DB::table('blog')->where('blog_id',$request->blog_id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
View
#foreach ($blog as $n)
<form method="post" action="/blog/update" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
You must provide id in your route
Route::post('/blog/update/{id}','BlogController#update');
In update method add parameter id and then find product against id
public function update(Request $request, $id)
{
DB::table('blog')->where('blog_id',$id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
#foreach ($blog as $n)
<form method="post" action="{{ route('your route name'), ['id' => $$n->id] }}" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
try separating the update into two statements like so
$blog = DB::table('blog')->where('blog_id',$id)->first();
$blog->update([
'blog_title' => $request->title,
'author' => $request->author]);
Also you might want to use models in the future so you can do it like
$blog = Blog::where('blog_id',$id)->first();
Doesn't really shorten your code but it improves the readibility.
Do your update like this:
public function update(Request $request)
{
$post = DB::table('blog')->where('blog_id',$request->blog_id)->first();
$post->blog_title = $request->title;
$post->author = $request->author;
$post->update();
return redirect('/blog');
}

Laravel error: Call to a member function store() on null

When I tried to send any file I get this message
It's form in home.blade.php
<form action="{{ URL::to('/upload') }}" enctype="multipart/form-data" method="post">
<input type="file" name="something" >
{{ csrf_field() }}
<button type="submit" name="button">Upload</button>
</form>
It's function inside a Controller
public function store(Request $request)
{
$path = $request->file('something')->store('upload');
echo $path;
}
Try to change controller code like this and see the result:
public function store(Request $request) {
$path = $request->something;
echo $path; }

get id from url and passing into form in laravel 5

i defined a route as
Route::get('roundtables/{name}/tags/store',['as'=>'tags.store','uses'=>'TagController#store','middleware'=>['owner','auth']]);
In my view, i have a form in this url
http://localhost:8000/roundtables/1/tags
<div class="col-md-3">
<div class="well">
{!! Form::open(['route'=>'tags.store','method'=>'GET']) !!}
<h2>New Tags</h2>
{{ Form::label('name','Name') }}
{{ Form::text('name',null,['class'=>'form-control']) }}
{{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
</div>
</div>
My problem is, how to get the id from url which is id '1' and passing into the form when user clicked submit.
My controller
public function store(Request $request,$name)
{
$this->validate($request,array('name'=>'required|max:255'));
$tag=new Tag;
$tag->name=$request->name;
$tag->roundtable_id=$name;
$tag->save();
Session::flash('success','New Tag was successfully added');
return redirect()->route('tags.index');
}
When you're using custom routes for CRUD, avoid using standard RESTful method and route names. Before building the form, you need to pass this variable to the view:
public function createTag($name)
{
....
return view('form', compact('name'));
}
Define your route as:
Route::get('roundtables/{name}/tags/storeTag',['as'=>'tags.storeTag','uses'=>'TagController#storeTag','middleware'=>['owner','auth']]);
Then pass variable to from the form:
{!! Form::open(['route' => ['tags.storeTag', $name], 'method'=>'GET']) !!}
And get it in the controller:
public function storeTag(Request $request, $name)
{
echo $name;
You get the wildcard value by using request() helper method easily.
{{request()->route('name')}}
In your TagController
public function index($name)
{
$tags= Tag::where($name)->first();
// add name parameter to return
return view('tags.index')->withTags($tags)->withName($name);
}
And in your view
<div class="col-md-3">
<div class="well">
//edit this form tag
{!! Form::open(['route'=>['tags.store',$name],'method'=>'GET']) !!}
<h2>New Tags</h2>
{{ Form::label('name','Name') }}
{{ Form::text('name',null,['class'=>'form-control']) }}
{{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
</div>
</div>
And in your TagController#store
public function store(Request $request,$name){
echo $name;
}
This should work.
Request::segment(2)
Or pass it from controller:
public function index($name)
{
$tags= Tag::where($name)->first();
return view('tags.index')->withTags($tags)->with('name', $name);
}
Then just pass it into the form:
{!! Form::open(['route'=>['tags.store', $name],'method'=>'GET']) !!}

Resources