Laravel 6.x Backpack NewsCrud Plugin - php

What I want to do is allow the admin panel to post news using newscrud. I am confused in how would I grab the information from the database and show the user the articles.
I am having trouble using the newscrud plugin for laravel. I quite don't understand how to use this plugin. I have installed it using composer. All the controllers, models etc. are in the vendor folder in my Laravel project.
Attempts:
What I tried to do is make a function in the
laravel-project/vendor/backpack/newscrud/src/app/Http/Controllers/Admin/ArticleCrudController.php file:
public function index()
{
# Pass the article database information in articles var
$articles = Articles::all();
# Return this variable to blog page
return view ('blog')->with('articles', $articles);
}
And in the routes/web.php file:
Route::get('/blog', 'ArticleCrudController#index')->name('blog');
Front-end code
#foreach($articles as $article)
<div class="col-md-12 d-flex ftco-animate">
<div class="blog-entry align-self-stretch d-md-flex">
<a href="blog-single.html" class="block-20" style="background-image: url('images/image_6.jpg');">
</a>
<div class="text d-block pl-md-4">
<div class="meta mb-3">
<div>July 20, 2019</div>
<div>Admin</div>
<div><span class="icon-chat"></span> 3</div>
</div>
<h3 class="heading">Even the all-powerful Pointing has no control about the blind texts</h3>
<p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
<p>Read more</p>
</div>
</div>
</div>
#endforeach
Error
The error message I get is:
Target class [App\Http\Controllers\ArticleCrudController] does not exist.
If you need information/screenshots I am willing to provide them.

When using composer in PHP, you should never make modifications to files inside the /vendor/ folder. Because as soon as you run composer update, they will get overwritten.
If I understand correctly, What you’re trying to do has little to do with Backpack. You should not overwrite the package in the vendor folder. You should create a controller in your app folder, with a routes in your routes folder, line you would normally do in Laravel. Just make sure that, when you reference the model, you reference Backpack\NewsCRUD\app\Models\Article instead of App\Models\Article.
Hope it helps!

The error message
App\Http\Controllers\ArticleCrudController
show that Laravel is not able to resolve the controller in the PHP namespace App\Http\Controllers.
I just looked into the library's code and I suggest you to replace
Route::get('/blog', 'ArticleCrudController#index')->name('blog');
by
Route::get('/blog', 'Backpack\\NewsCRUD\\app\\Http\\Controllers\\Admin\\ArticleCrudController#index')->name('blog');

Try to edit your web.php like this:
Route::group([
'prefix' => '/',
'middleware' => ['web'],
// This is all the namespace in directory
'namespace' => 'laravel-project\vendor\backpack\newscrud\src\app\Http\Controllers\Admin',
], function () {
Route::get('/blog', 'ArticleCrudController#index')->name('blog');
});
And edit your ArticleCrudController.php file to:
namespace laravel-project\vendor\backpack\newscrud\src\app\Http\Controllers\Admin;
Hope this can help :)

Related

Laravel 8.15.0/Jetstream - How to register new blades x-jet-newblade?

I am just doing my very first steps with Laravel 8 and found a problem that I can not solve.
/var/www/html/laravel/resources/views/dashboard.blade.php:
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<x-jet-welcome />
</div>
If i create a new blade in the same directory (f.e. the form.blade.php) with the same code as above but with <x-jet-subform/> instead of <x-jet-welcome> it should normally redirect to the subform.blade.php which is located under var/www/html/laravel/resources/views/vendor/jetstream/components/subform.blade.php
But if I try to get to that page (after setting a Route at web.php) it says
InvalidArgumentException
Unable to locate a class or view for component [jet-subform].
So I think it's necessary to "register" new blades but I found no way to do that...
The view is already published with
php artisan vendor:publish --tag=jetstream-views
You can register your jetstream blade components in App\Providers\JetstreamServiceProvider.php located in app\Providers folder.
Add the following helper function to the file:
protected function registerComponent(string $component) {
\Illuminate\Support\Facades\Blade::component('jetstream::components.'.$component, 'jet-'.$component);
}
Then use the following snippet in register function to register your jetstream blade components:
public function register() {
$this->registerComponent('subform');
}
Now you can use your custom jetstream component:
<x-jet-subform>
I was dealing with the same problem here and found your question unanswered.
The solution I found was to create my own new Blade component.
You can do that using:
$ php artisan make:component MyComponent
This will create two new files /resources/views/components/my-component.blade.php and /app/View/Components/MyComponent.php.
Now you just need to build your component on that blade file and reference it using the x-tag like this:
<x-my-component></x-my-component>
This is how the blade component code should look like
<button {{ $attributes->merge(['type' => 'button', 'class' => 'some-classes']) }}> {{ $slot }} </button>
Hope it helps. Greetings from Brazil :)
I am not sure whether it's the correct or intended way to add new custom x-jet components here as this method may not survive an update, but you can register new components in this file:
vendor/laravel/jetstream/src/JetstreamServiceProvider.php.
Add, $this->registerComponent('subform'); to the configureComponents method, and then call it with an <x-jet-subform> tag

Replacing Laravel blade files with Vue slowly over time but it's becoming messy

