Get user data on a POST request in Laravel - php

I just starting to learn laravel and i don't know how to pass data on controller
this is my routes.php
Route::get('/', 'view#index');
Route::get('/create', 'view#create');
Route::get('/store', 'view#store');
Route::post('/destroy', 'view#destroy');
and this is my controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Request;
class view extends Controller {
public function index()
{
echo $data = User::all();
}
public function create()
{
$user = User::create(['first_Name'=>'abc']);
echo "id =".$user->id." is created";
}
public function store()
{
echo "store";
}
/* here i want to get id using post request */
public function destroy(Request $request)
{
$id = $request->input('id');
echo $id;
}
}
now I want to pass 'id' using postman in post request. how can i perform this. my get request working perfectly

In Postman, you can fill the request body:
The id key corresponds to the 'id' argument inside $request->input('id');
Note: My example uses DELETE, which is common practice for destroy/delete actions. In your case, it should be POST. I do, however, advice you to use DELETE.

Related

Model binding in Laravel

I am trying to fetch data from database in Laravel using Model binding in Controller but it returns empty $task array instead
here is my routes.php :
<?php
Route::get('/tasks','TasksController#index');
Route::get('/tasks/{task}','TasksController#show');
and this the TasksController :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
class TasksController extends Controller
{
public function show(Task $task)
{
return $task;
}
}
Route::get('/tasks/{id}','TasksController#show');
in route model binding you can pass the id in the route and handle it in the show function
public function show($id)
{
$task = Task::find($id);
return $task;
}
This will directly return the task
Without seeing other code it's hard to answer however your function index() should look similar to this:
public function index(Task $task)
{
$tasks = $task->all();
return view('page', compact('tasks'));
}
Now, assuming you're gathering the ID's via a foreach loop your controller code should look like this:
public function show($id, Task $task)
{
$task = $task->find($id);
return view('somePage', compact('task'));
}
This will then pass $task back to a page of your choosing to display the specific task you are getting.
Your Routing is fine providing you're passing the:
Something
back within the blade.

Sharing a variable in controller - Laravel

I'm getting the user_id from the session and using it quite a bit throughout my contrpller. So am looking at ways of retrieving that variable.
I have set everything up to get it (How I understand) but the Variable is returning
null
My Controller looks as follows :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\VideoLog;
class VideoController extends Controller
{
private $user_id;
public function __construct(Request $request)
{
$user_id = session('id');
$this->user_id = $user_id;
}
public function log_watched(Request $request)
{
dd($this->user_id);
// See If Video Has Been Watched Before....
$video_watched = VideoLog::where('user_id', $this->user_id);
}
}
Is it something to do with the session?
How would I retrieve it?
The reason you're having this issue is because the controller __construct method is run before the middleware that starts the session.
(more information here)
As the post says, you can get round this issue by using the middleware method in the controller's __construct method:
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user_id = session('id');
return $next($request);
});
}
This will allow you to set the user_id on the controller.
Hope this helps!

$request->session didn't work in Laravel 5.3 resource controller

First of all, I already check that in other controller (not in resource controller) my session work very well, but when I did it in the resource controller my code for get session didn't work.
Here's my resource controller
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//tambahan
use DB;
use Session;
//model
use App\_admins;
use App\Mahasiswas;
class MahasiswaController extends Controller
{
protected $data;
protected $token;
public function __contruct(){
$this->data = array();
$this->middleware(function ($request, $next) {
$this->token = $request->session()->get('_admin_id');
if (!$request->session()->has('_admin_id')) {
abort(404);
}
return $next($request);
});
}
private function user($token){
$this->data['query'] = _admins::find($token);
}
public function index(){
echo $this->token;
}
There is more public function, but it's still empty so I am not showing it here to avoid confusion. And here is my route in web.php:
Route::group(['namespace' => 'Admin'],function(){
Route::resource('/admin/mahasiswa','MahasiswaController');
Route::resource('/admin/nilai','NilaiController');
});
In 5.3 the middleware hasn't run yet in the constructor, so you're unable to gather session data. But using your closure-based approach, you should be able to access it with something like this:
$this->middleware(function($request, $next) {
// Get the session value (uses global helper)
$this->token = session('_admin_id');
// If the value is null, abort the request
if (null === $this->token) abort(404);
return $next($request);
});

Laravel 5.3 - Blank Screen - Insert Query

I Newly Installed Laravel 5.3. I Create Model and Controller for Role table. When i send Insert Data to Role Controller. It will be display Blank Page. but print a posted will be display. Select Query will be working but insert query not working.
Controller :
<?php namespace App\Http\Controllers;
use Auth;
use DB;
use App\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('admin.userlist');
}
public function roleindex()
{
return view('admin.role');
}
public function saveRole(request $request)
{
$id=Auth::user()->id;
$role=new Role;
$role->role_title=$request->input('role');
$role->created_by=$id;
$role->created_date=date('Y-m-d H:i:s');
$role->save();
$request->session()->flash('alert-success','Role details Saved Successfully!');
return redirect('dddddd');
}
}
Change your saveRole function as follows.
public function saveRole(Request $request)
{ // Rest of your code
}
Use like this :
if this will not work then there will be other issue somewhere else.
public function saveRole(\Request $request)
{
$role=new Role;
$role->role_title=$request->get('role');
$role->created_by=$id;
$role->created_date=date('Y-m-d H:i:s');
$role->save();
$request->session()->flash('alert-success','Role details Saved Successfully!');
return redirect('to_some_url');
}

Request Class Laravel 5.2

i use this code for return articles but not run this code and return blank,
how to solve this problem?
namespace App\Http\Controllers;
use App\taxonomy;
use Illuminate\Http\Request;
class newPostController extends Controller {
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
first time:
for next record:
Try this:
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
Update
You're getting empty result seconds time, because first time you're sending info through request, so $request has some data. If you reload the page, you do not send any info, that's why it's empty. It's just how it works.
Usually, when you're using $request data, you want to store it in DB or somehting and then redirect somewhere, for example, return $redirect->back(); instead of your return clause.
Try this
public function submitArticle(Request $request){
$name = $request->all();
return $name;
}
add this line in your class
use Illuminate\Http\Request;
You need to inject the Request into your method:
namespace App\Http\Controllers;
use App\taxonomy;
use Illuminate\Http\Request;
class newPostController extends Controller {
public function submitArticle(Request $request){
$name = $request->input();
return $name;
}

Categories