glasstream2000

glasstream2000

Member Since 1 Year Ago

Experience Points
7,070
Total
Experience

2,930 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
49
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 2
7,070 XP
Jan
21
1 month ago
Activity icon

Replied to Laravel 6

Thank You so much!

Activity icon

Started a new Conversation Laravel 6

In my controller using laravel 5.6 I had the following code below that worked with no issues. Now using Laravel 6 I get an issue with - Non-static method Illuminate\Http\Request::get() should not be called statically.

It must be my Request::get in my IF statement to check on wether or not a box is checked on submission of a form. I have looked at the docs and not seeing what I'm doing wrong or what I should be using in place for Laravel 6 Any thoughts?

public function store(Request $request)
    {
        // $this->validate($request, [
        //     'first_name' => 'required|min:2|max:50',
        //     'last_name' => 'required|min:2|max:50',
        //     'year_born' => 'required|numeric|min:4',
        //     'last_four' => 'required|numeric|min:4',
        //     'location' => 'required',
        //     'email' => 'required|email',
        // ]);
        $request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|digits:4',
            'last_four' => 'required|numeric|digits:4',
            'location' => 'required',
            'email' => 'required|email',
            // ], [
            //     'first_name.required' => 'Name is required',
            //     'first_name.min' => 'Name must be at least 2 characters.',
            //     'first_name.max' => 'Name should not be greater than 50 characters.',
            //     'year_born.size' => 'Year born and Last four should only be 4 numbers',

        ]);

        // create record
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;

        $listing->save();

        if (Request::get('MED') === '1') {

            // DB::connection('mysql_external');

            $listing = new Listing;
            $listing->setConnection('mysql_external');
            $listing->first_name = $request->input('first_name');
            $listing->last_name = $request->input('last_name');
            $listing->year_born = $request->input('year_born');
            $listing->last_four = $request->input('last_four');
            $listing->location = $request->input('location');
            $listing->medlocation = $request->input('medlocation');
            $listing->email = $request->input('email');
            $listing->module = $request->input('module');
            $listing->user_id = auth()->user()->id;

            $listing->save();
        } else {
            // Checkbox is not checked
        }

        return redirect('/dashboard')->with('success', 'Module Completed');
    }
Nov
03
3 months ago
Activity icon

Replied to Change Multiple Selection To Integers

Wow, that was it! looks at it for days and never caught that. Thanks!!!

Activity icon

Started a new Conversation Change Multiple Selection To Integers

I have a blog where you can select multiple tags for the post, it is returning the multiple selections, but as strings instead of the id. Not sure why and my searches have not helped me either.

Migration

public function up()
    {
        Schema::create('post_tag', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('post_id');
            $table->integer('tag_id');
        });
    }

Controller Store function

/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreatePostsRequest $request)
    {
        // upload image
        $image = $request->image->store('posts');

        dd($request->all());

        $post = Post::create([

            'title' => $request->title,
            'description' => $request->description,
            'content' => $request->content,
            'image' => $image,
            'published_at' => $request->published_at,
            'category_id' => $request->category,

        ]);

        if ($request->tags) {
            $post->tags()->attach($request->tags);
        }


        session()->flash('success', 'Post created.');

        return redirect(route('posts.index'));
    }

View

<div class="form-group">

                    <label for="category">Category</label>

                    <select id="category" class="form-control" name="category">

                    @foreach($categories as $category)

                    <option value="{{ $category->id }}"

                        @if(isset($post))
                            @if($category->id === $post->category_id)
                            selected
                        @endif
                        @endif

                        >{{ $category->name }}</option>
                    @endforeach
                    </select>

                </div>
                 @if ($tags->count() > 0 )
                <div class="form-group">
                    <label for="tags">Tags</label>
                        <select name="tags[]" id="tags" class="form-control" multiple>
                        @foreach ($tags as $tag)
                    <option vlaue="{{ $tag->id }}"

                        @if(isset($post))
                        @if($post->hasTag($tag->id))
                            selected
                        @endif
                        @endif

                        >{{ $tag->name }}</option>
                        @endforeach
                    </select>
                </div>
                @endif
                <div class="form-group">

                    <button class="btn btn-success" type="submit">
                        {{ isset($post) ? 'Update Post' : 'Create post' }}
                    </button>

DD dump in the browser

