We know that laravel has an update () method that updates records using the http "put" method. But I do not know how to create an edpoint in which I will be able to modify the email.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
public function index()
{
return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]);
}
public function store(Request $request)
{
$user = new User();
$user->name = $request->get("name");
$user->email = $request->get("email");
$user->password = $request->get("password");
$user->save();
return response()->json($user->toArray(), 200);
}
public function show($id)
{
//
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
And my route:
<?php
use Illuminate\Http\Request;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('v1/users/create', 'UserController#store');
RESTful APIs made by me tests in Postman. Help me somebody
Looking at your question, I think you need to create a route to link to update method in your controller same way you created a post for creating user.
Route::put('v1/users/client/{id}', 'UserController#update);
OR you can use laravel predefined code to create all CRUD routes.
Route::resource('v1/users/client', 'UserController').
To view all the routes created, use
php artisan route:list
Have a further study on this.
Related
I am Having this error in my code
Undefined function 'App\Http\Controllers\api\createToken'.intelephense(1010)
i have done all the necessary imports needed to use passport so that i can generate apiTokens but it does not work for me
i will really appreciate it if i can get some one to help me in fixing this error thanks in advance
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class authController extends Controller
{
public function signup(Request $request)
{
//
$rules=[
'name'=>'required|max:55',
'email'=>'required|email|unique:users',
'password'=>'required',
// 'password_confirm'=>'required',
];
// $valid = $request->validate($rules);
$valid = Validator::make($request->all(),$rules);
if($valid->fails()){
return response()->json(
$valid->errors(),400
);
}else{
$user = User::create($request->all());
$accessToken = $user-createToken('authToken')->accessToken;
return response()->json(['user'=>$user,'accessToken'=>$accessToken], 201);
}
}
public function login()
{
//
}
public function logout(Request $request)
{
//
}
public function user(Request $request)
{
$request->user()->token()->revoke(); return response()->json([
'message' => 'Successfully logged out'
]);
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
Sorry Guys it was a typo in the code
$user-createToken('authToken')->accessToken;
i left out the '>' character
$user->createToken('authToken')->accessToken;
I have a table sites with list of sites with following columns ('id', 'path', 'site_link'). I've written in a Site model public $timestamps = false; so that it won't try to work with time what I don't need.
Also I have the following routes
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->get('sites', 'App\Http\Controllers\SiteController#index');
$api->get('sites/{site}', 'App\Http\Controllers\SiteController#show');
});
The first one is working fine and returning all the data, however the second one is returning just [].
I have a controller which is below
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Use App\Site;
class SiteController extends Controller
{
public function index()
{
return Site::all();
}
public function show(Site $site)
{
return Site::findOrFail($site);
}
public function store(Request $request)
{
$site = Site::create($request->all());
return response()->json($site, 201);
}
public function update(Request $request, Site $site)
{
$site->update($request->all());
return response()->json($site, 200);
}
public function delete(Site $site)
{
$site->delete();
return response()->json(null, 204);
}
}
The show method in your SiteController is taking a Site object. However the route is set up to only take the siteId. The code below should work for you based on how you've set up your routes.
public function show($site)
{
return Site::findOrFail($site);
}
Apply same to all your other controller methods since you want pass the site id via the url to the controller methods.
I want to be able to restrict users to only be able to see their own profiles in my laravel project. So when a user wants to access their profile, they would go to the url followed by /userprofile/{id}. But as of right now, any user can type in the specific id of a different user and access their profile. So if I'm logged in as the first user to register, I would have an id of 1. But I only want to be able to access my profile. If I try to type in id 2 or 3 in the url I want it to kick me back to the homepage. Any idea how I could accomplish this? Using some sort of middleware perhaps?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use App\User;
use App\Salutation;
use App\Http\Requests\UserRequest;
use Auth;
class HomeController extends Controller
{
public function index(){
$users_info = User::all();
return view('userprofile.index', compact("users_info"));
}
public function show($user_info){
$user_info = User::find($user_info);
return view('userprofile.show', compact("user_info"));
}
public function create(){
return view('userprofile.create');
}
public function store(UserRequest $request){
$formData = $request->all();
User::create($formData);
return redirect('userprofile');
}
public function edit($user_info) {
$user_info = User::findOrFail($user_info);
return view('userprofile.edit', compact("user_info"));
}
public function update(UserRequest $request, $user_info){
$formData = $request->all();
$user_info = User::findOrFail($user_info);
$user_info->update($formData);
return redirect('userprofile');
}
public function __construct(){
$this->middleware('auth', ['only' =>['create', 'edit',
'destroy']]);
}
}
just compare the current user with param id
example:
public function getProfile(Request $request, $id)
{
if(Auth::id() == $id) {
// valid user
$user_info = Auth::user();
return view('userprofile.show', compact("user_info"));
} else {
//not allowed
}
}
is it possible to pass id through an link href in Laravel and display that page like /projects/display/2.
I have this link:
<td>View</td>
It displays the id when hovering over the link as /projects/display/2. But whenever i click on the link i get an error message of:
Sorry, the page you are looking for could not be found.
I have a view setup called projects/display, plus routes and controller.
routes:
<?php
Route::group(['middleware' => ['web']], function (){
Route::get('/', 'PagesController#getIndex');
Route::get('/login', 'PagesController#getLogin');
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::get('/projects/display', 'ProjectsController#getDisplay');
Route::resource('projects', 'ProjectsController');
});
Controller:
<?php
namespace App\Http\Controllers;
use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
class ProjectsController extends Controller
{
public function index()
{
}
public function create()
{
return view('projects.create');
}
public function store(Request $request)
{
$this->validate($request, array(
'name' => 'required|max:200',
'description' => 'required'
));
$project = new project;
$project->name = $request->name;
$project->description = $request->description;
$project->save();
Session::flash('success', 'The project was successfully created!');
return redirect()->route('projects.show', $project->id);
}
public function show()
{
$project = Project::all();
return view('projects.show')->withProject($project);
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function getDisplay($id){
$project = Project::find($id);
return view('projects/display')->withProject($project);
}
}
You need to change your route to:
Route::get('/projects/display/{id}', 'ProjectsController#getDisplay');
And then generate URL with:
{{ url('projects/display/'.$projects->id) }}
If you write route like below,
Route::get('/projects/display/{projectId}', 'ProjectsController#getDisplay')->name('displayProject');
You can use the name 'displayProject' in the href and pass the id as Array :
<td>View</td>
What you are looking for is a parameterized route. Read more about them here:
https://laravel.com/docs/5.3/routing#required-parameters
I found a better solution:
In your blade file do like this
<a href="{{route('displayProject',"$id")}}">
View
</a>
with this route , in route file
Route::get('/projects/display/{id}', 'ProjectsController#getDisplay');
$id is sent form your controller with compact
return view('viewPage', compact('id'));
So i have a 'TicketController' which holds my functions for manipulating 'tickets' in a system.
I am looking to work out the best way to send my new route that will take a route parameter of {id} to my TicketController to view a ticket.
Here is my route set
Route::group(['middleware' => 'auth', 'prefix' => 'tickets'], function(){
Route::get('/', 'TicketController#userGetTicketsIndex');
Route::get('/new', function(){
return view('tickets.new');
});
Route::post('/new/post', 'TicketController#addNewTicket');
Route::get('/new/post', function(){
return view('tickets.new');
});
Route::get('/view/{id}', function($id){
// I would like to ideally call my TicketController here
});
});
Here is my ticket controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Ticket;
use App\User;
class TicketController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Returns active tickets for the currently logged in user
* #return \Illuminate\Http\Response
*/
public function userGetTicketsIndex()
{
$currentuser = \Auth::id();
$tickets = Ticket::where('user_id', $currentuser)
->orderBy('updated_at', 'desc')
->paginate(10);
return view('tickets.index')->with('tickets', $tickets);
}
public function userGetTicketActiveAmount()
{
$currentuser = \Auth::id();
}
public function addNewTicket(Request $request)
{
$this->validate($request,[
'Subject' => 'required|max:255',
'Message' => 'required|max:1000',
]);
$currentuser = \Auth::id();
$ticket = new Ticket;
$ticket->user_id = $currentuser;
$ticket->subject = $request->Subject;
$ticket->comment = $request->Message;
$ticket->status = '1';
$ticket->save();
}
public function viewTicketDetails()
{
//retrieve ticket details here
{
}
You don't need to use closure here. Just call an action:
Route::get('/view/{id}', 'TicketController#showTicket');
And in TicketController you'll get ID:
public function showTicket($id)
{
dd($id);
}
More about this here.
You should use type-hint in laravel. Its awesome
In route
Route::get('/view/{ticket}', 'TicketController#viewTicketDetails');
In controller
public function viewTicketDetails(Ticket $ticket)
{
//$ticket is instance of Ticket Model with given ID
//And you don't need to $ticket = Ticket::find($id) anymore
{