<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bogIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
$table->bogIncrements('id'); typo in here.
Related
I'm trying to change the data type (from string to boolean) of 5 columns in my database, but it's showing an error that i have no idea what is the meaning.
My migration is this:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeFieldDataType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('categoria_pneu')->change();
$table->boolean('categoria_dicas')->change();
$table->boolean('categoria_servicos')->change();
$table->boolean('categoria_dicas_gerais')->change();
$table->boolean('categoria_variados')->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('categoria_pneu');
$table->dropColumn('categoria_dicas');
$table->dropColumn('categoria_servicos');
$table->dropColumn('categoria_dicas_gerais');
$table->dropColumn('categoria_variados');
});
}
}
The erros that's showing, is this:
I don't know what is the meaning of the error, since i'm trying to change the type to BOOLEAN and not TINYINT (i don't actually know if it's the same...)
\App\Models\Testt::factory()->create();
PHP Error: Class "Database\Factories\TesttFactory" not found in C:\xampp\htdocs\Project1\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php on line 683
im using cmd and using migrations to generate fake data
below is the mentioned code of factory, migrations
<?php
namespace Database\Factories;
use App\Models\Testt;
use Illuminate\Database\Eloquent\Factories\Factory;
**class TesttfactoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = \App\Models\Testt::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
//
];
}
}**
and for migrations the code is
**<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
class CreateTesttsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('\App\Models\testts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->mediumText('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('testts');
}
}**
I have the following migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->uuid('answer_token')->default(DB::raw('UUID()'));
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}
As you can see I'm trying to set UUID as default value. I've seen it here
But when I run php artisan migrate I see the following:
What is wrong?
As I came across this question when having the same issue:
With MySQL 8 you can use functions as default values. But you need to put paranthesis around them. So this works:
$table->uuid('uid')->default(DB::raw('(UUID())'));
But this won't be compatible with MySQL 5.7!
You cannot use mySQL function as default value it should be either set or you use a trigger.
try please like this:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$uuid = DB::raw('select UUID()');
$table->uuid('answer_token')->default($uuid);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}
I am trying to make some relationships between some tables but have an error that states..
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 MySQL server version for the
right syntax to use near ')' at line 1 (SQL: alter table users add
constraint users_board_id_foreign foreign key (board_id)
references `` ())
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->foreign('board_id')->unsigned();
$table->foreign('board_id')->references('id')->on('boards');
$table->foreign('message_id')->references('id')->on('messages');
$table->foreign('message_id')->unsigned();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
This table is supposed to be related to this table...
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBoardsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('boards', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100)->unique();
$table->foreign('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('link_id')->unsigned();
$table->foreign('link_id')->references('id')->on('links');
$table->foreign('message_id')->unsigned();
$table->foreign('message_id')->references('id')->on('messages');
$table->foreign('tag_id')->unsigned();
$table->foreign('tag_id')->references('id')->on('tags');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('boards');
}
}
I've already drop the table and the schema and started fresh and still get the same error! Please be kind :!
Try this one:
// USERS
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->integer('board_id')->unsigned();
$table->foreign('board_id')->references('id')->on('boards');
$table->integer('message_id')->unsigned();
$table->foreign('message_id')->references('id')->on('messages');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
//BOARDS
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBoardsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('boards', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100)->unique();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('link_id')->unsigned();
$table->foreign('link_id')->references('id')->on('links');
$table->integer('message_id')->unsigned();
$table->foreign('message_id')->references('id')->on('messages');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->references('id')->on('tags');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('boards');
}
}
You can't define columns with foreign():
$table->integer('board_id')->unsigned();
$table->foreign('board_id')->references('id')->on('boards');
$table->integer('message_id')->unsigned();
$table->foreign('message_id')->references('id')->on('messages');
The same goes for the boards migration.
I experienced very alike situation, my schema
`
public function up() {
Schema::dropIfExists('countries_states');
Schema::create('countries_states', function (Blueprint $table) {
$table->id();
$table->foreignId('country_id')->unsigned();
$table->foreign('country_id')->references('id')->on('countries');
$table->string('state', 30);
$table->string('abbr', 30);
});
//Schema::enableForeignKeyConstraints();
}
`
And I had the same
I solved it by granting access to specific user for specific database and voila - everything started to function like charm
I have two tables in my database Users and Complains.
Users table :
id | name | password
Complains table
id | user_id | title | body
The user_id is a foreign key. How do I retrieve title and body from the complains table based on the user_id?
This is what I have so far but I cannot retrieve data specific to the user_id
My User Model :
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'surname', 'regnumber', 'course', 'department', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
My Profile Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Complain;
use App\Feedback;
use App\Item;
use Illuminate\Support\Facades\Redirect;
use Session;
use Auth;
class ProfileController extends Controller
{
public function profile($user_id){
$user = User::whereId($user_id)->first();
$complains = Complain::paginate(2);
return view('user.profile', compact('user', 'complains'));
}
}
My view:
#foreach($complains as $complain)
<div>
<h3 class="operator-complain-title">Title: </h3>
<p>{{ $complain->title }}</p>
<h3 class="operator-complain-title">Complain:</h3>
<p>{{ $complain->body }}</p>
</div>
<hr>
#endforeach
Anyone with any ideas please share.
You'll want to setup a one-to-many relationship as outlined here: https://laravel.com/docs/5.4/eloquent-relationships#one-to-many
Once you have the correct methods in place, Eloquent will find relations when you do something like $user->complains.
class User extends Model
{
/**
* Get the complains for the User.
*/
public function complains()
{
return $this->hasMany('App\Complain');
}
}
class Complain extends Model
{
/**
* Get the User for the Complain.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
This is my users table migration :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
This is my complains table migration :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateComplainsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('complains', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('complains');
}
}
This is the migration I used to add foreign key to complains table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddForeignKeyToComplains extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('complains', function (Blueprint $table) {
$table->integer('user_id')->after('id')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('complains', function (Blueprint $table) {
$table->dropColumn('user_id');
});
}
}