array:8 [▼
  "_token" => "BhvnTsZ0XCmdGo9LKDWtpJb5Rhc0kQiDrP7Ae9kN"
  "title" => "Laravel install on windows server"
  "description" => "dsfgsfgsdg"
  "content" => "<div>dsfgdsfgdsfg</div>"
  "published_at" => "2019-11-04 12:00"
  "category" => "3"
  "tags" => array:2 [▼
    0 => "laravel"
    1 => "laracasts"
  ]
  "image" => UploadedFile {#288 ▶}
]

Laravel should be 0 and laracasts should be a 1 to be sent to the post_tag table and laravel is giving me this error: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'laravel' for column qq.post_tag.tag_id at row 1 (SQL: insert into post_tag (post_id, tag_id) values (2, laravel), (2, laracasts))

Not sure what I am missing

Sep
13
5 months ago
Activity icon

Replied to If Condition Based On If A Checkbox Is Checked

To help anyone else out with this, I solved it by changing the connection to:

if (Input::get('MED') === '1')
        {
        
        // DB::connection('mysql_external');
        
        $listing = new Listing;
        $listing->setConnection('mysql_external');
Activity icon

Started a new Conversation If Condition Based On If A Checkbox Is Checked

FYI this is a repost of the same discussion that I started yesterday as I don't know how to delete it since I made the post so hard to read... My apologies

I have a form, that the user fills out and they submit and it goes into DB1. I want if they check a box that the fields go into DB1 and also DB2. I have the connection added to my config/database and a model just for the connection to DB2, named protected $connection = 'mysql_external'; My form has the checkbox code as follows

<div class="form-check">
    <input type="checkbox" class="form-check-input" id="MED" name="MED" value="1">
    <label class="form-check-label" for="MED">Employee?</label>
  </div>

Going to my [email protected], where I have the below code:

public function store(Request $request)
    {
        $request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|digits:4',
            'last_four' => 'required|numeric|digits:4',
            'location' => 'required',
            'email' => 'required|email', 
        ]);
        // create record
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        $listing->save();

        if (Input::get('MED') === '1')
        {
            DB::connection('mysql_external');
        
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        
        $listing->save();
        }
        else
        {
            // Checkbox is not checked
        }
        
        return redirect('/dashboard')->with('success', 'Module Completed');
        
    }

Right now it is adding two sets of records into DB1 when the checkbox is checked and only one when it isn't checked, why when the checkbox is checked it is not using the DB:connection I have specified in my controller?

Sep
12
5 months ago
Activity icon

Replied to Pass Data To Another Database If A Checkbox Is Checked

Anyone have any ideas if this is even possible or can point me to some docs where I might be able to read and do this?

Activity icon

Replied to Pass Data To Another Database If A Checkbox Is Checked

I got ya, the connection is in my other model is connection now

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon;
use DB;

class ListExternal extends Model
{
    
    protected $fillable = [

       'first_name', 'last_name', 'email', 'password', 'created_at', 'module', 'location',

    ];
    
    public function user(){
        return $this->belongsTo('App\User');
    }
    
    public function module(){
        return $this->belongsTo('App\Module');
    }
    

    protected $connection = 'mysql_external';

}

and my controller now has

$listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        
        $listing->save();
       
        

        if( $request->has('MED')) 
        DB::connection('mysql_external')->insert([
            ['first_name' => 'first_name'],
            ['last_name' => 'last_name'],
            ['year_born' => 'year_born'],
            ['last_four' => 'last_four'],
            ['location' => 'location'],
            ['email' => 'email'],
            ['module' => 'module'],
        
        ]);

It enters into the main database but when the checkbox is checked it just adds it into the main db not the external one as well

Activity icon

Replied to Pass Data To Another Database If A Checkbox Is Checked

Ok so I made another model and added protected $connection2 = 'mysql_external'; to it. and my controller is setup like:

// create record
        
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        
        $listing->save();
       
        

        if( $request->has('MED')) 
        DB::connection2('mysql_external')->insert([
            ['first_name' => 'first_name'],
            ['last_name' => 'last_name'],
            ['year_born' => 'year_born'],
            ['last_four' => 'last_four'],
            ['location' => 'location'],
            ['email' => 'email'],
            ['module' => 'module'],
        
        ]);

It works in the default database but when the box is checked it does'nt go into the other database, I'm assuming the error is in my if( $request->has('MED')) what would be the best syntax to check wether the box is checked or not?

Activity icon

Replied to Pass Data To Another Database If A Checkbox Is Checked

My model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon;

class Listing extends Model
{
    
    protected $fillable = [

       'first_name', 'last_name', 'email', 'password', 'created_at', 'module', 'location',

    ];
    
    public function user(){
        return $this->belongsTo('App\User');
    }
    
