Do you guys have a smart and easy way to make crud in laravel framework?
Hot to make crud in laravel easy And fast,i tery step step in official website laravel but i do not understand.
Please let me know the easy steps i understands Thanks.
Do you guys have a smart and easy way to make crud in laravel framework?
Hot to make crud in laravel easy And fast,i tery step step in official website laravel but i do not understand. Please let me know the easy steps i understands Thanks.
I have the small totarial, this can help you !
//////////// FUNDAMENTAL ////////////
Create LARAVEL Project
composer create-project --prefer-dist laravel/laravel Airport
Create Database in PhpMyAdmin
Open project file in cmd / powerShell
Create Table with php artisan
php artisan make:migration create_flight_table
Open your text editor and edit .env
DB_CONNECTION=mysql
DB_HOST=localhost
DB_DATABASE=airport
DB_USERNAME=root
DB_PASSWORD=
Go to folder database>migration , delete user and pasword table ,open flight_table file and edit
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
public function down()
{
Schema::drop('flights');
}
Migrate the table in cmd or powerShell
php artisan migrate
Check your table in PhpMyAdmin
==============================================================================
//////////// MODEL VIEW CONTROLLER ////////////
Create Model in cmd or powerShell
php artisan make:model Tower
protected $table = 'flights';
Create View in folder resources>views
create new folder called hangar
create index.blade.php
create edit.blade.php
create create.blade.php
Create Controller in cmd or powerShell
php artisan make:controller flightController --resource
Open and edit your controller
use App\model;
public function index()
{
$vars = Tower::all();
return view('hangar.index',['var' => $vars]);
}
Go to folder routes, Open and edit web.php
Route::resource('main', 'flightController');
Insert data in PhpMyAdmin
Open and edit your index.blade
VIEW
CREATE
#foreach($var as $var)
<p> {{ $var->name}} </p>
<p> {{ $var->airline}} </p>
{{ date('F d, Y', strtotime($var->created_at))}}<br><br>
<hr>
#endforeach
Run this command in cmd or powerShell
php artisan serve
copy paste this
http://127.0.0.1:8000/main
//////////// CRUD ////////////
//////////// CREATE ////////////
Open and edit your controller
public function create()
{
return view('hangar.create');
}
Open and edit create.blade.php
#if(count($errors)>0)
<ul>
#foreach($errors->all() as $error)
<li>
{{$error}}
</li>
#endforeach
</ul>
#endif
CREATE
Open and edit controller
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required', 'airline' => 'required',
]);
$var = new asd;
$var->name = $request->name;
$var->airline = $request->airline;
$var->save();
return redirect('main');
}
==============================================================================
//////////// UPDATE ////////////
Open and edit controller
public function edit($id)
{
$var = Tower::find($id);
if(!$var){
abort(404);
}
return view('hangar.edit')->with('var', $var);
}
Open and edit edit.blade
#if(count($errors)>0)
<ul>
#foreach($errors->all() as $error)
<li>
{{$error}}
</li>
#endforeach
</ul>
#endif
EDIT
id}}" method="post">
plane}}" placeholder="plane">
airline}}"
placeholder="airline">
Open and edit controller
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required', 'airline' => 'required',
]);
$var = Tower::find($id);
$var ->name = $request->name;
$var ->airline = $request->airline;
$var ->save();
return redirect('main');
}
Open and edit index.blade
id}}/edit"> EDIT
==============================================================================
//////////// DELETE ////////////
Open and edit Controller
public function destroy($id)
{
$var = Tower::find($id);
$var ->delete();
return redirect('main');
}
Open and edit index.blade
id}}" method="post">
//////////// FINISH ////////////
Related
enter image description here
here's my code below
public function store(Request $request)
{
$validateData = $request->validate([
'pembelian_id' => '',
'pconfirm_pic' => 'image|file|max:5087'
]);
// $validatedData['pconfirm_pic'] = $request->file('pconfirm_pic')->store('payment_confirmation','public');
Payconfirm::create($validateData);
return redirect('/user/purchase/?status_pembayaran=1');
}
match with path where controller store to
Run below artisan command
php artisan storage:link
Read More About
In production mode (on the server), there is an error on my site in the Chrome console:
Uncaught (in promise) Error: Page not found: ./Pages/Posts/Show.vue
Also, the Dashboard page isn't updating to changes with text and a new pagination table I've introduced in local development.
Everything works fine locally, but pushing to Digital Ocean Server doesn't show the latest changes.
I checked the source code online and they raw code is there. I can see the changes in text, the pagination table and the new routes. But they are not showing when I load the website. I suspect something to do with the cache or the build process?
I have done:
php artisan cache:clear
php artisan config:clear
php artisan view:clear
npm run build (new vite build of assets)
Can anyone help?
Shared Files:
resources/js/app.js
import './bootstrap';
import '../css/app.css';
import { createApp, h } from 'vue';
import { createInertiaApp } from '#inertiajs/inertia-vue3';
import { InertiaProgress } from '#inertiajs/progress';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';
const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';
createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
setup({ el, app, props, plugin }) {
return createApp({ render: () => h(app, props) })
.use(plugin)
.use(ZiggyVue, Ziggy)
.mount(el);
},
});
InertiaProgress.init({ color: '#4B5563' });
Post Controller
<?php
namespace App\Http\Controllers\Post;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\Post;
class PostController extends Controller
{
/**
* Display all posts
*
* #return \Inertia\Response
*/
public function index(Request $request)
{
$posts = Post::paginate(10);
return Inertia::render('Dashboard', ['posts' => $posts]);
}
/**
* Display a post
*
* #return \Inertia\Response
*/
public function show(Request $request, $id)
{
$post = Post::findOrFail($id);
return Inertia::render('Posts/Show', ['post' => $post]);
}
}
This was an docker/nginx issue. The files generated by the app weren't routing correctly and so the static files from the original build weren't being replaced.
I changed to using volumes to sync data between the containers and it worked.
Everything was working fine before I installed a package https://github.com/kreait/firebase-php.
Now I get 404 error on adding new Routes.
Old Routes are working fine, but new routes are not working
Web.php
//old routes
Route::get('/', function () {
return view('welcome'); //working
});
Route::get('email', 'EmailController#sendEmail'); /working
Route::get('/counter',function() {
$counter = Redis::incr('counter'); //working
return $counter;
});
//new routes
//Firebase admin SDK
Route::get("fire","FirebaseSdkController#fire");
Route::get("say-hello", function(){
return "hello";
});
FirebaseSdkController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Messaging\CloudMessage;
class FirebaseSdkController extends Controller
{
public function fire()
{
$firebase = (new Firebase\Factory())->create();
$messaging = $firebase->getMessaging();
$topic = 'News';
$message = CloudMessage::withTarget('topic', $topic)
->withNotification("Hello") // optional
// ->withData($data) // optional
;
$message = CloudMessage::fromArray([
'topic' => $topic,
'notification' => [/* Notification data as array */], // optional
'data' => [/* data array */], // optional
]);
$messaging->send($message);
}
}
This is what I have tried so far -
php artisan route:list
shows the new routes in the list
php artisan route:clear
php artisan config:clear
composer dump-autoload
None of them solved the issue.
Update
Now, if I delete any route and do php artisan route:clear, I still can access the route. I don't know what's happening please help.
I am a laravel newbie and I'm trying to do the basic task list lesson in the laravel 5.1 docs and I get this error: NotFoundHttpException in RouteCollection.php line 161.
This is the lesson I'm trying to do:
https://laravel.com/docs/5.1/quickstart
I did exactly what the lesson said, copied everything line by line and I still don't know why this is happening.
These are my routes. I have a feeling that my error is somewhere there or I might be totally wrong.
use App\Task;
use Illuminate\Http\Request;
Route::group(['middleware' => ['web']], function () {
/**
* Show Task Dashboard
*/
Route::get('/', function () {
return view('tasks', [
'tasks' => Task::orderBy('created_at', 'asc')->get()
]);
});
/**
* Add New Task
*/
Route::post('/task', function (Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
$task = new Task;
$task->name = $request->name;
$task->save();
return redirect('/');
});
/**
* Delete Task
*/
Route::delete('/task/{id}', function ($id) {
Task::findOrFail($id)->delete();
return redirect('/');
});
});
And this is where I go to the browser and get the error: http://localhost:8000/tasks
Maybe my URL in my browser is wrong
I've also tried with localhost:8000/laravel-uni-project/public/tasks
As I mentioned above I copied everything from the tutorial exactly as it said, so I may have an error somewhere else in my application.
p.s. the tutorial is for laravel 5.1 and I'm using 5.1 as well.
Thank you!
you are browsing task,but in your routes file there is not route for task get request,may be that will your task get request.
Route::get('/tasks', function () {
return view('tasks', [
'tasks' => Task::orderBy('created_at', 'asc')->get()
]);
});
Session message is not working i tried this code and many fix available online
Here id my store function `
public function store(Request $request)
{
// dd($request->all());
$this->validate($request, [
'name' =>'required',
'username' =>'required',
'email' =>'required',
'address' =>'required',
'likes' =>'required',
'gender' =>'required'
]);
$input = $request->all();
Contacts::create($input);
Session::put('flash_message', 'Task successfully added!');
return redirect()->back();
}
And Retrieving by this code
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
I resolved issue with laravel 6.x
As soon as I moved \Illuminate\Session\Middleware\StartSession::class and \Illuminate\View\Middleware\ShareErrorsFromSession::class from web $middlewareGroups to $middleware in app\Http\Kernel.php everything started working as expected.
I Resolved the Issue with laravel 5.2.
I was having route like this
Route::group(['middleware' => [ 'web','auth']], function () {
.......
}
So Removed the web middle ware
Route::group(['middleware' => ['auth']], function () {
.......
}
and its start working
Analysis: By default Laravel Add web Middleware.
check by php artisan route:list it shows web, web,auth .
so by defining it again redirect two time for the web middleware.
I RESOLVED the issue with laravel 5.2.
I was having all the routes inside this:
Route::group(['middleware' => 'web'], function() {
I remove it because when I used the command php artisan route:list in the Name Middleware column the "web" shows to times: web, web.
If you have AUTH replace by:
Route::group(['middleware' => 'auth'], function() {
Also I delete a duplicate route (in routes.php). Now I just have:
Route::resource('/publicaciones', 'PublicacionesController');
My controller:
return redirect()->back()->with('success', 'Saved!');
My view:
#if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
#endif
have you include the following namespace
use Session;
instead of the following code
Session::put('flash_message', 'Task successfully added!');
use
Session::flash('flash_message', 'Task successfully added!');
in instead to
return redirect()->back();
try using
return redirect()->route('your route');
When the validation fails, no further code is executed and the previous page is loaded again. This is the reason why session message is not working. In order to check for any validation errors use the following code snippet at the top of your blade file.
#if ($errors->any())
#foreach ($errors->all() as $error)
<div class="alert alert-danger alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $error }}</strong>
</div>
#endforeach
#endif
A bit late for this forum. I encounter this problem, I've been different sites searching for the right solution but none works. Here's my case, I'm using v6.0, and I put the route inside routes\api.php.
I think there is difference of putting the route to the right place or file, can't explain more.
Here's how I solved, I transfer the route from routes\api.php to routes\web.php and thats it after so many researching I now successfully display the flash message.
Try this code
Session::flash('flash_message', 'Task successfully added!');
Session::save();
return redirect()->back();
This worked for me
This will work in case "Session" fails to display errors in your blade view.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif