In my Laravel-5.8, I am trying to use ViewComposers so that I can display data in layouts\header
App\http\View\Composers\NotificationsComposer
<?php
namespace App\http\View\Composers;
use App\Models\Notification\UserNotification;
use Illuminate\View\View;
use Illuminate\Support\Facades\Auth;
class NotificationsComposer
{
public function compose(View $view)
{
$userCompany = Auth::user()->company_id;
$userID = Auth::user()->id;
$countUnreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->count();
$unreadNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->take(5)->get();
$allNotifications = UserNotification::where('send_to', $userID)->where('company_id', $userCompany)->orderBy('created_at', 'desc')->get();
return $view->with([
'countUnreadNotifications' => $countUnreadNotifications,
'unreadNotifications ' => $unreadNotifications,
'allNotifications ' => $allNotifications
]);
}
AppServiceProvider
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\Models\Notification\UserNotification;
use App\Http\View\Composers\NotificationsComposer;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer(['layouts.header'], NotificationsComposer::class);
}
}
layouts\header.blade
<span class="dropdown-item dropdown-header">You have {{ $countUnreadNotifications }} unread notifications</span>
When I logged in I got this error:
Undefined variable: countUnreadNotifications (View: C:\xampp\htdocs\resources\views\layouts\header.blade.php)
How do I resolve this?
The error is here 'unreadNotifications '. the is a space before the parenthesis. So I removed the space 'unreadNotifications' and everything works fine. Thank you
Related
I've got an error in laravel named
Target class [App\Http\Controllers\PostController] does not exist
The code below is from my PostsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
public function postCreate(Request $request)
{
$id = Auth::id();
$gebrnaam = Auth::name();
$post = new Post();
$image = $request->file('image')->getClientOriginalName();
$post->image_path = $image;
$request->image->move(public_path('img'), $image);
$post->Titel = $request->input('txtTitle');
$post->Inleiding = $request->input('txtinleiding');
$post->Inhoud = $request->input('txtinhoud');
$post->user_id = $id;
$post->userNaam = $gebrnaam;
$post->save();
}
And this is the code of my web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('/', [PostController::class, 'getpost']);
Route::post('/', [PostController::class, 'postCreate']);
You haven't declared the class in your PostContoller so the Laravel is throwing an exception as it is not able to find the controller class.
class PostController extends Controller
{
//
}
Wrap your code in class PostController so as follows:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class PostController extends Controller{
public function postCreate(Request $request)
{
$id = Auth::id();
$gebrnaam = Auth::name();
$post = new Post();
$image = $request->file('image')->getClientOriginalName();
$post->image_path = $image;
$request->image->move(public_path('img'), $image);
$post->Titel = $request->input('txtTitle');
$post->Inleiding = $request->input('txtinleiding');
$post->Inhoud = $request->input('txtinhoud');
$post->user_id = $id;
$post->userNaam = $gebrnaam;
$post->save();
}
}
For reference: https://laravel.com/docs/8.x/controllers#basic-controllers
I want to pass $defaultFrom from NewsletterController.php:
<?php
namespace App\Http\Controllers;
use App\Mail\NewsletterMail;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class NewsletterController extends Controller
{
public function send()
{
$defaultFrom = 'newsletter#stuttard.de';
DB::table('newsletter_mails')->insert(['from' => $defaultFrom]);
$emails = DB::select('select * from newsletters order by id desc');
foreach ($emails as $email) {
Mail::to($email)->send(new NewsletterMail());
}
}
}
to NewsletterMail.php:
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
class NewsletterMail extends Resource
{
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('From', 'from')->default($defaultFrom)->placeholder($defaultFrom),
];
}
}
I've tried to put public $defaultFrom; above the fields() function or call new NewsletterMail($defaultFrom) but this seems to be wrong syntax. Sorry, I'm a bit new to Laravel.
I assume that you have Newsletter model. Move $defaultFrom to model as public const DEFAULT_FROM = 'newsletter#stuttard.de';. After doing this, you can call it's value in both places using Newsletter::DEFAULT_FROM.
I added migrate in database, but why getting error I don't know, I am pretty new to Laravel, I don't know how can I fix this.
Error is:
Undefined variable: title
My code:
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$aboutus = new Abouts();
$aboutus->$title = $request->input('title');
$aboutus->$subtitle = $request->input('subtitle');
$aboutus->$description = $request->input('description');
$aboutus->save();
return redirect('/abouts')->with('success','nice');
}
}
you can write this way also.
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$input = $request->all();
Abouts::create($input); //here About us your model
return redirect('/abouts')->with('success','nice');
}
}
MODEL:
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class product extends Model
{
public function create(Request $request) {
$file = $request->file('photo');
if ( $request->hasFile('photo') && $request->file('photo')->isValid() )
{
$extension = $file->extension();
$name = 'bjdsakbhdebkhdabhkedbhe'.$extension;
$path = $file->storeAs('public/images',$name);
}
else {
return 'error';
}
product::create([
'photo' => $path,
]);
}
protected $fillable = ['name', 'price', 'roast', 'origin', 'photo', 'stock'];
}
CONTROLLER
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\product;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
class adminController extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function create(Request $request) {
ini_set('max_execution_time', 300);
$validatedData = $request->validate([
'photo' => 'required|file|image'
]);
$new = new product;
$new->create($request);
}
}
I am trying to upload a file image. I have reworked the above code several times and an error is thrown. Absolutely NO idea why the file is not uploading. It is not a server error. File size and time allowed have been adjusted.
Why are you calling product::create inside your create method in product class? This causes an infinite recursion.
I have ChatController located in app/http/controllers like so:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use DB;
class ChatController extends Controller implements MessageComponentInterface {
protected $clients;
function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
foreach ($this->clients as $client)
{
if ($client !== $conn )
$client->send($msg);
DB::table('messages')->insert(
['message' => $msg]
);
}
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo 'the following error occured: ' . $e->getMessage();
$conn->close();
}
}
And I have chatserver.php file in the root like so:
<?php
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Http\Controllers\ChatController;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ChatController()
)
),
8080
);
$server->run();
If I remove
DB::table('messages')->insert(
['message' => $msg]
);
from the ChatController and start chatserver.php it works, but if I don't remove it then the server starts but as soon as I send a message I get this error:
Fatal error: Uncaught Error: Class 'DB' not found in C:\wamp\www\laraveltesting\app\Http\Controllers\ChatController.php:31
Why won't it use DB? I am extending the laravel controller.
This one is better
use Illuminate\Support\Facades\DB;
Or you can use a slash('/') before DB like below
/DB::table('messages')->insert(
['message' => $msg]
);
As previously advised First use
use Illuminate\Support\Facades\DB;
then go /bootstrap/app.php and uncomment
$app->withFacades();
try using this
use Illuminate\Support\Facades\DB;
instead of
use DB;
for Laravel 5 and up simply just use this
use DB;
instead of
use Illuminate\Support\Facades\DB;
which is use for Laravel 4 version
change use Illuminate\Support\Facades\DB; to Use DB;