Ajax not working in Laravel?

Assume you have a list of items with checkboxes:


<ul class="project-ul">
@foreach ($projects as $project)
<li>  <label><input type="checkbox" value="1" class="checking"  data-id="{{$project->id}}" {{($project->featured) ? 'checked' : ''}}> {{$project->title}}</label></li>
@endforeach
</ul>

This may return a list like this:
Continue reading "Ajax not working in Laravel?"

Foreign Keys in Laravel

Imagine you have a user who hasMany logs. If you delete the user, you probably don't need the logs of him anymore. The cleanest way to automatically remove them is to use foreign keys in your database. Note that the foreign key column needs to be unsigned.

Here is a clean code when you create a new database for a relation:


public function up()
{
  Schema::create('logs', function (Blueprint $table) {
          $table->integer('id_user')->unsigned();
          $table->string('log');
          $table->timestamps();

          $table->foreign('id_user')
              ->references('id')->on('users')
              ->onDelete('cascade'); 
 });
}     

Continue reading "Foreign Keys in Laravel"

Best Practice For hasOne Relation in Laravel

User hasOne Phone

You should have two tables that look somehow like this:

User Table
id name
1 Bob
Phone Table
user_id phone
1 1234567

From the user class you can access his phone as


public function phone()
{
 return $this->hasOne('App\Phone');   
}

Continue reading "Best Practice For hasOne Relation in Laravel"

Create database for branch and phpunit

Whenever I work on a new branch, I find it useful to have a unique table for the branch and for my testing purposes.
Lets assume we have three tables denoted in our `.env` file like that:

 
    DB_CONNECTION      = mysql_local 
    DB_DATABASE        = mastertable
    DB_DATABASE_BRANCH = branchtable
    DB_DATABASE_TEST   = testtable

Continue reading "Create database for branch and phpunit"

Background-Image for HTML Email in Outlook

If you create a HTML email with an background image, you might be using something like this:


<td align="center" bgcolor="#F4C046" 
 style="background-image: url('../image.jpg');
        background-repeat: no-repeat;
        background-size: cover; 
        background-position:center;
        background-color:#F4C046">
  <div>
  </div>
</td>

While this is will work great in most Email-Clients, it won't work in Outlook 2007-2016. For that you need to use Vektor Markup Language (VML), which is a feature of Microsoft which is deprecated since 2011, but its the only way that one can make background-images accessable for Outlook 2007-2016. Using VML is often refered to as "BULLETPROOF-BACKGROUNDS", because if you use inline CSS combined with VML, then the background should work in every client.

Continue reading "Background-Image for HTML Email in Outlook"

Gitignore Pattern Explained

Rules

You find these rules in more depth explained in the official docs.

  1. foo/ matches any folder foo (in any folder inside your git worktree), but no file foo or foo.txt
  2. foo matches any folder foo and file foo (in any folder inside your git worktree) but not files that contain the string, like foo.txt Continue reading "Gitignore Pattern Explained"

Pivot tables don’t need an AI column

Most scenarios are covered by a hasOne, hasMany or ManyToMany relation. How to use them is explained in detail here. I give a brief overview how they are connected to the database.

User hasOne Phone

You should have two tables that look somehow like this:

User Table
id name
1 Bob
Phone Table
user_id phone
1 1234567

From the user class you can access his phone as


 return $this->hasOne('App\Phone','user_id','id');   

Continue reading "Pivot tables don’t need an AI column"