Laravel: Contract file is not instantiable while building Controller - laravel

I am trying to add a shopping cart function to my Laravel application. I installed darryldecode/laravelshoppingcart package from GitHub and have been following instructions in these two websites.
TECHPOOL-Create a Shopping Cart with Laravel 6
LARASHOUT-Laravel E-Commerce Application Development – Checkout
I was able to create most of the shopping cart function with the first website but it didn't cover checkouts and placing orders so I found the second website.
The problem is that the contract file is not working. Here is the error I got.
Illuminate\Contracts\Container\BindingResolutionException
Target [App\Contracts\OrderContract] is not instantiable while building [App\Http\Controllers\CheckoutController].
http://localhost:8000/checkout
Where I use the contract file is in the checkout process witch is explained in the second website. I made few changes in the codes so that it will be consistent with the first website but mostly I followed what the website says.
Here are the codes that are mentioned in the error.
OrderContract.php
<?php
namespace App\Contracts;
interface OrderContract
{
public function storeOrderDetails($params);
}
CheckoutController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contracts\OrderContract;
use App\Http\Controllers\Controller;
class CheckoutController extends Controller
{
protected $orderRepository;
public function __construct(OrderContract $orderRepository)
{
$this->orderRepository = $orderRepository;
}
public function getCheckout()
{
return view('checkout');
}
public function placeOrder(Request $request)
{
// Before storing the order we should implement the
// request validation which I leave it to you
$order = $this->orderRepository->storeOrderDetails($request->all());
dd($order);
}
}
OrderRepository.php
<?php
namespace App\Repositories;
use Cart;
use App\Models\Order;
use App\Product;
use App\Models\OrderItem;
use App\Contracts\OrderContract;
class OrderRepository extends BaseRepository implements OrderContract
{
public function __construct(Order $model)
{
parent::__construct($model);
$this->model = $model;
}
public function storeOrderDetails($params)
{
$order = Order::create([
'order_number' => 'ORD-' . strtoupper(uniqid()),
'status' => 'pending',
'grand_total' => Cart::getSubTotal(),
'item_count' => Cart::getTotalQuantity(),
'table_number' => $params['table_number'],
'name' => $params['name'],
'notes' => $params['notes']
]);
if ($order) {
$items = Cart::getContent();
foreach ($items as $item) {
// A better way will be to bring the product id with the cart items
// you can explore the package documentation to send product id with the cart
$product = Product::where('name', $item->name)->first();
$orderItem = new OrderItem([
'product_id' => $product->id,
'quantity' => $item->quantity,
'price' => $item->getPriceSum()
]);
$order->items()->save($orderItem);
}
}
return $order;
}
}
RepositoryServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Contracts\OrderContract;
use App\Repositories\OrderRepository;
class RepositoryServiceProvider extends ServiceProvider
{
protected $repositories = [
OrderContract::class => OrderRepository::class,
];
/**
* Register services.
*
* #return void
*/
public function register()
{
foreach ($this->repositories as $interface => $implementation) {
$this->app->bind($interface, $implementation);
}
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
I'm not really familiar with the contract concept since I only started learning Laravel recently and I'm completely lost here. Maybe the problem is that I haven't created another file that is necessary or maybe something else.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.

Yes, this is expected...your contract should point to a Solid class else it's going to fail while trying to resolve it out of the container. So this is what you should do:
Create a class that implements that trait.
Go to your AppServiceProvider and bind it to that contract like this:
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
$this->app->bind(\App\Contracts\OrderContract::class, App\Repositories\ClassImplementingOrderContract::class);
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}
this should fix your problem.

This error could caused even if you have forget to add "RepositoryServiceProvider" into "config/app.php" 's serviceProviders array.

Related

Faker get streetAddress throwing ErrorException

I'm building a laravel application, and I've created a FakerServiceProvider to populate factories for testing and local dev.
<?php
namespace App\Providers;
use Faker\Factory;
use Faker\Generator;
use Faker\Provider\en_GB\Address;
use Faker\Provider\en_GB\Person;
use Faker\Provider\en_GB\PhoneNumber;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Class FakerServiceProvider
* #package App\Providers
*/
class FakerServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
*
*/
public function register()
{
$this->app->singleton(Generator::class, function ($app) {
$factory = Factory::create('en_GB');
$factory->addProvider(Person::class);
$factory->addProvider(Address::class);
$factory->addProvider(PhoneNumber::class);
return $factory;
});
}
/**
* #return array
*/
public function provides()
{
return [Generator::class];
}
}
I have created an address factory:
<?php
use App\Address;
use App\Country;
$factory->define(Address::class, function (Faker\Generator $faker) {
return [
'line_1' => $faker->secondaryAddress,
'line_2' => $faker->streetAddress,
'town' => $faker->city,
'county' => $faker->county,
'country_id' => factory(Country::class)->make()->id,
'postcode' => $faker->postcode,
'phone' => $faker->phoneNumber,
];
});
When I try to use this factory I get the following error:
ErrorException: call_user_func_array() expects parameter 1 to be a valid callback, non-static method Faker\Provider\Address::streetAddress() should not be called statically
I have checked the source for the Faker library and there is a streetAddress method here
I have tried calling both $faker->streetAddress and $faker->streetAddress()with no luck. I would expect$faker->streetAddressto produce something like ` or something similar.
Can anyone shed a bit of light on this for me
Removing the added providers in the Faker Service Provider fixed the issue

Is it a good practice to add custom method on Laravel Model class to insert record in another table?

I am following a tutorial to create a referal system in Laravel. In the tutorial it was not shown how to implement the addCredit() method of the user model class. I am a bit confuse. Assuming I have another table to keep the record of credits like :
user_credits
------------
user_id
credits
Is it good practice to write the code on user model's addCredits method to update the user_credits table? What will be the best in this case?
class User extends Authenticatable
{
/**
* Add bonus to the user
*/
public function addCredits($credit) {
//
}
}
The listener class to handle addition of the bonus for both the users.
namespace App\Listeners;
use App\Events\UserReferred;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class RewardUser
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param UserReferred $event
* #return void
*/
public function handle(UserReferred $event)
{
$referral = \App\ReferralLink::find($event->referralId);
if (!is_null($referral)) {
\App\ReferralRelationship::create(['referral_link_id' => $referral->id, 'user_id' => $event->user->id]);
if ($referral->program->name === 'Sign-up Bonus') {
// User who was sharing link
$provider = $referral->user;
// add credits to provider
$provider->addCredits(15);
// User who used the link
$user = $event->user;
$user->addCredits(20);
}
}
}
}
I'm not pretty sure, is it good practice or not, but i prefer abstract such things into a standalone service.
In your case it would be something like that:
CreditService
namespace App\Services;
use App\User;
class CreditService
{
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function addCredits($credits)
{
$this->user->credits += $credits;
$this->user->save();
}
}
Then in controller/listener you can work with this service
use App\Services\CreditService;
...
public function handle(UserReferred $event)
{
$referral = \App\ReferralLink::find($event->referralId);
if ( !is_null($referral) ) {
\App\ReferralRelationship::create([
'referral_link_id' => $referral->id,
'user_id' => $event->user->id,
]);
if ( $referral->program->name === 'Sign-up Bonus' ) {
(new CreditService($referral->user))->addCredits(15);
(new CreditService($event->user))->addCredits(20);
}
}
}
The way how you make and then use service might be different. So, if you don't want work via constructors, you can write static class and pass User into method directly.
I often put some additional actions into services. For example, fire events when i need to do it. Or log some things.

Laravel 5.3 - Best way to implement Entrust role on signup?

I'm working with Laravel 5.3 and I'm trying to set a role when someone signs up, I've used the Zizaco Entrust library.
I'm unsure on the best way to achieve something like this.
I tried to do this inside RegisterController's create method like below:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user = User::where('email', '=', $data['email'])->first();
// role attach alias
$user->attachRole($employee);
}
But obviously that's not right. So I'm a bit unsure on what the best practice is with this sort of thing.
If, as your comment on the OP suggests, you always want to assign the same role to a registered user, you can use a Model Observer for this - it's really simple.
// app/Observers/UserObserver.php
<?php namespace App\Observers;
use App\Models\User;
use App\Models\Role; // or the namespace to the Zizaco Role class
class UserObserver {
public function created( User $user ) {
$role = Role::find( 1 ); // or any other way of getting a role
$user->attachRole( $role );
}
Then you simply register the observer in your AppServiceProvider:
// app/Providers/AppServiceProvider.php
use App\Models\User;
use App\Observers\UserObserver;
class AppServiceProvider extends Provider {
public function boot() {
User::observe( new UserObserver );
// ...
}
// ...
}
This answer is mainly based off looking at your current solution, with a dash of original question.
Rather than filling out your model with methods like createNew, you'll probably find things easier to manage if you create a type of class specifically for interacting with models. You can call this a Repository or a Service or whatever takes your fancy, but we'll run with Service.
// app/Services/UserService.php
<?php namespace App\Services;
use App\Models\User; // or wherever your User model is
class UserService {
public function __construct( User $user ) {
$this->user = $user;
}
public function create( array $attributes, $role = null ) {
$user = $this->user->create( $attributes );
if ( $role ) {
$user->attachRole( $role );
}
return $user;
}
}
Now we need to deal with the fact that we've lost the hashing of passwords:
// app/Models/User.php
class User ... {
public function setPasswordAttribute( $password ) {
$this->attributes[ 'password' ] = bcrypt( $password );
}
}
And now we have the problem of sending out an activation email - that can be solved cleanly with events. Run this in the terminal:
php artisan make:event UserHasRegistered
and it should look something like this:
// app/Events/UserHasRegistered.php
<?php namespace App\Events;
use App\Models\User;
use Illuminate\Queue\SerializesModels;
class UserHasRegistered extends Event {
use SerializesModels;
public $user;
public function __construct( User $user ) {
$this->user = $user;
}
}
Now we need a listener for the event:
php artisan make:listener SendUserWelcomeEmail
And this can be as complex as you like, here's one I'm just copy/pasting from a project I have lying around:
// app/Listeners/SendUserWelcomeEmail.php
<?php namespace App\Listeners;
use App\Events\UserHasRegistered;
use App\Services\NotificationService;
class SendUserWelcomeEmail {
protected $notificationService;
public function __construct( NotificationService $notificationService ) {
$this->notify = $notificationService;
}
public function handle( UserHasRegistered $event ) {
$this->notify
->byEmail( $event->user->email, 'Welcome to the site', 'welcome-user' )
->send();
}
}
All that remains is to tell Laravel that the Event and Listener we've just created are related, then to fire the event.
// app/Providers/EventServiceProvider.php
use App\Events\UserHasRegistered;
use App\Listeners\SendUserWelcomeEmail;
class EventServiceProvider extends ServiceProvider {
// find this array near the top, and add this in
protected $listen = [
UserHasRegistered::class => [
SendUserWelcomeEmail::class,
],
];
// ...
}
Now we just need to raise the event - see my other post about Model Observers. First off you'll need to import Event and App\Events\UserHasRegistered, then in your created method, just call Event::fire( new UserHasRegistered( $user ) ).
What I ended up doing, since I do need to do more than just one operation on the user creation is having another function for user creations.
User model
/**
* Create a new user instance after a valid registration.
*
* #param array $attributes
* #param null $role
* #param bool $send_activation_email
*
* #return User $user
*
* #internal param array $args
*/
public function createNew(array $attributes, $role = null, $send_activation_email = true)
{
$this->name = $attributes['name'];
$this->company_id = $attributes['company_id'];
$this->email = $attributes['email'];
$this->password = bcrypt($attributes['password']);
$this->save();
if (isset($role)) {
// Assigning the role to the new user
$this->attachRole($role);
}
//If the activation email flag is ok, we send the email
if ($send_activation_email) {
$this->sendAccountActivationEmail();
}
return $this;
}
and calling it like:
User Controller
$user = new User();
$user->createNew($request->all(), $request->role);
It might not be the best solution, but it does the job, and it's future prof, so if the logic on the user creation grows can be implemented aswell.

Best place to store an array in Laravel

I'm using Laravel 5.2 and my app has the need for select boxes with half-hourly times in various different views throughout.
Here's an example of how the array data looks:
[
'00:00' => '00:00',
'00:30' => '00:30',
'01:00' => '01:00',
/* ... etc ... */
'18:00' => '18:00',
'18:30' => '18:30',
'19:00' => '19:00',
/* ... etc ... */
]
Obviously I don't want to put this in each controller that calls each view, but I'm wondering what the best way of storing it would be. So far, I've thought of the following:
1) A helper function that returns the array
2) A config file in app/config that contains the array
3) A database model (seems excessive)
4) A function that generates the list each time (performance concern?)
Can anyone think of a better way or suggest which one of the ways above would be best and why?
I have found a very good solution to this on Laracasts at https://laracasts.com/series/build-project-flyer-with-me/episodes/5
Here are the steps:
1) Create a file app/Http/Utilities.php:
<?php
namespace App\Http\Utilities;
class Time
{
protected static $times = [
'00:00' => '00:00', '00:30' => '00:30', '01:00' => '01:00'
/** etc ... **/
'22:30' => '22:30', '23:00' => '23:00', '23:30' => '23:30'
];
public static function all()
{
return static::$times;
}
2) In the view, add the following at the top:
#inject ('times','App\Http\Utilities\Time')
3) $times::all() can now be used within the view wherever necessary.
You can use Form Macro with Laravel Collective Package
Laravel Collective: https://github.com/LaravelCollective/html
An awesome example you can find https://github.com/rappasoft/laravel-5-boilerplate
See App\Providers\MacroServiceProvider.php
From my understanding of the question, this would be a perfect situation for View Composers, as it seems like you have some data that has to be generated on multiple views and/or in multiple controller methods.
The way I go about doing this:
Start by running php artisan make:provider ComposerProvider in your terminal.
This is what that provider could look like:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
view()->composer(
[
'view_one'
'view_two'
], 'App\Http\ViewComposers\DashboardComposer'
);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
There you can list all the views that need this data.
An example of how a View Composer could look like. I place these in the folder app/Http/ViewComposers, as the namespace suggests.
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use Illuminate\Users\Repository as UserRepository;
class DashboardComposer
{
public function __construct()
{
}
public function compose(View $view)
{
$the_variable = 'Hello World';
$data = [
'some_very_important_variable' => $the_variable
];
$view->with($data);
}
}

Only use translations from messages.php in Laravel 5

I would like to load all our translations into a flat array in resources/lang/en/messages.php and resources/lang/fr/messages.php
I would like to translate them in the view simply with trans('key') rather than trans('file.key')
Anyway to enable this behaviour? Seems it does not come out of the box. Thanks.
Figured out how, first define your own provider in App\Providers. Have it use your own custom class instead.
<?php namespace App\Providers;
use App\Utilities\TranslationUtility;
use Illuminate\Translation\TranslationServiceProvider;
class SimpleTranslationProvider extends TranslationServiceProvider {
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->registerLoader();
$this->app->singleton('translator', function($app)
{
$loader = $app['translation.loader'];
$locale = $app['config']['app.locale'];
$trans = new TranslationUtility($loader, $locale);
$trans->setFallback($app['config']['app.fallback_locale']);
return $trans;
});
}
}
Custom class:
<?php namespace App\Utilities;
use Illuminate\Translation\Translator;
class TranslationUtility extends Translator {
public function get($key, array $replace = array(), $locale = NULL)
{
return parent::get('messages.' . $key);
}
}
Then add your service provider in config/app.php instead of 'Illuminate\Translation\TranslationServiceProvider'

Resources