Skip to content

Instantly share code, notes, and snippets.

View EricMcWinNer's full-sized avatar
😴
Sleeping... Dream it to me if it's important

Eric Aprioku (Eric McWinNEr) EricMcWinNer

😴
Sleeping... Dream it to me if it's important
View GitHub Profile
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Candidate extends Model {
/**
* Gets the political party a candidate belongs to
*/
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Candidate extends Model {
/**
* Gets the political party a candidate belongs to
*/
<?php
namespace App\Http\Controllers;
use App\Models\Candidate;
class RandomController extends Controller {
public function getPoliticalParty(){
$candidate = Candidate::find(1); // Get a Candidate Instance
$politicalParty = $candidate->politicalParty; // Get the Political party this candidate belongs to
// Do whatever with it
@EricMcWinNer
EricMcWinNer / User.php
Created November 20, 2020 19:55
Shows a basic example for defining a Has One Through Laravel Relationship
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
/**
* Get the party this user voted for.
*/
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Vendor extends Model {
/**
* Get the this vendor's user reviews
*/
@EricMcWinNer
EricMcWinNer / Models.php
Last active November 20, 2020 21:50
A file showing how one to one polymorphic relationships are created in Laravel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Wallet extends Model {
/**
* Defines the owner of the wallet
*/
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model {
/**
* Defines all posts that belong to this category
*/
<?php
use App\Models\User;
class RandomClass {
public function randomFunction(){
$user = User::find(1); // Gets a User Instance
$vote = $user->vote; // Gets the User's Vote
// Do whatever with it
print_r($vote);
<?php
use App\Models\User;
class RandomClass {
public function randomFunction(){
$user = User::find(1); // Gets a User Instance
$vote = $user->vote->politicalParty; // Gets the party the user voted for
// Do whatever with it
print_r($vote);
@EricMcWinNer
EricMcWinNer / Word.php
Created December 2, 2020 23:27
Define the relationship
<?php
// app/Models/Word.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Word extends Model
{
use HasFactory;
// Define the One to One Relationship