Creating a data import page for Laravel Voyager - php

I am using Voyager for a basic BREAD admin to a small web application I am building for a small non-profit. Yearly they need to import 300-500 new semi-complex entries into the database from Excel, so I want to build an admin script that will store all the data in the right places automatically.
Is there a structured way to add a custom controller/view to Voyager?
(I have not found such documentation yet, maybe I am blind. So I have started manually extending existing bits of Voyager, but as I get deeper I want to make sure this is the best option for future growth.)

Yes, You can add custom controllers to voyager.
First let's make a controller: php artisan make:controller ExportController
//app/Http/Controllers/ExportController.php
class ExportController extends Controller{
public function form(){
return view('export.form');
}
public function submit(){
// do export stuff
}
}
Add two routes as below:
//routes/web.php
Route::group(['prefix' => 'admin','as' => 'voyager.', 'middleware' => 'admin.user'], function()
{
Route::get('export','ExportController#form')->name('export.form');
Route::post('export','ExportController#submit')->name('export.submit');
});
then make the related view file at resources/views/export/form.blade.php just note you need to #extends('voyager::master')
make a new menu item using Voyager's menu builder

you must use this package. here you can know how to import excel or csv file into database table and export or download in different format using maatwebsite package with the power of PHPOffice's PHPExcel.
and here is video.

Related

Laravel Inertia rendering data from backend to frontend

Totally new to using reactJS with laravel inertia.
I am trying to render the data from my database to the front end. I use a controller to get the data from the database by using...
// Show all main categories
public static function index() {
return MainCategory::all(['main_category_id', 'main_category_name']);
}
then passing it to the frontend using the web.php by using the following code.
Route::get('/', function () {
return Inertia::render('Admin/Categories', [
'categories' => MainCategoryController::index()
]);
})->name('admin.category-setup');
I currently do not know how to call categories in the front end using reactjs.
How can I do that?
I sometimes approach the accessing of passed props by using Inertia's inbuilt usePage-Hook for React. This comes in handy when you are working with shared data that you have made accessible for all of the frontend components (https://inertiajs.com/shared-data). As long as your Backend (Laravel) Code works properly and the actual categories-Property is handed over to the Categories-Component or shared for all of the frontend, you could do something like this:
import React from 'react';
import { usePage } from '#inertiajs/inertia-react';
function App() {
const categories = usePage().props.categories;
return (
<ul>
{categories.map((category) =>
(<li>{category}</li>))}
</ul>
);
}
The most common practice for me however is to simply give the same variable names that you pass in Laravel into the Inertia-Component into the React-Component-Props like this. To me it looks like that should perfectly work with your Use-Case:
import React from 'react';
function App({categories}) {
return (
<ul>
{categories.map((category) =>
(<li>{category}</li>))}
</ul>
);
}
If you want to find more about Inertia, I highly recommend to go through the docs. The developers did a great job in explaining everything so that an unexperienced Laravel and React Dev could understand what is going on. It is actually not much to read but shows some interesting features:
https://inertiajs.com/

How to organize views in Laravel breeze?

This may be a dumb question, but I've started exploring Laravel Breeze with vue and I'm not sure how to manage resources and their views. (Using Laravel 9)
For example, if I have a table 'members' and a resource controller 'MemberController', ordinarily in the web.php file I would do something like:
Route::resource('members', MemberController::class)->names('members');
Then I'd create a 'member' folder in my resource/views folder to store the views for that table. I could then call the view from the MemberController something like:
public function index()
{
$members = Member::all();
return view('member.index', compact('members'));
}
I'm trying to do something similar using vue in breeze, so I guess I'd have the vue pages in the 'resources/js/Pages' folder. But this didn't seem to work properly.
I don't know how to transition from the previous approach of handling resources and views to Breeze using vue.
What's the recommended way to handle resources in an SPA with Breeze/vue?
After following Djave's suggestion, I took a look at the sample app from inertia.js and that answered everything.
The problem I had was due to using the dot notation when specifying the path to the vue page. So, in my example above, it should instead be like this:
return view('Member/Index', compact('members'));
The inertia sample app code has a lot of good examples!

How to integrate other pages into laravel pre-built authentication

