I'm new to Laravel and php so I face and error I don't know how to solve.
The basic problem is since a lot of tables have primary:id and created_by,updated_by columns what I've figured out is inheriting them in my migrations.
I'm using php7
So I have a Base class
class BaseMigration extends Migration {
public function up(string $tableName) {
Schema::create($tableName, function (Blueprint $table) {
$table->mediumIncrements('id');
$table->primary('id');
$table->unsignedMediumInteger('created_by')->references('id')->on('users');
$table->unsignedMediumInteger('updated_by')->references('id')->on('users');
});
}
}
and the extending migration
class CreateItemsTable extends BaseMigration {
public function up() {
parent::up('items');
Schema::create('items', function (Blueprint $table) {
$table->string('name', 74);
$table->timestamps();
});
}
// ......
}
However php artisan migrate gives me this:
[ErrorException] Declaration of CreateItemsTable::up() should be compatible with Illuminate\Database\Migrations\BaseMigration::up(string $tableName)
Is it because I'm running double up() ?
What am I missing? Appreciate your kind help.
I think you need to have the same function signature, so pass string $tableName:
class CreateItemsTable extends BaseMigration {
public function up(string $tableName) {
Schema::create('items', function (Blueprint $table) {
parent::up('items');
$table->string('name', 74);
$table->timestamps();
});
}
// ......
}
Related
The following code in tinker returns a null value while it should return the project to which the first task is linked.
App\Task::first()->projects;
Already tried renaming the method names, column names in migrations, tried exiting tinker and logging back in
Project Migration
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('title');
$table->string('description');
$table->timestamps();
});
}
Task Migration
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('project_id');
$table->string('description');
$table->boolean('completed')->default(false);
$table->timestamps();
});
}
Project.php
use App\Task;
class Project extends Model
{
protected $fillable = ['title','description'];
public function tasks(){
return $this->hasMany(Task::class);
}
}
Task.php
use App\Project;
class Task extends Model
{
protected $fillable = [
'completed'
];
public function projects(){
return $this->belongsTo(Project::class);
}
}
If anyone could just review this piece of code and let me know where I have made any conventional\idiotic mistakes (since Im new to route model binding) it would be of great help!
A task belongs to a project, so rename projects to project as it is singular. If you keep projects then provide the column name as second parameter:
public function projects(){
return $this->belongsTo(Project::class, 'project_id');
}
// I suggest this
public function project(){
return $this->belongsTo(Project::class);
}
Your column types are different, for the id of the project you use Big Integer and for the reference you use Integer, so this:
$table->unsignedInteger('project_id');
should be this:
$table->unsignedBigInteger('project_id');
// also good to make the relationship on the Database level:
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
Not sure what im doing wrong but im getting this error, and im not sure how to solve it.
i referenced this, but im still unsure, maybe its my schema migration
im using laravel 5.5
laravel 5.2 Call to undefined method Illuminate\Database\Query\Builder::associate()
Call to undefined method
Illuminate\Database\Query\Builder::withTrashed()
PostController.php
public function isLikedByMe($id)
{
$post = Post::findOrFail($id)->first();
if (Like::whereUserId(auth()->id())->wherePostId($post->id)->exists()){
return 'true';
}
return 'false';
}
public function like(Post $post)
{
$existing_like = Like::withTrashed()->wherePostId($post->id)->whereUserId(auth()->id())->first();
if (is_null($existing_like)) {
Like::create([
'post_id' => $post->id,
'user_id' => auth()->id()
]);
} else {
if (is_null($existing_like->deleted_at)) {
$existing_like->delete();
} else {
$existing_like->restore();
}
}
Likes Model
class Like extends Model
{
//
}
User model condensed to whats important
public function likes()
{
return $this->belongsToMany('App\Post', 'likes', 'user_id', 'post_id');
}
Post model
public function likes()
{
return $this->belongsToMany('App\User', 'likes');
}
Migration:
likes migration
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->foreign('user_id')->references('id')->on('users');
$table->softDeletes();
$table->timestamps();
});
}
Posts migration
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
Your Eloquent model should use the Illuminate\Database\Eloquent\SoftDeletes trait to make use of the withTrashed() method.
Like::class
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Like extends Model
{
use SoftDeletes;
}
Had the same issue, the problem was that I used DB::table($table_name) instead of calling the model, using the model helped.
Regards
this should be quite simple but i don't know what wrong . i have the following migrations .
create_players_table
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->integer('club_id');
$table->string('first_name');
$table->string('last_name');
$table->double('price', 3, 2);
$table->enum('position', ['goalkeeper','defender','Midfielder','Striker']);
$table->timestamps();
});
}
create_clubs_table
public function up()
{
Schema::create('clubs', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('location');
$table->timestamps();
});
}
The relationship is straight forward as one player has one team (One to one) and one team can have many players (One to Many ) . The problem is i want to list all the players with their respective teams but somehow i end up with errors . This is my player model .
class Player extends Model
{
public function club()
{
return $this->belongsTo('App\Club');
}
}
And here is the controller :
public function index()
{
$players=Player::find(1);
echo $players->club()->location;
}
I get this error
ErrorException in TeamController.php line 15: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
Any help will be appreciated...
Your relation is not really a database relation try to make you're relation like this:
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('club_id');
$table->foreign('club_id')->references('id')->on('clubs');
$table->string('first_name');
$table->string('last_name');
$table->double('price', 3, 2);
$table->enum('position', ['goalkeeper','defender','Midfielder','Striker']);
$table->timestamps();
});
}
https://laravel.com/docs/5.4/migrations#foreign-key-constraints
Player Model
class Player extends Model
{
public function club()
{
return $this->belongsTo('App\Club', 'club_id', 'id');
}
}
Player controller
public function index()
{
$players=Player::find(1);
echo $players->club->location;
}
it should be like this :
$players=Player::with('club')->find(1);
echo $players->club->location;
Try without () in ->club(),
public function index()
{
$players=Player::find(1);
echo $players->club->location;
}
As the error says, it refers to the "BelongsTo" relationship. i.e. Here ->club() returns the relationship object, not the club object. If you want to get the club object call as $players->club->location; .
My migration is like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id_test');
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
I want to change data type and add column in table test. Then update table in database. I edit like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->string('id_test', 18);
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
Then I run : php artisan migrate:refresh
I lost my data in database
Is there any people who can help me?
Create a new migration file using below command :
php artisan make:migration name_of_your_file
And its function should be like this
public function up()
{
Schema::table('tests', function($table) {
$table->renameColumn('id_test', 'id');
$table->string('id_test', 18);
});
}
public function down()
{
Schema::table('tests', function($table) {
$table->dropColumn('id_test');
});
}
Now just run below command
php artisan migrate
Note : Dont use refresh it deletes the data
To know more about Laravel Migration refer : https://laravel.com/docs/5.3/migrations#modifying-columns
there. I'm trying to deal with Laravel Framework, but there is a problem. Here is my code
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
});
}
I made migration and tried to add some fields to my table. But IDE doesn't recognize $table variable properly, so as a result I have warning: "Method increments doesn't found in class", and I can't use auto-completion.
Are there propositions how to fix this?
Whole code:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration {
public function up()
{
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title',150);
$table->text('body');
$table->string('preview',300);
$table->string('author',100);
$table->timestamps();
});
}
public function down()
{
Schema::drop('posts');
}
}
Just specify the type of $table. The class is Illuminate\Database\Schema\Blueprint
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
});