jhansi
1570
12
Laravel

Undefined variable: countries (View: C:\xampp\htdocs\list\resources\views\vendor.blade.php)

Posted 2 years ago by jhansi

app/Country.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;


class Country extends Model
{
    
   use Notifiable;
   protected $fillable = [
        'country_name', 'country_code ',
    ];
    Public function vendor()
    {
        return $this-> belongsTo(Vendor::class);
    }
}

app/Vendor.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;


class Vendor extends Model
{
    protected $primaryKey = 'Vendor_id';
    use Notifiable;
    protected $fillable = [
        'Vendor_id', 'Vendor','Date' ,
    ];
    Public function country(){
        return $this->hasOne('App\Country');
    }
}

vendor.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
     {!! Breadcrumbs::render('vendors/listing.vendors/add') !!}
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/increment') }}">
                        {{ csrf_field() }}
                    <div class="form-group{{ $errors->has('Vendor') ? ' has-error' : '' }}">
                        
                            <label for="Vendor" class="col-md-4 control-label">Vendor Name</label>
                            
                     
                            <div class="col-md-6">
                             <input id="Vendor" type="text" class="form-control" name="Vendor" value="{{ old('Vendor') }}" required>
                            
                                @if ($errors->has('Vendor'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('Vendor') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                        <div class="form-group{{ $errors->has('Date') ? ' has-error' : '' }}">
                            <label for="Date" class="col-md-4 control-label">Date</label>

                            <div class="col-md-6">
                                <input id="Date" type="Date" class="date form-control" name="Date" value="{{ old('Date') }}" required>
                                @if ($errors->has('Date'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('Date') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>
                         <?php echo Form::select('country', $countries, Null, ['class' => 'form-control']); ?>
                        

                              <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                    Submit
                                </button>
                                <button type="submit" onclick="window.location='{{ url("home") }}'" class="btn btn-primary">
                                    Back
                                </button>
                            </div>
                        </div>
                   </form>
                </div>
                @endsection
                @section('js')
<script type="text/javascript">  

                                $('.date').datepicker({  

                                format: 'yyyy-mm-dd',
                                autoclose: true

                                });  

                                </script>
            </div>
        </div>
    </div>
</div>
@endsection

vendorcontroller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Vendor;
use Illuminate\Support\Facades\Redirect;
use Carbon\Carbon;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Seeder;
use App\Country;

class VendorsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function indro()
    {
        $vendors = Vendor::paginate(10); // To use $vendors->links(), you need paginate();
        return  view('indro')->with('vendors', $vendors);
    }

  
    public function hike()
    {
       echo "Your Table is as follows:";
         $vendor = new Vendor;

    $vendor->Vendor = Input::get('Vendor');
    $vendor->Date = Input::get('Date');
   $countries = Country::orderBy('countries')->pluck('country_name', 'country_code');
   $countries = $countries->prepend('Please Select a Country', '');

       return view('vendor', compact(
         'countries'
      ))->country();
    
    $vendor->save();
        return Redirect::to('vendors/listing');
    }
      /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
      public function create()
    {
        return View::make('vendor');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
          DB::table('vendors')->insert($request->all());
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $Vendor_id)
    {
        //
        echo "Your Table is as follows:";
        
        $evendor = Vendor::find($Vendor_id);
        $evendor->Vendor = $request->get('Vendor');
        $evendor->Date = $request->get('Date');
        $evendor->save();
                return Redirect::to('vendors/listing');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function delete($Vendor_id)
    {
        Vendor::find($Vendor_id)->delete();
        return Redirect::to('vendors/listing');

    }

    public function findRecord($Vendor_id)
    {
        $findvendor = Vendor::find($Vendor_id);
            return view('vendoredit')->with('findvendor', $findvendor);
    }

    public function showDateDiff(Request $request)
    {
        $fd = new Vendor;
        $td = new Vendor;
        $fd=$request->input('from_date');
        $td=$request->input('to_date');
        $vendors = Vendor::whereBetween('Date', [$fd, $td])->paginate(10);
        //$fetchs = Team::paginate(10); // To use $teams->links(), you need paginate();
            return view('indro')->with('vendors', $vendors);
    }

    public function today()
    {
        $vendors = Vendor::where('Date', '=', Carbon::today()->toDateString())->paginate(10);
            return view('indro')->with('vendors', $vendors);
    }

    public function sevenday()
    {
        $date = new Carbon;
        $date = Carbon::now()->subWeek();
        $vendors = Vendor::where('Date', '>', $date->toDateString())->paginate(10);
            return view('indro')->with('vendors', $vendors);
    }
}

two migrations of country

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddCountriesToVendorsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('vendors', function (Blueprint $table) {
            $table->string('country')->nullable();
            
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('vendors', function (Blueprint $table) {
            $table->dropColumn('country');

        });
    }
}
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCountriesTabe extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::dropIfExists('countries');

        Schema::create('countries', function (Blueprint $table) {
            $table->increments('id');

            $table->string('country_name');
            $table->string('country_code', 3);
           
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
       Schema::drop('countries');

    }
}

databaseseeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
      

        DB::unprepared(file_get_contents(base_path('database/seeds/sql/countries.sql')));
    }
}

Please sign in or create an account to participate in this conversation.