model e controller laravel - php

I've this this model City.php in app\Models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
//
}
and this CityController.php
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CityController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$cities = City::OrderBy('city_code')->get();
return view('testcities',compact('cities'));
}
but i get back this error in laravel:
Error Class 'App\Models\City' not found

You should rename cities.php to Cities.php. But based on Laravel best practices Model names MUST be in singular form with its first letter in uppercase. So it's better to create model name with City rather than Cities.

Please make sure your class is named Cities.php. You can also try to run composer dump-autoload to tell Composer to read all your classes again.

Related

Im getting a error in Laravel after find and replacing some syntax

I just tried renaming a form submission git to vampire
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Vampire;
class VampireController extends Controller
{
/**
* Display a listing of the prducts.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$vampires = Vampire::all();
return view('vampires.index',compact('vampires',$vampires));
}
I get a error saying
"Class 'App\Vampire' not found"
The issue doesn't appear to be in VampireController.php. There are three things to check.
Make sure you have the correct file at app/Vampire.php
Verify the namespace: at the top of app/Vampire.php you should see namespace App;
Verify class name: class Vampire extends Model
It could be a minor typo, so be sure everything matches up exactly. If all else fails, a composer dump-autoload never hurts.

Laravel Notifications with custom connection [duplicate]

I am implementing Laravel 5.3 Notifications at the moment which is working very nice.
At the moment I am using 'email' as a notifications channel but I want to add 'database' too. I am using different databases/connections for languages and want to store the notifications in a central database / Connection.
How do I use a different database connection for notifications?
I already tried creating a Notifications model but that did not work:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notifications extends Model
{
protected $connection = 'system';
}
Hackish solution. But tried and tested on a MongoDB connection.
What needs to be modified;
The Notifiable trait
The DatabaseNotification model
Optionally (nothing changes if you are using mysql) modify the HasNotifications trait
Modify the DatabaseNotificationCollection.Again this is useful for a non-mysql connection
Step One : Create a custom Notifiable Trait
Copy the contents from Illuminate\Notifications\Notifiable and create a new file in your custom path...say App\Overrides\Notifications\Notifiable.
Your file will feature two changes...the namespace and you have to load the RoutesNotifications trait since we are not copying it over.
<?php
namespace App\Overrides\Notifications;
use use Illuminate\Notifications\RoutesNotifications;
trait Notifiable{
//The rest of the code remains
}
Step Two : Create a custom DatabaseNotification model
Follow the same procedure as above and copy the contents of the Illuminate\Notifications\DatabaseNotification file to the custom path that we created above...App\Overrides\Notification\DatabaseNotification
This is a standard Eloquent model and the connection change actually happens here
<?php
namespace App\Overrides\Notification;
//Use this if on mongodb.otherwise use to Illuminate\Database\Eloquent\Model
use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Notifications\DatabaseNotificationCollection;
class DatabaseNotification extends Model
{
protected $connection = 'YOUR_CONNECTION_NAME_GOES HERE';
}
As of this point this should work if you are on a mysql connection.
To try this out change the Notifiable trait on the user model to use App\Overrides\Notifications\Notifiable. The notifications will use the connection you specified.
Users of MongoDB will have to take extra steps since the most popular driver I know of does not yet support MorphMany relations which are put to use for Laravel notifications.
Since that is not the asked question we leave it at that :-)
On Laravel 5.7 based on #Bernard answer
User.php
<?php
namespace App;
// implement the override Notifiable trait
use App\Traits\Override\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
}
Notifiable.php
<?php
namespace App\Traits\Override;
use Illuminate\Notifications\RoutesNotifications;
trait Notifiable
{
use HasDatabaseNotifications, RoutesNotifications;
}
HasDatabaseNotifications.php
<?php
namespace App\Traits\Override;
use App\Models\Override\MultiConnectionDatabaseNotification;
trait HasDatabaseNotifications
{
/**
* Get the entity's notifications.
*
* #return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function notifications()
{
return $this->morphMany(MultiConnectionDatabaseNotification::class, 'notifiable')->orderBy('created_at', 'desc');
}
/**
* Get the entity's read notifications.
*
* #return \Illuminate\Database\Query\Builder
*/
public function readNotifications()
{
return $this->notifications()->whereNotNull('read_at');
}
/**
* Get the entity's unread notifications.
*
* #return \Illuminate\Database\Query\Builder
*/
public function unreadNotifications()
{
return $this->notifications()->whereNull('read_at');
}
}
MultiConnectionDatabaseNotification.php
<?php
namespace App\Models\Override;
use Illuminate\Notifications\DatabaseNotification as DatabaseNotification;
class MultiConnectionDatabaseNotification extends DatabaseNotification
{
// set your preferred connection here
protected $connection = 'oracle';
}
It's pretty simple, Just add protected $connection = 'YOUR CONNECTION NAME'; at Illuminate\Notifications\DatabaseNotification
That's all and it will work :)
You don't need to create new models if you are going to use one notification table with same connection.
My code will works if ur using different connection for USER model.

