0

I have a problem that I want to create a drop-down list from the city table and display in pack view. How can I create city_name in pack view?

thanks

Here is my code _create.blade.php

@extends('layouts.admin.app')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-sm-6 col-md-6 col-lg-6">
            <div class="card">
                <div class="card-header">Create Pack Master</div>
                <div class="card-body">
                    <form action="{{ route('packs.store') }}" method="post">
                        @include('packsmaster._form', ['buttonText'=>'Save', 'inputName'=>'Pack Name'])
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

_form.blade.php

@csrf
<div class="form-group">
    <label for="pack-name" class="col-md-4 col-form-label">{{ $inputName }}</label>
    <input id="pack-name" type="text" name="pack_name" class="form-control @error('pack_name') is-invalid @enderror" 
        value="{{ old('pack_name', $pack->pack_name) }}" autocomplete="pack_name" autofocus>
    @error('pack_name')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
    @enderror
</div>
<div class="form-group">
    <label for="city_name">City Name</label>
    <select class="form-control" id="city_name">
        @foreach ($cities as $city)
            <option>{{ $city->city_name }}</option>
        @endforeach
    </select>
</div>
<div class="form-group">
    <button type="submit" class="btn btn-outline-secondary">{{ $buttonText }}</button>
    <a href="{{ route('packs.index') }}" class="btn btn-outline-danger">Cancel</a>
</div>

This is a form from pack and i am trying to get city_name from City table. but I think I can't directly call that. I want to display city_name in create pack master with dropdown list.

PacksController.php

<?php

namespace App\Http\Controllers;

use App\Pack;
use App\City;
use Illuminate\Http\Request;
use App\Http\Requests\PackRequest;

class PacksController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $packs = Pack::latest()->paginate(5);
        return view('packsmaster.index', compact('packs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $pack = new Pack();
        return view('packsmaster._create', compact('pack'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PackRequest $request)
    {
        // $request->user()->packs()->create($request->only('pack_name'));
        // return redirect('packs')->with('success', "your data has bee saved!!");
        $request->user()->packs()->create($request->only('pack_name'));
        return redirect('packs')->with('success', "your pack has been saved!!");
    }
albus_severus
  • 3,626
  • 1
  • 13
  • 25

1 Answers1

0

Load cities in the controller?

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cities = City::all();
        $pack = new Pack();
        return view('packsmaster._create', compact('pack','cities'));
    }

Now that you have the list of cities in the view, iterate over them adding the id of the city as the value of the option. Be sure to name the form element, ideally with the same name as the column you want to populate.

    <select class="form-control" name="city_id">
        @foreach ($cities as $city)
            <option value="{{ $city->id }}">{{ $city->city_name }}</option>
        @endforeach
    </select>

Now in the controller, you can save the ID to the database

    $request->user()->packs()->create($request->only(['pack_name','city_id']));

At some point, you will want to add some validation !

Snapey
  • 3,604
  • 1
  • 21
  • 19
  • What does that even mean? Errors? Nothing displayed? what? – Snapey Jan 29 '20 at 10:04
  • NOO... it actually error.. and i have no idea of that ... what i mean is i want to store data from the different table along with new input... i did but it keeps shows error... SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'city_id' cannot be null (SQL: insert into `packs` (`pack_name`, `user_id`, `city_id`, `updated_at`, `created_at`) values (b, 1, ?, 2020-01-30 03:47:49, 2020-01-30 03:47:49)) – Michael William Jan 30 '20 at 03:51