Target [App\Repositories\Setting\SettingRepositoryContract] is not instantiable while building [App\Http\Controllers\SettingsController] - laravel-5

How can i solve Target [App\Repositories\Setting\SettingRepositoryContract] is not instantiable while building [App\Http\Controllers\SettingsController].
Here is my controller
namespace App\Http\Controllers;
use Auth;
use Session;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Repositories\Setting\SettingRepositoryContract;
class SettingsController extends Controller
{
protected $settings;
public function __construct(
SettingRepositoryContract $settings
)
{
$this->settings = $settings;
}
SettingRepositoryContract
namespace App\Repositories\Setting;
interface SettingRepositoryContract
{
public function getCompanyName();
public function updateOverall($requestData);
public function getSetting();
}
NOTE: I am new to laravel and can't understand this error. So please help me if anyone know the answer.
Thank you

You need to do it right way:
SettingsRepositoryInterface
namespace App\Repositories;
interface SettingsRepositoryInterface
{
public function getCompanyName();
public function updateOverall($requestData);
public function getSetting();
}
Implement the interface SettingsRepository
namespace App\Repositories;
use App\Repositories\SettingsRepositoryInterface;
class SettingsRepository implements SettingsRepositoryInterface
{
public function getCompanyName()
{
// your code here
}
// all other functions here
}
Create a new service provider SettingsRepositoryServiceProvider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class SettingsRepositoryServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(
'App\Repositories\SettingsRepositoryInterface',
'App\Repositories\SettingsRepository');
}
}
Register your service provider by adding it to config/app.php
App\Providers\SettingsRepositoryServiceProvider::class,
And finally in your controller
namespace App\Http\Controllers;
use Auth;
use Session;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Repositories\SettingsRepositoryInterface;
class SettingsController extends Controller
{
protected $settings;
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
}
For your reference:
https://laravel.com/docs/6.x/container &
https://laravel.com/docs/6.x/providers
Modify namespace according to your needs.

Related

How to pass data from database to Mailable?

I know how to pass data to view but how do you pass data to a mailable? I'm also a bit confused on where to put the database call DB::select, in the controller or the mailable? So far I've tried this:
web.php:
Route::get('/test-email', 'App\Http\Controllers\TestMailController#test');
TestMailController.php:
<?php
namespace App\Http\Controllers;
use App\Mail\ContactMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class TestMailController extends Controller
{
public function test()
{
return new ContactMail();
}
}
TestMail.php:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
class TestMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $data;
public function __construct()
{
$this->data = DB::select('select * from db_name');
}
public function build()
{
return $this->markdown('emails.testmail')->with('data', $data);
}
}
You need to use $this :
public function build()
{
return $this->markdown('emails.testmail')->with('data', $this->data);
}

Cannot access model using controller in Laravel

I create new controller and models using php artisan command. And I tried to access model using controller but I get Class 'Thankful' not found error.
Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Thankful;
class Thanks extends Controller
{
public function hello(){
$my_data = new Thankful;
$data = $my_data->my_model();
return view("thanks",compact('data'));
}
}
Model Code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Thankful extends Model
{
public function my_model()
{
$data = "Thank You";
return $data;
}
}
The namespace of your Thankful model is incorrect:
<?php
namespace App\Http\Controllers;
use App\Thankful;
use Illuminate\Http\Request;
class Thanks extends Controller
{
public function hello ()
{
//
}
}

Laravel repository Class App\Repository\User does not exist

Hello I want to use my own repository class in my Laravel 5.8 project
I created my file Repository in the App File and in this file I added A class called ConversationRepository
This is my class:
<?php
namespace App\Repository;
class ConversationRepository{
private $user;
public function __construct(User $user){
$this->user=$user;
}
public function getConversation(int $userId){
return $this->user->newQuery()
->select('name','id')
->where('id','!=',$userId)
->get();
}
}
And then when I use it on my controller :
<?php
namespace App\Http\Controllers;
use App\User;
use Auth;
use Illuminate\Http\Request;
use App\Repository\ConversationRepository;
class ConversationsController extends Controller
{
private $r;
private $auth;
public function __construct(ConversationRepository $conversationRepository,AuthManager $auth){
$this->r = $conversationRepository;
$this->auth = $auth;
}
public function index(){
return view('conversation.index',[
'users'=>$this->r->getConversation($this->auth->user()->id)
]);
}
public function show(User $user){
return view('conversation.show',['users'=>$this->r->getConversation(
$this->auth->user()->id),
'user'=>$user
]);
}
public function store(User $user){
}
}
I get the error
Class App\Repository\User does not exist
Apparently, you forgot to add use App\User; in the class ConversationsController file.

Multilevel Relationship in Laravel 5.1

I have three tables and i want to make multiple relationship between then
orders,order_details,book
I want to make a common relation between then to take values in a single array .I am using following Code
Order.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
public $timestamps = false;
protected $table='orders';
public function orderDetails(){
return $this->hasMany('App\OrderDetail','order_id');
}
}
OrderDetail.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderDetail extends Model
{
//
public $timestamps = false;
protected $table='order_details';
public function orders(){
return $this->belongsToMany('App\Order');
}
public function book(){
return $this->hasMany('App\Book');
}
}
and Book.php
public function orderDetail(){
return $this->hasManyThrough('App\OrderDetail','App\Book','id','item');
}
where item is foreign key and primary key in book table
in OrderController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Order,App\OrderDetail,App\Book,App\User;
use Auth;
use App\Http\Requests;
use Response;
use App\Http\Controllers\Controller;
class OrderController extends Controller
{
public function userOrders(){OrderDetail::find($orderId)->where('order_id',$orderId)->where('item','book.id')->get(); }

Call to a member function save() on a non-object

I am building a small laravel app beginning level. I got error in code in I couldn't solve out.
Here is the code
post model
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
public function user()
{
return $this->belongsTo('app\user');
}
}
user model
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
$this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
Thank you in advance for any try from you to solve this query.
Make sure inside your user model you're returning the relationship.
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('app\post');
}
}
Your posted code doesn't have a return inside the posts function.
i think this should fix
add your table field to protected $fillable = [] array
user
namespace App;
use Illuminate\Database\Eloquent\Model;
class post extends Model
{
protected $fillable = ['body'];
public function user()
{
return $this->belongsTo('app\user');
}
}
post
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class user extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
protected $fillable = ['username', 'field2', 'blabla'];
public function posts()
{
return $this->hasMany('app\post');
}
}
postcontroller
namespace App\Http\Controllers;
use App\post;
use Illuminate\Http\Request;
class postcontroller extends Controller{
public function postCreatePost(Request $request){
$post = new post(['body' => $request->body]);
$request->user->posts()->save($post);
return redirect()->route('dashboard');
}
}

Resources