We have a Laravel app that has a nav bar on the main page that links to different areas on the site like this:
<li class="{{ activeLink(['admin/orders', 'admin/orders/open', 'admin/orders/fulfilled']) }}">
<a href="/admin/orders/new">
</li>
and the route is mapped as follows: Route::get('admin/orders/new', 'Admin\OrdersController#new');
OrdersController
class OrdersController extends Controller
{
public function new() {
return view('admin.orders');
}
}
and this is where I removed the previous blade code and inject the Vue entry point:
Orders.blade.php
#extends('layouts.admin')
#section('content')
<div id="app-orders">
</div>
The issue I'm having is that I'm gradually replacing blade files with Vue, I am having to make 'new' Vue apps since there is no common entry point. So right now I am looking to replace the Support page, which means I'll have to add an entry point on the support.blade.php like:
<div id="app-support">
</div>
Ideally, I'd like to obviously replace the whole admin section and use Vue but the CTO wants to do it gradually so I'm wondering if this is the best approach or is there another way I can be doing this?

Laravel No query results for model [App\Topic] create

I'm trying to get a view where the user is able to create a topic. I did this many times in my project but never got this error, Because all of the other ones work just fine.
I get the error No query results for model [App\Topic] create. Here is the code.
This is the link that is supposed to bring the user to the view.
<div class="col s12">
<div class="card">
<div class="card-content clearfix">
New topic <i class="material-icons right">edit</i>
</div>
</div>
</div>
These are the routes that are used in this problem.
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#store')->name('savetopic');
The Controller method create.
dd('Hij is er ' . $id);
And the store method is empty, The link doesn't show the DumpDie method but shows me the error instead. So there is no need to post the view i'm trying to display because that's not where the problem is. Thanks in advance!
You need to understand how routes and controller methods are working
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
When you are hitting the above route, i mean something for example yourdomain/theme/1/topics/create in your browser, then it will take 1 in place of theme_id and it will go to the create method of your TopicsController which will accept one argument.
Your create method should be something like that
public function create($id)
{
dd($id);
}
Here you will get 1 as result, because you have passed this argument in your url instead of theme_id.

404 not found laravel

I am new to Laravel , I am getting 404 not found error when returning view to salary report from my controller. The below mentioned is my function which returns my simple view to salary report.
public function getSalaryReport()
{
return view('Company.salaryReport');
}
the routes.php file ahs route to company controller.
Route::group(['middleware' => 'auth.company'], function () {
Route::get('company/notice-board/create', 'CompanyController#getNoticeBoardCreate');
Route::get('company/notice-board/{id}/edit', 'CompanyController#getNoticeBoardEdit');
Route::get('company/designation/{id}/edit', 'CompanyController#getDesignationEdit');
Route::get('company/all-user/{id}/force', 'CompanyController#getForce');
Route::post('company/all-user/{id}/force', 'CompanyController#postForce');
Route::controller('company', 'CompanyController')
this is my view which i am trying to display from my controller.
#extends('Company.CompanyLayout')
#section('content')
<div>
<ul class="breadcrumb">
<li>
Home <span class="divider">/</span>
</li>
<li>
<a href='{!! URL::to("company/report-summery") !!}'>Summery Report</a>
</li>
</ul>
</div>
#endsection
where i am going wrong and what should be done to make my view visible. Thanks to all in advance.
Route::controller is depricated in the latest versions of Laravel, try not to use it anymore.
You can use Route::resource or create a specific route for your salary report like this:
Route::get('company/salary-report', 'CompanyController#getSalaryReport');
Also make sure that you have resources\views\Company\salaryReport.blade.php as your view.
404 not found is an error because you don't have any routes for the given url. And I didn't find any routes in your example for the function getSalaryReport()
if you want to call this method, at least add this to your routes:
Route::get('company/report-summery', 'CompanyController#getSalaryReport');

Can't establish proper routes in Laravel 4

So sorry to bother y'all, but I was wondering if anyone could help me. I'm having a wee bit of a problem with my code in Laravel, being used to working from scratch rather than with frameworks. I made use of the Laravel Bootstrap Starter Site and I'm trying to add additional pages, but the routing isn't exactly co-operating. It's rather frustrating.
The Controller: app/controller/community/CommunityController.php
<?php
class CommunityController extends BaseController {
public function index() {
return View::make('community.index');
}
}
?>
The View
#extends('site.layouts.default')
{{-- Content --}}
#section('content')
#foreach ($posts as $post)
<div>
I'm just going to put this here...
</div>
#endforeach
{{ $posts->links() }}
#stop
And finally, last but not least, my routes.
Route::get('community', array(
'uses' => 'CommunityController#index',
'as' => 'community.index'
));
Now, I have this nagging feeling that I'm missing something rather small, but for the life of me I can't figure it out. If anyone would be so kind to explain what I'm doing wrong, I'd appreciate it. Especially since I can prevent this kind of problem happening in the future as well.
With friendly regards,
User who still hasn't picked out a good name
Edit: Sorry I forgot to mention this. I removed public, so I don't know if that influences anything. If it does, again, sorry for forgetting to mention this in the beginning.
you can try to bind the whole route to the controller, using
Route::controller('community', 'CommunityController');
then in your controller you have to prefix the controller methods with HTTP verbs.
Your index() method will be
public function getIndex() {
return View::make('community.index');
}
Just fire composer dump-autoload in root of your project folder from terminal / console.
It'll load your controller which is in subfolder.

Categories