Darkkell / steps

is a PHP project to learn basics of laravel 10.40.0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

steps

  • is a PHP project to learn basics of laravel

How to start

composer create -project laravel/laravel name-project
php artisan serve

Breeze to auth

composer require laravel/breeze --dev
php artisan  breeze:install blade --dark

Create database

  • on the file .env change the name of DB_DATABASE
  • on DBMS create the database with the same name of before step
  • on the explorer choose run migrations

Lang

composer require laravel-lang/common --dev
  • add support to espanish
php artisan lang:add es
  • or support to fr
php artisan lang:Add fr

note: publish always lang on english for default

php artisan lang:publish

config app.php to lang

  • on 'locale' => 'en' change to 'locale' => 'es'

change welcome.blade.php

  • in this file add lang on login and register
  • for lang use the next sintax:
{{__('name_variable')}}

to add new translation or change translation

  • change the file on lang > language > language.json

use env variables

  • to create env variables add the variable on .env with the next sintax:
NAME_VARIABLE=VALUE
  • to use env variables
env('NAME_VARIABLE', 'DEFAULT_VALUE')

best practice

  • always add on .env.example when you add new line on .env

routing

  • structure:
Route::method('/url', function(){
    return //message or view(nameView);
});
  • endpoint with params
Route::method('/url/{param}', function($variable){
    return 'message'.$variable;
});
  • optional params
Route::method('/url/{param?}', function($variable = dafaultValue){
    return 'message'.$variable;
});
  • redirect
Route::method('/url/{param?}', function($variable){
    if($variable == value){
       return redirect('/url');
    }
    return 'message'.$variable;
});
  • endpoint name
Route::method('/url/{param}', function($variable){
    return 'message'.$variable;
})->name('name.index'); // .index .create .show is a convention
  • redirect to endpoint name
Route::method('/url/{param?}', function($variable){
    if($variable == value){
       return redirect()->route('name.index');
    }
    return 'message'.$variable;
});

or

Route::method('/url/{param?}', function($variable){
    if($variable == value){
       return to_route('name.index');
    }
    return 'message'.$variable;
});
  • only return a view
Route::view('/url', 'view');
//or with name
Route::view('/url', 'view')->name('welcome');
  • view all defined routes
php artisan route:list
  • only on application
php artisan route:list --except-vendor

const Home

  • is a constant route o endpoint to home in this case is '/dashboard'. is ubicate on file RouteServiceProvider

middleware

  • execute something after and before the route is executed

  • how to use:

Route::get('/url', function (){
    return //function, view o something
})->middleware(['beforeMiddleware', 'afterMiddleware'])->name('endpointName')
  • inheritance middleware
Route::middleware('nameMiddleware')->group(function(){
    Route::method('/url', action)->name('endpointName')
})

navigation

  • on file >resources>views>layouts>navigation.blade.php modify navigation links and responsive navigation menu to include new routes

make model to db

  • create migration and controller:
php artisan make:model Name -mrc

execute migrations

  • function up
php artisan migrate
  • function down of the last lote
php artisan migrate:rollback
  • function down of the last #n of migrations
php artisan migrate:rollback --step=numberOfMigrations

what is eloquent (app>Models)

  • is a Laravel ORM

assignable mass on models

  • on app>Models>NameModel.php
class NameModel extends Model
{
    use HasFactory;

    protected $fillable = [
        'fillName1',
        'fillName2'
    ];
}
  • how to use
use App\Models\NameModel;

NameModel::operation([
    'fillName1'=>$variable,
    'fillName2'=>'value' // or num
]);

styles and js

  • on runtime
npm run dev
  • compile styles (Note: Only compile the styles what are when exec the comand)
npm run build

Session flash messages

  • on routes
Route::method('/url', function(){
    session()->flash('status','message');
    return to_route('name.index');
});
Route::method('/url', function(){
    return to_route('name.index')->with('status','message'); //  __('message') to i8n
});
  • on controller πŸ‘ŒπŸΌ
public function nameFunction(){
    session()->flash('status','message');
    return to_route('name.index');
}
public function nameFunction(){
    return to_route('name.index')->with('status','message'); //  __('message') to i8n
}
  • on views
@if(session('status'))
    <div class="">{{ session('status')}}</div>
@endif

controllers

  • on routes
Route::method('/url', [NameController::class, 'function'])->name('name.convention');
  • on controller
public function nameFunction(){
    //all code
    return view('name.convention')  //or to_route('name.convention') //with ->('nameSessionFlashMessage', 'value'); // or __('value') to i8n
}

form validation

  • directive @csrf (cross-site-request-forgery) to prevent csrf vulnerability
<form method="method" action="{{route('name.convention')}}">
    @csrf
</form>
$request->validate([
    'nameToValidate'=>'required', // or rule to validate
    'nameToValidate'=> ['required', 'min:3'] // rules
]);
  • save value if have error validation of form
<textarea name="name"> {{old('name')}} </textarea>

show error msg

  • directive of Blade @dump
@dump($errors) //to see all errors

@dump($errors->get('tweet')) //to see tweet errors
  • directive of Blade @error
@error('name'){{$message}}@enderror
  • error to change style
<a class="@error('name') dark:border-red-300 @enderror">
  • x-input-error of brezee
<x-input-error class="" :messages="$errors->get('name')"

Timezone

  • to change timezone you need go to config/app.php to change attribute timezone

conditional

@if
@endif
  • !if
@unless
@endunless

functions

  • eq -> is equal? response with bool
eq()
  • is -> compare if a model is the actual model
@if(auth()->user()->is(tweet->user))
// or
@if (auth()->user()->id === $tweet->user_id)

Policies // policy

php artisan make:policy NamePolicy --model=Name

vite on production

npm run build # to compile

docs

  • tailwind colors

https://tailwindcss.com/docs/customizing-colors

  • tailwind align

https://tailwindcss.com/docs/text-align

  • laravel eloquent

https://laravel.com/docs/10.x/eloquent#inserts

  • laravel validation rules

https://laravel.com/docs/10.x/validation#available-validation-rules

  • Timezone

https://www.php.net/manual/en/timezones.america.php

errors

2024_01_19_222432_create_tweets_table ........... 30ms FAIL

   Illuminate\Database\QueryException

  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 (Connection: mysql, SQL: alter table `tweets` add constraint `tweets_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete )

  at vendor\laravel\framework\src\Illuminate\Database\Connection.php:822
    818β–•                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    819β–•                 );
    820β–•             }
    821β–•
  ➜ 822β–•             throw new QueryException(
    823β–•                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    824β–•             );
    825β–•         }
    826β–•     }

  1   vendor\laravel\framework\src\Illuminate\Database\Connection.php:574
      PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1")

  2   vendor\laravel\framework\src\Illuminate\Database\Connection.php:574
      PDO::prepare("alter table `tweets` add constraint `tweets_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete ")
  • solution:
alter table `tweets` add constraint `tweets_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade;

Laravel Logo

Build Status Total Downloads Latest Stable Version License

About Laravel

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:

Laravel is accessible, powerful, and provides tools required for large, robust applications.

Learning Laravel

Laravel has the most extensive and thorough documentation and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.

You may also try the Laravel Bootcamp, where you will be guided through building a modern Laravel application from scratch.

If you don't feel like reading, Laracasts can help. Laracasts contains over 2000 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.

Laravel Sponsors

We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Partners program.

Premium Partners

Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

Security Vulnerabilities

If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.

License

The Laravel framework is open-sourced software licensed under the MIT license.

About

is a PHP project to learn basics of laravel 10.40.0


Languages

Language:PHP 66.2%Language:Blade 33.3%Language:JavaScript 0.5%Language:CSS 0.0%