After reading some tutorials on laravel 5.4 authentication (including the doc),I don't know how to work with it in my file.
I have been able to run the artisan command.. php artisan make:auth. Have seen the the controller, views etc that was created and even have accessed it by going to http://localhost/blogsite/public/register (don't worry about, its on my local disk) but how do I integrate it with with the pages that needs authentication? That I don't know..
Who can put me through how to integrate it with other pages
Many way you can use for this solution.
First Way:
If you load views file from controller just use the following line to your controller.
Suppose my controller name is DashBoardController
public function __construct()
{
$this->middleware('auth');
}
So all of the view you return from DashboardController it will make you auth for you. That means if you return any of view from this controller you must need to log in.
So you need to put this constructor function to all of your Controller from where you return view and need to authenticate the user.
To avoid this constructor funtion to all controller you can use the following
Way using route:
Route::group(['middleware' => 'auth'], function () {
Route::Your_Request_Method('your_url1', 'YourController1');
Route::Your_Request_Method('your_url2', 'YourController2');
});
You can get more way at laravel authentication documentation
Hope you will understand.

Laravel 5.3 make a query to database and pass it all views

I have a some dynamic data that I want to fetch from database and pass it to all views. I tried to make some examples from internet but they seems to be not working in my situation like
public function boot(){}
to which I can't make a database query only pass hardcoded values.
Another method I have tried is a base controller but the view gives me error when accessing the variable
$query = DB::table('users')->first();
\View::share('user', $query);
Anyone have a BaseController based solution will be appreciated and if I missed something please do remind me
According to the docs at https://laravel.com/docs/5.4/views#sharing-data-with-all-views:
Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method.
Again as an example in that site you should add this to one of your service providers:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
if (!app()->runningInConsole()) {
$query = DB::table('users')->first();
\View::share('user', $query);
}
}
}
You can use view composer.
"View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location."
you can find more detail on https://laravel.com/docs/5.3/views#view-composers
It is working from boot :
View::share ('variabelname', DB table first () etc etc)
However i encounter a problem :
If i delete tables or drop tables, then run migration and seed, I got error :
base tabel / view not found.
So i have to comment out that line from App Service Provider before any migrate refresh etc.

How to start a new website project in codeigniter?

I'm really beginner to codeigniter I'm working on CI since last 2 weeks. During this period I have created many views.php files, some controllers.php files and some models.php files
Now I want start a new website project.
What should I do. Should I delete all files of my controllers, views and models, etc., and download another codeigniter and start from the beginning?
You should check the documentation of codeigniter for help but just to give you a quick start ill explain how to create your first codeigniter project.
Installation
1 Download the codeigniter framework from http://ellislab.com/codeigniter
2 upload it in root directory of your website or local apache server directory.
Creating your codeigniter project.
In codeigniter your controller will handle the url requests and load appropriate model and views. So the first step is to create your controller.
1 Creating your controller: go to Applications->controllers and there you will find a built in controller called welcome.php.
This controller loads a view welcome_message.php which is inside Application->views.
You can use this controller or create your own.
To create your own controller create a new php file myfirstcontroller.php and extend a class with same name from CI_Controller.
Note that the name of the file and your class name should be the same. the index function is the default function that will be called when you make a request to the controller
class myfirstcontroller extends CI_Controller {
public function index(){
$this->load->view("myfirstview");
}
}
so when you request this controller through yoursite/index.php/myfirstcontroller
it will load a view called myfirstview.php which will reside inside applications->views.
Go ahead and create this file in applications ->views.
2 To pass data from controller to view you will send an array to the view
class myfirstcontroller extends CI_Controller {
public function index(){
$data['name']="My first application.";
$this->load->view("myfirstview",$data);
}
}
3 You can access this variable in view
echo $name
and it will output your variable
3 you use models you have to create a file inside applications->models and call it from controller and it will return the result in the form of array.
You can look at the documentation for further help.
Hope this helped you to get started with codeigniter.
The user guide is inside your download library.
You can also view it in http://ellislab.com/codeigniter/user-guide/
Good luck!!!
Here is Phil Sturgeon's article on how to do multiple site on one CI instance, in here he explains 2 ways of doing it and describes pros and cons.
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
But in his latest articles he has told what happened to modular separation.
http://philsturgeon.co.uk/blog/2010/03/modular-separation-codeigniter-2

Categories