I create a project based on the book Getting Started with Laravel 4.
So, I create two files in app/models/ - Cat.php and Breed.php with this content:
Cat.php
<?php
class Cat extends Eloquent {
protected $fillable = array('name','date_of_birth','breed_id');
public function breed() {
return $this->belongsTo('Breed');
}
}
and Breed.php
<?php
class Breed extends Eloquent {
public $timestamps = false;
public function cats()
{
return $this->hasMany('Cat');
}
}
and after, I use command php artisan migration:make create_cats_and_breeds_table
Ok, and should arise file in app/database/migrations. It is.
But, its contents it's not same as in the book...
In book:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCatsAndBreedsTable extends Migration {
public function up()
{
Schema::create('cats', function($table)
{
$table->increments('id');
$table->string('name');
$table->date('date_of_birth');
$table->integer('breed_id')->nullable();
$table->timestamps();
})
Schema::create('breeds', function($table)
{
$table->increments('id');
$table->string('name');
})
}
public function down()
{
Schema::drop('cats');
Schema::drop('breeds');
}
}
My code:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCatsAndBreedsTable extends Migration {
public function up()
{
//
}
public function down()
{
//
}
}
What's happen?
https://github.com/laracasts/Laravel-4-Generators
Provides some additional artisan commands which you can used to specific your fields in order to generate the migration files.
php artisan generate:migration create_posts_table --fields="title:string, body:text"
migration:make command does not know anything about your models. It just creates a stub that you need to fill with column definitions for your tables.
Related
I want to create several schemas to my database using migration. I have this code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSchemaAdministracion extends Migration
{
public function up()
{
DB::unprepared("CREATE SCHEMA `administracion`");
}
public function down()
{
DB::unprepared('DROP SCHEMA `administracion`');
}
}
I tried this way but I got: Invalid schema name: 7 ERROR: No schema has been selected.
Try using DB::statement with DB::raw.
I tested this with Laravel 5.8. I guess it should work with earlier versions too.
\DB::statement(\DB::raw("CREATE SCHEMA administracion"));
You can then use it with the schema as prefix. Like this:
Schema::create('administracion.mytable', function (Blueprint $table) {
$table->bigIncrements('id');
...
});
Notice for rollback operation: consider that in postgres you cannot drop a schema if it has elements. You should remove them before removing the schema.
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdministracionTable extends Migration
{
public function up()
{
Schema::create('administracion', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
public function down()
{
Schema::drop('administracion');
}
}
I've got two different tables, one is called articles, the other one images I Want to create an article that contains multiple images,
How can I create with Laravel 5.5 a many-to-many relation? I've followed this post on laracast: https://laracasts.com/discuss/channels/laravel/multiple-images-in-article-galerry
here is the code
ARTICLE MODEL
public function images() {
return $this->belongsToMany('App\Image');
}
IMAGE MODEL
public function articles() {
return $this->belongsToMany('App\Article');
}
ARTICLE-IMAGE MIGRATION(TABLE)
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticleImageTable extends Migration {
public function up()
{
Schema::create('article_image', function(Blueprint $table)
{
$table->increments('id');
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('image_id')->unsigned()->index();
$table->foreign('image_id')->references('id')->on('images')->onDelete('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::drop('article_image');
}
}
ARTICLES CONTROLLER
<?php namespace App\Http\Controllers;
use App\Article;
class ArticlesController extends Controller
{
public function index()
{
$articles = Article::with('images')->get();
return view('articles.index', compact('articles'));
}
public function show($id)
{
$article = Article::find($id);
$article->load('images');
return view('articles.show', compact('article'));
}
}
MY PROBLEM IS How do i continue from here(that is display both the articles and the images)
Currently, I am trying to create roles for my application, unfortunately I am having some troubles. Whenever I run php artisan migrate --seed, I get the error I've written in the title. Honestly, I feel like I've messed up something really simple like a name but I just can't find my mistake. I'd appreciate any help.
User.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
public function roles(){
return $this->belongsToMany('App\Role');
}
}
Role.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users(){
return $this->belongsToMany('App\User');
}
}
Users table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('username');
$table->string('password');
$table->string('email');
$table->timestamps();
$table->rememberToken();
});
}
Roles table:
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description')->nullable()->default(null);
$table->timestamps();
});
}
role_user table
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('role_id');
$table->timestamps();
});
}
RoleTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Role;
class RoleTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$role_user = new Role();
$role_user->name = 'User';
$role_user->description = "Normal User";
$role_user->save();
$role_admin = new Role();
$role_admin->name = 'Admin';
$role_admin->description = "Admin User";
$role_admin->save();
}
}
UserTableSeeder.php
public function run()
{
$role_admin = Role::where('name', 'Admin')->first();
$user = new User();
$user->first_name = 'test';
$user->last_name = 'test';
$user->username = 'Admin';
$user->password = bcrypt('test');
$user->email = 'test#gmail.com';
$user->save();
$user->roles()->attach($role_admin);
}
DatabaseSeeder.php
public function run()
{
$this->call(RoleTableSeeder::class);
$this->call(UserTableSeeder::class);
}
}
As mentioned in the comments:
Run: composer dump-autoload.
If Composer\Exception\NoSslException exception is thrown, you may need to run composer config -g -- disable-tls true before composer dump-autoload.
I had the same issue as above
the composer command didn't do the trick.
on Laravel 5.5 I ran
php artisan cache:clear
and then all my new created seeder classes were found
I have faced the same issue on PH 7.2.* with Lavarel 5.5.*
Solution very easy - use the namespace:
php artisan db:seed --class=Modules\{moduleName}\Database\Seeders\{className}DatabaseSeeder
Also here:
$this->call('Modules\{moduleName}\Database\Seeders\{className}TableSeeder');
I am having offers and services table.
Service(s) is a child of an offer. So far I have established functionality for soft deleting an offer. How would I also soft delete appended services? Here is my code:
Migration Offers
Schema::create('offers', function(Blueprint $table)
{
$table->increments('id')->unsigned();
...
$table->timestamps();
$table->softDeletes();
});
Migration Services
Schema::create('services', function(Blueprint $table)
{
$table->increments('id');
$table->integer('offer_id')->unsigned();
...
$table->timestamps();
$table->softDeletes();
});
Schema::table('services', function($table)
{
$table->foreign('offer_id')
->references('id')
->on('offers');
});
Model Offer
use SoftDeletes;
protected $dates = ['deleted_at'];
public function services() {
return $this->hasMany('App\Service');
}
Model Service
public function offer() {
return $this->belongsTo('App\Offer');
}
Delete method
public function destroy($id)
{
$offer = Offer::find($id);
$offer->delete();
}
Thank you for all the help.
I have put this code in Offer model:
protected static function boot() {
parent::boot();
static::deleting(function($offer) {
$offer->services()->delete();
});
}
And added missing
use SoftDeletes;
protected $dates = ['deleted_at'];
in the Service model.
You should use Eloquent events for this.
Offers::deleted(function($offer) {
$offer->services()->delete();
});
Offers::restored(function($offer) {
$offer->services()->withTrashed()->restore();
});
If you want to get cascading softDeletes in your Eloquent Models I highly recommend using this library iatstuti/laravel-cascade-soft-deletes
Composer
// get it with composer.
$ composer require iatstuti/laravel-cascade-soft-deletes="1.0.*"
Quick example
The one provided in the getting started sample.
<?php
namespace App;
use App\Comment;
use Iatstuti\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes, CascadeSoftDeletes;
protected $cascadeDeletes = ['comments'];
protected $dates = ['deleted_at'];
public function comments()
{
return $this->hasMany(Comment::class);
}
}
You can do like this.
self::deleting(function($offer) {
$offer->services()->delete();
});
self::restoring(function ($offer) {
$offer->services()->restore();
});
You should first delete/restore the children records (services) before deleting/restoring the parent (offer). Failing to do this, will trigger referential integrity MySql error.
I am new to Laravel from code igniter and I am LOVING THE FRAMEWORK! My life is so much easier now.
I have created a table with columns using php artisan and entered some test data. I now want to add a few new columns to the database without affecting the current data, and setting the new fields to be null.
My inital thought was to enter a new field in the database migrate file and the run "php artisan migrate", but this just gave me the message "nothing to migrate" and did enter the new column in my database.
Here is my database migrate file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateFestivalsTable extends Migration {
public function up()
{
Schema::create('festivals', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title');
$table->timestamps();
});
}
public function down()
{
Schema::drop('festivals');
}
}
create new migration with artisan name it addColumnFestivalTable
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class addColumnFestivalTable extends Migration {
public function up()
{
Schema::table('festivals', function($table)
{
$table->string('new_col_name');
});
}
public function down()
{
Schema::table('festivals', function($table)
{
$table->dropColumn('new_col_name');
});
}
}
for more information read Laravel 5.4 doc