Skip to content

Instantly share code, notes, and snippets.

@morad7
Last active March 5, 2017 23:23
Show Gist options
  • Save morad7/c697b9221414b8023e9f7fcfeb64558f to your computer and use it in GitHub Desktop.
Save morad7/c697b9221414b8023e9f7fcfeb64558f to your computer and use it in GitHub Desktop.
Laravel stylesheet
laravel new blog
---------------------------------------------------
subl . //sublime-text
---------------------------------------------------
php artisan serve
---------------------------------------------------
Route::get('about', function() {
$friends = ['Ismail', 'Nordine', 'Mounir'];
return view('about', compact('friends'));
});
---------------------------------------------------
@foreach($friends as $friend)
<li>{{ $friend }} </li>
@endforeach
----------------------------------------------------
php artisan make:controller AboutController
----------------------------------------------------
Route::get('about', 'AboutController@index');
----------------------------------------------------
public function index () {
return view('about');
}
----------------------------------------------------
@section('content)
//content
@stop
----------------------------------------------------
@yield('content
----------------------------------------------------
php artisan tinker
----------------------------------------------------
DB::table('cards')->get();
DB::table('cards')->insert(['title'=>'My new first card']);
----------------------------------------------------
sudo apt-get install sqlite3 php7.0-sqlite3
----------------------------------------------------
php artisan make:migration create_table_cards --create=cards
----------------------------------------------------
$table->string('title');
----------------------------------------------------
php artsian migrate
----------------------------------------------------
php artisan make:model Card
----------------------------------------------------
use App\Card
//...
public function index() {
$cards=Card::all();
return view('cards.index', compact('cards'));
}
----------------------------------------------------
Route::get('cards', 'CardController@index');
Route::get('cards/{cards}', 'CardController@show');
// model route binding : route wildcard must matches function parameter variable on controller.
----------------------------------------------------
public function show(Card $card) {
return view('cards.view', compact('card'));
}
----------------------------------------------------
//make model with migration
php artisan make model:Note -m
----------------------------------------------------
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->increments('id');
$table->integer('card_id')->unsigned()->index();
$table->text('body');
$table->timestamps();
});
}
----------------------------------------------------
class Card extends Model
{
public function notes()
{
return $this->hasMany(Note::class);
}
}
----------------------------------------------------
class Note extends Model
{
public function card()
{
return $this->belongsTo(Card::class);
}
}
----------------------------------------------------
//test relation
php artisan tinker
$note = new App\Note;
$note->card_id = 1;
$note->body = "First note";
$node->save();
$note->card;
$card = App\Card::find(1);
$card->notes;
-----------------------------------------------------
//test relation 2
php artisan tinker
$card = App\Card::first();
$note = new App\Note;
$note->body = "new note";
$note->save();
$card->notes()->save($note);
-----------------------------------------------------
//test relation 3
php artisan tinker
$card = App\Card::first();
$card->notes()->create(['body' => 'another note'']);
//be sure to add 'protected $fillable = ['body']' to the Note model.
-----------------------------------------------------
@extends('layout')
@section('content')
{{ Form::open(['url' => '/new/store']) }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', '', ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('post', 'Post') }}
{{ Form::textarea('post', '', ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('user', 'User') }}
{{ Form::select('user', $users, '',['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::submit('Save',['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
@stop
-----------------------------------------------------
public function store(Request $request) {
$article = new Article;
$article->title = $request->title;
$article->post = $request->post;
$article->user_id = $request->user;
$article->save();
return redirect('/');
//back();
}
-----------------------------------------------------
//controller
public function store(Request $request) {
$this->validate($request, [
'title' => 'required',
'author' => 'required',
'category_id' => 'required'
]);
$book = new Book;
$book->title = $request->title;
$book->author = $request->author;
$book->category_id = $request->category;
$book->save();
return redirect()->to('/');
}
-----------------------------------------------------
//view
@foreach($errors->all() as $error)
<p> {{ $error }} </p>
@endforeach
-----------------------------------------------------
php artisan make:auth
php artisan migrate
-----------------------------------------------------
-----------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment