I look around about this issue but still can't understand why I'm getting error :
Class "App\Models\MenuList" not found
app/Models/Listing.php
<?php
namespace App\Models;
class MenuList
{
public static function all()
{
return [
'id' => 1,
'title' => 'liston one',
];
}
}
routes/web.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\MenuList;
Route::get('/', function () {
return view('listings', [
'heading' => 'lates listing',
'listings' => MenuList::all()
]);
});
Try this
create model using command
php artisan make:model MenuList
App\Models\MenuList.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MenuList extends Model
{
use HasFactory;
public static function listData()
{
return [
'id' => 1,
'title' => 'liston one',
];
}
}
routes/web.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\MenuList;
Route::get('/', function () {
return view('listings', [
'heading' => 'lates listing',
'listings' => MenuList::listData()
]);
});
Related
I have two model that are related.
Models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Address extends Model
{
use HasFactory;
protected $fillable = [
'city',
'post_code',
'street',
'country'
];
protected $hidden = [
'created_at',
'updated_at',
'addressable_id',
'addressable_type'
];
public function addressable(): MorphTo
{
return $this->morphTo();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Club extends Model
{
use HasFactory;
protected $fillable = [
'name',
'email',
'phone',
'description',
'user_id'
];
protected $hidden = [
'created_at',
'updated_at'
];
public function address()
{
return $this->morphMany(Address::class, 'addressable');
}
}
For that models i've got Request class with validator rules.
Requests
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreAddressRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'city' => 'required|string|max:125',
'post_code' => 'required|string',
'street' => 'required|string',
'country' => 'required|string| max:125'
];
}
}
<?php
namespace App\Http\Requests;
use App\Traits\FailedValidation;
use Illuminate\Foundation\Http\FormRequest;
class StoreClubRequest extends FormRequest
{
use FailedValidation;
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|string|max:125',
'description' => 'required|string',
'email' => 'email:dns',
'phone' => 'string|max:125',
// 'address' => (new StoreAddressRequest)->rules(), ???
'images' => 'required|image'
];
}
}
Trying to create new club with address.
I'm sending request to API like that:
[
"name":"dfsdf",
"dsfsdf":"sdfsdf",
"address": {
"city": "name",
"post_code":"46-454"
}
]
Address may be added standalone or from other model that are related too.
Now only fields from club request are validated.
How to validate club and address request?
A few days ago I started learning laravel 7.
I bought a course on udemy.
I got to the part where the real registry system went and started to rewrite the code like in the video, but when I do it I get an error!
Error Message: "Class 'App\Http\Controllers\Validator' not found"
I've been trying to fix this for hours, and I'm not doing well
AccountController.php
<?php
namespace App\Http\Controllers;
class AccountController extends Controller
{
public function getcreate(){
return view('account.create');
}
public function postcreate(){
$validator = Validator::make(Input::all(),
array(
'email' => 'required|max:50|email|unique:users',
'username' => 'required|max:20|min:3|unique:users',
'password' => 'required|min:6',
'repeat_pass' => 'required|same:password'
));
if($validator->fails()){
die('ERROR');
}
else{
die('Cool');
}
}
}
you need to import validator namespace
use Illuminate\Support\Facades\Validator;
then instead of Input you could use request()->all() helper function
so it will be like this
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Validator;
class AccountController extends Controller
{
public function getcreate(){
return view('account.create');
}
public function postcreate(){
$validator = Validator::make(request()->all(),
array(
'email' => 'required|max:50|email|unique:users',
'username' => 'required|max:20|min:3|unique:users',
'password' => 'required|min:6',
'repeat_pass' => 'required|same:password'
));
if($validator->fails()){
die('ERROR');
}
else{
die('Cool');
}
}
}
You will need to import the Validator class from it's correct namespace which is Illuminate\Support\Facades. So goes with Input class. Best way I can suggest is to add these in aliases section in config\app.php like below:
'aliases' => [
// other imports
'Validator' => Illuminate\Support\Facades\Validator::class,
'Input' => Illuminate\Support\Facades\Input::class,
]
Now, you can simply use them in your controller like below:
<?php
namespace App\Http\Controllers;
use Validator;
use Input;
class AccountController extends Controller
{
// rest of your code
}
You can use Validator namespace in your controller at top like:
use Validator;
why dont display relation data in Rest Yii2
i have two tables.
sample:
category , subcategory
<?php
namespace app\controllers;
use app\models\Category;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\rest\Controller;
class ApiController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats'] = ['application/json' => Response::FORMAT_JSON];
return $behaviors;
}
public function actionGetSk($cId)
{
$result= Category::find()->with('subCategory')->where(['id' => $cId])->all()
return $result;
}
}
i result i have only from Category. (result is json)
but print_r($result) i have data from Category and subCategory.
web.php
[
'class' => 'yii\rest\UrlRule',
'pluralize' => false,
'controller' => 'api',
],
Try this: in your Category model, add this method:
public function extraFields() {
return [
'subcategory' => 'subCategory',
];
}
And now, call your api with the expand get parameter like:
http://yourapi.com/api/get-sk?cID=1&expand=subcategory
I am having the following error
InvalidArgumentException in FormBuilder.php line 39:
Form class with name App\Http\Controllers\App\Forms\SongForm does not exist.
on Laravel,
SongsController.php class
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;
class SongsController extends BaseController {
public function create(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
return view('song.create', compact('form'));
}
public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class);
if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}
// Do saving and other things...
}
}
SongForm.php
<?php
namespace App\Forms;
use Kris\LaravelFormBuilder\Form;
class SongForm extends Form
{
public function buildForm()
{
$this
->add('name', 'text', [
'rules' => 'required|min:5'
])
->add('lyrics', 'textarea', [
'rules' => 'max:5000'
])
->add('publish', 'checkbox');
}
}
routes.php
Route::get('songs/create', [
'uses' => 'SongsController#create',
'as' => 'song.create'
]);
Route::post('songs', [
'uses' => 'SongsController#store',
'as' => 'song.store'
]);
And I do not know where is the problem because the file exist in the project folder.
Explanation of the Error
Here:
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
You're specifing the class name with a namespace relative to the current namespace:
App\Forms\SongForm::class
the full class name will be built relatively from the current namespace that is:
namespace App\Http\Controllers;
So, the class you're passing as parameter becomes:
App\Http\Controllers\App\Forms\SongForm::class
That class doesn't exists, and so you get the error
How to solve
To solve, you can specify the absolute namespace. Change this:
App\Forms\SongForm::class
to this:
\App\Forms\SongForm::class
and it should work
I create a customized login system in Laravel.
This is my controller:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Auth;
class Front extends Controller
{
public function register()
{
if (Request::isMethod('post')) {
User::create([
'name' => Request::get('name'),
'email' => Request::get('email'),
'password' => bcrypt(Request::get('password')),
]);
}
return Redirect::away('login');
}
public function authenticate()
{
if (Auth::attempt(['email' => Request::get('email'), 'password' => Request::get('password')])) {
return redirect()->intended('checkout');
} else {
return view('login', array('title' => 'Welcome', 'description' => '', 'page' => 'home'));
}
}
public function login()
{
return view('auth/login', array('page' => 'home'));
}
public function checkout()
{
return view('/aboutme', array('page' => 'home'));
}
}
And the routes are:
// Authentication routes...
Route::get('auth/login', 'Front#login');
Route::post('auth/login', 'Front#authenticate');
Route::get('auth/logout', 'Front#logout');
// Registration routes...
Route::post('/register', 'Front#register');
Route::get('/checkout', [
'middleware' => 'auth',
'uses' => 'Front#checkout'
]);
The error i am getting is:
FatalErrorException in Front.php line 12: Class
'App\Http\Controllers\Request' not found
You need to import Request. Add this to the top of your controller:
use Request;
By the way you will need to do this for the Redirect facade as well.
Your imports should look like this:
use Auth;
use Request;
use Redirect;
use App\Http\Controllers\Controller;
Try the following as it seems like you are not using Request specific namespace in your Front Controller:
use Illuminate\Http\Request;