Learn from your fellow PHP developers with our PHP blogs, or help share the knowledge you've gained by writing your own.
hasManyThrough()
for the relation Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->integer('country_id')->unsigned();
$table->rememberToken();
$table->timestamps();
$table->foreign('country_id')->references('id')->on('countries')
->onDelete('cascade');
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade');
});
Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
public function posts(){
return $this->hasManyThrough(
Post::class,
User::class,
'country_id',
'user_id',
'id',
'id'
);
}
}
$country = Country::find(1);
dd($country->posts);
morphMany()
and morphTo()
for relation.Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Videos TableSchema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Comments TableSchema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string("body");
$table->integer('commentable_id');
$table->string("commentable_type");
$table->timestamps();
});
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function comments(){
return $this->morphMany(Comment::class, 'commentable');
}
}
Video Model<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model{
public function comments(){
return $this->morphMany(Comment::class, 'commentable');
}
}
Comment Model<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model{
public function commentable(){
return $this->morphTo();
}
}
$post = Post::find(1);
$comment = new Comment;
$comment->body = "Hi Harikrishnan";
$post->comments()->save($comment);
$video = Video::find(1);
$comment = new Comment;
$comment->body = "Hi Harikrishnan";
$video->comments()->save($comment);
$post = Post::find(1);
dd($post->comments);
$video = Video::find(1);
dd($video->comments);
morphToMany()
and morphedByMany()
will be used for many to many polymorphic relationshipsSchema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Videos TableSchema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Tags tableSchema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Taggables tableSchema::create('taggables', function (Blueprint $table) {
$table->integer("tag_id");
$table->integer("taggable_id");
$table->string("taggable_type");
});
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function tags(){
return $this->morphToMany(Tag::class, 'taggable');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
public function tags(){
return $this->morphToMany(Tag::class, 'taggable');
}
}
Tag Model<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public function posts(){
return $this->morphedByMany(Post::class, 'taggable');
}
public function videos(){
return $this->morphedByMany(Video::class, 'taggable');
}
}
Creating Records$post = Post::find(1);
$tag = new Tag;
$tag->name = "Hi Harikrishnan";
$post->tags()->save($tag);
$video = Video::find(1);
$tag = new Tag;
$tag->name = "Vishnu";
$video->tags()->save($tag);
$post = Post::find(1);
$tag1 = new Tag;
$tag1->name = "Kerala Blasters";
$tag2 = new Tag;
$tag2->name = "Manajapadda";
$post->tags()->saveMany([$tag1, $tag2]);
$video = Video::find(1);
$tag1 = new Tag;
$tag1->name = "Kerala Blasters";
$tag2 = new Tag;
$tag2->name = "Manajappada";
$video->tags()->saveMany([$tag1, $tag2]);
$post = Post::find(1);
$tag1 = Tag::find(3);
$tag2 = Tag::find(4);
$post->tags()->attach([$tag1->id, $tag2->id]);
$video = Video::find(1);
$tag1 = Tag::find(3);
$tag2 = Tag::find(4);
$video->tags()->attach([$tag1->id, $tag2->id]);
$post = Post::find(1);
$tag1 = Tag::find(3);
$tag2 = Tag::find(4);
$post->tags()->sync([$tag1->id, $tag2->id]);
$video = Video::find(1);
$tag1 = Tag::find(3);
$tag2 = Tag::find(4);
$video->tags()->sync([$tag1->id, $tag2->id]);
$post = Post::find(1);
dd($post->tags);
$video = Video::find(1);
dd($video->tags)
$tag = Tag::find(1);
dd($tag->posts);
$tag = Tag::find(1);
dd($tag->videos);
composer require --dev phpunit/phpunit
tests
in your project root, and within that directory, create a file named ExampleTest.php
. Here's an example of what your test file might look like:<?php
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
public function testTrueAssertsToTrue()
{
$this->assertTrue(true);
}
}
testTrueAssertsToTrue
, which asserts that true
is indeed true
. vendor/bin/phpunit tests
Ctrl + Alt + T
to open the terminal.Alt + F2
, type konsole
, and press Enter.cd
(Change Directory): Use cd
followed by the name of the directory to navigate to that directory. For example: cd Documents
ls
(List Files): Use ls
to list the files and directories in the current directory. ls
pwd
(Print Working Directory): Use pwd
to display the full path of the current directory. pwd
mkdir
(Make Directory): Use mkdir
followed by the name of the directory to create a new directory. mkdir my_directory
touch
: Use touch
followed by the name of the file to create a new empty file. touch my_file.txt
cp
(Copy): Use cp
followed by the source file and destination to copy files. cp source_file.txt destination_directory/
mv
(Move/Rename): Use mv
followed by the source and destination to move or rename files. mv old_name.txt new_name.txt
rm
(Remove): Use rm
followed by the file name to delete files. Be careful as this action is irreversible. rm unwanted_file.txt
cat
(Concatenate): Use cat
followed by the file name to display the contents of a file. cat my_file.txt
nano
or vim
(Text Editors): Use nano
or vim
followed by the file name to edit a file in the terminal. nano my_file.txt
grep
(Global Regular Expression Print): Use grep
followed by a search term and file name to search for a specific pattern in a file. grep "pattern" my_file.txt
uname
(Unix Name): Use uname
to display system information. uname -a
df
(Disk Free): Use df
to display disk space usage. df -h
top
or htop
(Process Monitoring): Use top
or htop
to display real-time system resource usage. top
<?php
$encryptionKey = openssl_random_pseudo_bytes(32);
$plaintext = "Sensitive data to encrypt";
$ciphertext = openssl_encrypt($plaintext, 'aes-256-cbc', $encryptionKey, 0, $iv);
$decryptedText = openssl_decrypt($ciphertext, 'aes-256-cbc', $encryptionKey, 0, $iv);
echo $decryptedText;
?>
<?php
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$keyPair = openssl_pkey_new($config);
openssl_pkey_export($keyPair, $privateKey);
$publicKey = openssl_pkey_get_details($keyPair)["key"];
$plaintext = "Confidential message";
openssl_public_encrypt($plaintext, $encrypted, $publicKey);
openssl_private_decrypt($encrypted, $decrypted, $privateKey);
echo $decrypted;
?>