Using model's class static method directly, without "USE" keyword - php

I have "UsersController" file:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UsersController extends Controller
{
public function store(Request $request) {
User::create( $request->all() );
return "user added";
}
}
"store" method corresponds to "POST" request:
Route::post('users', ['uses' => 'UsersController#store']);
So now, if I send POST data to "users" page, it adds new user to "users" table in DB.
Ok, this works, but I have question about use App\User;
if remove this line and then try to use "create" method like this:
public function store(Request $request) {
App\User::create( $request->all() );
...
gives error: Class 'App\Http\Controllers\App\User' not found
Question is: why can't use "User" class static method "create" directly like this:
App\User::create( $request->all() )
?

replace your
App\User::create( $request->all() );
by
\App\User::create( $request->all() );

Related

Laravel of Controller Function Should be Compatible with Base Controller

I created a new route link to Users Controller validate function in web.php. I also create a function on Users Controller called validate. Other routes has no problem other than this.
Error
(1/1) ErrorException
Declaration of App\Http\Controllers\UsersController::validate(App\$id) should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)
Web routes
Route::group(['middleware' => 'can:see-admin'], function () {
Route::resource('users', 'UsersController', ['only' => ['index', 'destroy', 'create', 'validate']]);
Route::post('users/store', 'UsersController#store')->name('user.store');
Route::get('users/{user}/impersonate', 'UsersController#impersonate')->name('users.impersonate');
Route::get('users/{id}', 'UsersController#validate')->name('users.validate'); //this is the new route that is created
});
Users controller
class UsersController extends Controller
{
public function validate($id)
{
$validation = User::validate($image);
return back();
}
}
Controller
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
1: Don't use this Method name, it is be used by validator.
2: Try to use another action nameBecause you have missing the Request $request in your action, try to change it like this:
use Illuminate\Http\Request; // Remember import Request
...
class UsersController extends Controller
{
public function validateUser(Request $request, $id) {
...
}
}

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.

Routing Laravel namepsace issue

When I remove the inital use Illuminate\Http\Request and add use App\Item instead in the Controller file, the items/create route responds with a 404. How can I still use the App\Item namespace and get to the items/create route? I've tried adding both, but does not work.
web.php
Route::get('items', 'ItemsController#index');
Route::get('items/{item}', 'ItemsController#show');
Route::get('items/create', 'ItemsController#create');
ItemsController.php
<?php
namespace App\Http\Controllers;
use App\Item;
class ItemsController extends Controller
{
public function index(){
$items = Item::all();
return view('items.index', ['items' => $items]);
}
public function show(Item $item){
return $item->body;
}
public function create(){
return view('items.create');
}
}
Item.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
//
}
The problem is that laravel tries to match the routes in the order they are declared and the items/{item} route will match all routes starting with items/, including items/create. And because of the route model binding, Laravel tries to load an Item with ID create which obviously doesn't exist, so it throws a 404 error.
Route model binding in the docs:
Since the $user variable is type-hinted as the App\User Eloquent model
and the variable name matches the {user} URI segment, Laravel will
automatically inject the model instance that has an ID matching the
corresponding value from the request URI. If a matching model instance
is not found in the database, a 404 HTTP response will automatically
be generated.
To fix it simply change the order of your routes and put items/{item} after all other item/* routes:
Route::get('items', 'ItemsController#index');
Route::get('items/create', 'ItemsController#create');
Route::get('items/{item}', 'ItemsController#show');
Can you try this, please.
<?php
namespace App\Http\Controllers;
use App\Item;
class ItemsController extends Controller
{
public function index(){
$items = Item::all();
return view('items.index', ['items' => $items]);
}
public function show($id){
$item= Item::find($id);
return $item->body;
}
public function create(){
return view('items.create');
}
}

How to get value from URL in Laravel 5.2?

Routes.php
use Illuminate\Http\Request;
Route::get('/age/{val}','AgeController#store');
AgeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class AgeController extends Controller
{
//
public function store(Request $request)
{
$data = $request->input('val');
var_dump($data);
}
}
My url is localhost/lara/public/age/20.
but
output:array(0) { }
When I change $request->input('val',500);
then output: int(500)
How to get 20 (val) in controller? Why array empty?
If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies. Add $val as the 2nd parameter in you store function. In your case it should be
public function store (Request $request,$val){dd($val);}

Posting data return blank with a Laravel controller

I have a problem posting data in Laravel 5. I've done a page that post stuff on a controller that would create a new row on a database, according to Eloquent class. But when I post that, I receive a blank page and nothing works.
This is my controller, focused on the affected class:
<?php namespace App\Http\Controllers;
use App\T_subject;
use App\T_reservation;
use Date;
use Request;
use Config;
use Validator;
use Auth;
class TutoringController extends Controller {
public function reserve(Request $request)
{
$reservation = new T_reservation;
$reservation->user_id = Auth::user()->id;
$reservation->subject_code = $request->input('subject');
$reservation->date = $request->input('date');
$reservation->topic = $request->input('topic');
$reservation->save();
return redirect('/tutoring/view/'.$request->input('subject'));
}
}
This is my Eloquent model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class T_reservation extends Model {
protected $table = 't_reservations';
public function user()
{
return $this->belongsTo('App\User');
}
}
And my routes file:
<?php
Route::post('tutoring/reserve', ['middleware' => 'auth', 'uses' => 'TutoringController#reserve']);
Where's the problem?
In your routes file there is no definied route for your final redirection: '/tutoring/view/'.$request->input('subject').
Also, your model have no fillable property, which should list properties for which you can make a mass assignment. For instance something like this:
protected $fillable = ['user_id', 'subject_code', 'date', 'topic'];

Categories