Request Class Laravel 5.2 - php

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

Related

Get user data on a POST request in Laravel

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.

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.

Class 'App\Http\Controllers\customer' not found Laravel 5.2

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\customer;
use App\Http\Requests;
class CustomerController extends Controller
{
public function index()
{
return view('insert');
}
public function create()
{
}
public function store(Request $request)
{
$customer = new customer;
$customer->name = $request->name;
$customer->sex = $request->sex;
$customer->pob = $request->pob;
$customer->tel = $request->tel;
$customer->email = $request->email;
$customer->save();
}
public function show()
{
return view('show');
}
}
?>
I m getting error as Class 'App\Http\Controllers\customer' not found Laravel 5.2.
I have use App\customer;
use Inputs;
Why am I getting error ?
What is the problem in the code?
If your customer class is in App namespace, you need to use:
use App\customer;
instead of
use App\Http\Controllers\customer;
This is simple You need to understand. Make sure you have created a model Customer.php in app/Customer.php and another important thing you must create your table with customers that's all. After that you need to add this line before class like:-
use App\Customer;
Hope it helps :)

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

how to protect to delete previous value of class element

I have controller class in laravel in which i have a function create() and a variable attachment i am calling function by ajax
my class code is.
class AttachmentController extends Controller
{
public $_attachments;
public function create()
{
$this->_attachments[]= 'test';
var_dump($this->_attachments);
}
problem is every time when i call it by ajax it return me "test" at 0 index of attachment array . but i want if i call create function 1st time it give me test on 0 index but when next time when i call it . it give me "test" on both 0 and 1 index and so on ..
how it is possible please help me
To preserve the values between requests you need to store them somewhere, an alternative is to use sessions, as the example below, for more information see https://laravel.com/docs/session
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AttachmentController extends Controller
{
public function create(Request $request)
{
$request->session()->push('attachments', 'test');
var_dump($request->session()->get('attachments'));
}
}
Try something like this.
$_attachments[] = Session::get('test');
$_attachments[] = 'test';
Session::get('test', $_attachments);
dd(Session::get('test'));
let me know if this is what you want.
Since HTTP driven applications are stateless, sessions provide a way
to store information about the user across requests.
class AttachmentController extends Controller
{
public function create()
{
$attachments = Session::get('attachments', array());
$attachments[] = 'test';
Session::put($attachments);
var_dump($this->_attachments);
}
}
Further reading: Laravel Session

Categories