empty array in instance variable

im learning about laravel and im following some videos from laracasts, but im having a issue in displaying the data from the controller, i made all right, but still appears me empty array, the instance Card isnt working, here is my code:
Model:
Card.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
//
}
route:
Route::get('cards/{card}', 'CardsController#show');
CardsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Card;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CardsController extends Controller
{
public function show(Card $card)
{
return $card;
//$card = Card::find($card);
//return view('cards.show', compact('card'));
}
}
What you are attempting is called "Route Model Binding" and it seems to me that you are using Laravel 5.1 or lower (where route model binding is not implicit).
If you are using Laravel 5.2 or higher that code should just work. https://laravel.com/docs/5.3/routing#route-model-binding
But, if you are in Laravel 5.1 you need to do an additional step: https://laravel.com/docs/5.1/routing#route-model-binding
In the provider class RouteServiceProvider, in the boot method, you need to bind which route name {card} should bind to which Model, in this case Card.
So, you do something like this:
public function boot(Router $router)
{
parent::boot($router);
$router->model('card', \App\Card::class);
}
If you add that, the router will know that when it finds {card} it should get that number and do the Card::findOrFail with the ID automatically and if the model is found it will be passed down to your controller.
First of all, you will need to add the fillable protected property to your Eloquent model! Now, on to the good part.
In your routes file you have
Route::get('cards/{card}', 'CardsController#show');
This code, in a nutshell, will pass the card ID to your show function in your CardsController class. Eg. for this route: https://example.com/cards/5, it will in essence call the function show like this: show(5). In your code, you have that the show parameter is typehinted to be a Card. This is wrong. This is going to be an integer.
Thus, what you really need to do is check whether this ID exists and then pass the relevant information to your view. Something like this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Card;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CardsController extends Controller
{
/**
* Show the relevant card information
*
* #param $card This is the card ID (its an integer)
*/
public function show($card)
{
$card = Card::findOrFail($card);
return view('cards.show')->with(compact('card'));
}
}

Laravel can't find class in namespace

I using Laravel, I have a Model class under App/Models
<?php
namespace App\Models;
class TodoList extends \Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
In my Controller I have included the namespace as follows:
namespace App\Http\Controllers;
...
use App\Models\TodoItem;
use App\Models\TodoList;
class TodoListController extends Controller
My method looks like this:
public function show($id)
{
$list=TodoList::findOrFail($id);
return \View::make('todos.show')->with('list', $todo_list);
}
but when I call to a request i get the error:
FatalErrorException in TodoListController.php line 75: Class
'App\Models\TodoList' not found
Trying running composer dump-autoload. Basically your classes become cached so you need to tell Laravel to look for newly added classes.
I'm not sure, just try this :
namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class TodoList extends Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
}
Here is the code from the docs, but for your example. Note the use Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TodoList extends Model {
//insert public function listItems() here
}
Hope this is helpful!
Make sure the file is in the correct folder and has the same name caption as the Class you want to import.
If the caption of the file "TodoList" is not TodoList.php but Todolist.php for example, Laravel wont find it.
Depending on your setup running "composer dump" might help to refresh autoload files.

Laravel namespace in relations confused me

In my app I have Event and User models.Because of Event model I have to put it into namespace .so I created namespace as of following.
Event
<?php namespace App\Models;
class Event extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
User
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use App\Models\Event;
class User extends Eloquent implements UserInterface, RemindableInterface {
public function events()
{
return $this->hasMany('Event');
}
The relations between User and Event are simple OneToMany. So in my EventController I use POST method to create new event resource .
$e = new Event(array('keys'=>'values')); //without user_id filled
$user->events()->save($e);
At the same time i got an error .
Call to undefined method Illuminate\Support\Facades\Event::newQuery()
If I am not wrong i guess it is namespace error.But namespaces are already declared correctly I guess.
But I try visiting similar questions and used alternative way in relationship and then it worked fine.Personally I don't find it satisfied.Any idea why this is occurred ?
Change the above relation to
public function events()
{
return $this->hasMany('\App\Models\Event');
}
If you put Event inside a namespace you need to use it with it's fully qualified classname.
You can either import it into your controller:
use App\Models\Event;
Or specify the namespace inline:
new App\Models\Event();
Also, I would recommend that if you create a namespace for models that you put all of them in there. (User is missing a namespace in your code)

Categories