    public function module(){
        return $this->belongsTo('App\Module');
    }
    public function connection(){
        $connection = 'mysql_external';
    }
Activity icon

Replied to Pass Data To Another Database If A Checkbox Is Checked

Forgot to say that the main database always gets the fields entered into the main database even when the box is checked and the external connection only gets data if the box is checked.

Activity icon

Started a new Conversation Pass Data To Another Database If A Checkbox Is Checked

What I'm trying to achieve is when a user fills out a form and they check a checkbox, then the form is submitted into 2 separate DB's with the same structure.

database.php

'mysql' => [
            'driver' => 'mysql',

and second DB'

'mysql_external' => [
            'driver' => 'mysql',

Both Databases are on the same server and have the same credentials and are both referenced in the env file.

My form:

{!!Form::open(['action' => '[email protected]','method' => 'POST'])!!}
<form>
  <div class="form-group row">
    <label for="first_name" class="col-2 col-form-label">First Name</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="first_name" name="first_name" placeholder="First Name" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="last_name" class="col-2 col-form-label">Last Name</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="last_name" name="last_name" placeholder="Last Name" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="year_born" class="col-2 col-form-label">Year Born</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="year_born" name="year_born" placeholder="Year Born" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="last_four" class="col-2 col-form-label">Last Four of SSN</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="last_four" name="last_four" placeholder="Last Four" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div>
  <div class="form-group row">
    <label for="location" class="col-2 col-form-label">Location</label> 
    <div class="col-10">
      <select id="location" name="location" class="custom-select" required="required">
        <option value="">Please select a location</option>
          @foreach ($locations as $location)
        <option value="{{ $location->locations }}">{{ $location->locations }}</option>
        @endforeach
      </select>
    </div>
  </div>
  <div class="form-group row">
    <label for="email" class="col-2 col-form-label">Email</label> 
    <div class="col-10">
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupPrepend"><i class="fa fa-chevron-right"></i></span>
        </div> 
        <input id="email" name="email" placeholder="Email" type="text" class="form-control here" required="required">
      </div>
    </div>
  </div> 
  <div class="form-check">
    <input type="checkbox" class="form-check-input" id="MED">
    <label class="form-check-label" for="MED">Employee?</label>
  </div>
    <input name="module" type="hidden" value="Awareness Walks" id="module" />
  <div class="form-group row">
    <div class="offset-2 col-10">
      <button name="submit" type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>
                        {!! Form::close() !!}

And my controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Listing;
use App\Location;
use App\Module;
use DB;
use Yajra\Datatables\Datatables;

class ListingsController extends Controller
{
    
    public function __construct()
    {
        $this->middleware('auth'); 
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    // Pull all records from DB and modify route and controller
    public function getData() 
    {
       
     }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       $locations = Location::orderBy('locations', 'asc')->get();
   
       return view('createlisting', compact('locations'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|digits:4',
            'last_four' => 'required|numeric|digits:4',
            'location' => 'required',
            'email' => 'required|email',
            
        ]);
        
        // create record
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        
        $listing->save();

        $MED = $request->input('MED' == 1);
        $listing = DB::connection('mysql_external'){
        $listing->first_name = $request->input('first_name')
        $listing->last_name = $request->input('last_name'),
        $listing->year_born = $request->input('year_born'),
        $listing->last_four = $request->input('last_four'),
        $listing->location = $request->input('location'),
        $listing->email = $request->input('email'),
        $listing->module = $request->input('module'),
        $listing->user_id = auth()->user()->id 
        $listing->save();
        
        };
        return redirect('/dashboard')->with('success', 'Module Completed');
        
    }

I have tried multiple ways by searching online and the docs, just not sure how to implement it. Anyone give me any pointers or relevant docs online that is about what i'm trying to do specifically? Thank you in advance!

Aug
28
5 months ago
Activity icon

Replied to Min Max

Ok looks like using digits:4 achieves what I need, in case someone else is looking for the same!

Activity icon

Replied to Min Max

That might work for my year_born field but for the last 4 it will be different. I just want to validate that the field only has 4 numbers of any combination.

Activity icon

Replied to Min Max

Tried the between like below, from reading the docs: between:min,max The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule. Using a four digit number it throws the error: The year born must be between 3 and 5. I got to be missing something here.

$request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|between:3,5',
            'last_four' => 'required|numeric|min:4',
            'location' => 'required',
            'email' => 'required|email',
        // ], [
        //     'first_name.required' => 'Name is required',
        //     'first_name.min' => 'Name must be at least 2 characters.',
        //     'first_name.max' => 'Name should not be greater than 50 characters.',
        //     'year_born.size' => 'Year born and Last four should only be 4 numbers',
            
        ]);
Activity icon

Started a new Conversation Min Max

Trying to add a minimum and a maximum numeric size to my validation. I only want 4 numbers in each of the two fields, year_born and last_four. They are to only have a numeric 4 digit number. I have tried min:4|max:4 and size:4 and it throws an error on every submit like the below. If the user enter any 4 digit number I want it to go into the database, but 3 or fewer or 5 or more numbers throw an error.

The year born may not be greater than 4. I have even changed max:4 to max:5 and the error says The year born may not be greater than 5

In my controller, I have tried different things and just can't seem to get the min and max to work, saw in another post about using size, but the same result:

Any idea why or what I'm doing wrong?

public function store(Request $request)
    {
        // $this->validate($request, [
        //     'first_name' => 'required',
        //     'last_name' => 'required',
        //     'year_born' => 'required',
        //     'last_four' => 'required',
        //     'location' => '',
        //     'email' => 'email'
        // ]);
        $request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|min:4|max:4',
            'last_four' => 'required|numeric|min:4|max:4',
            'location' => 'required',
            'email' => 'required|email',
        // ], [
        //     'first_name.required' => 'Name is required',
        //     'first_name.min' => 'Name must be at least 2 characters.',
        //     'first_name.max' => 'Name should not be greater than 50 characters.',
        //     'year_born.size' => 'Year born and Last four should only be 4 numbers',
            
        ]);
        
        // create record
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        
        $listing->save();
        
        return redirect('/dashboard')->with('success', 'Module Completed');
        
    }