mardi 16 juillet 2019

Laravel multi-choice checkbox for a many to many relationship (crud)

After making the many-to-many relationship in my database I now moved on to try and implement the changes to the project itself. I am having a hard time doing this.

Essentially what I want is to add the multi-choice checkbox functionality to my Edit and Create to already set-up blade.php pages.

My Product Model (keep in mind the 'shipping_id' is the thing I want replaced with the multi-choice checkbox):

    class Product extends Model
    {
    //

    protected $fillable = [

    'category_id',
    'location_id',
    'shipping_id',
    'photo_id',
    'title',
    'body',
    'price',
    'availability',
    'quantity'
    ];


    public static $rules = array(
    'category_id'=>'required|integer',
    'location_id'=>'required|integer',
    'shipping_id'=>'required|integer',
    'title'=>'required|min:2',
    'body'=>'required|min:20',
    'price'=>'required|numeric',
    'availability'=>'integer|between:0,1',
    'quantity'=>'integer|between:1,99'
    );



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


    public function photo(){
    return $this->belongsTo('App\Photo');
    }


    public function category(){
    return $this->belongsTo('App\Category');
    }


    public function location(){
    return $this->belongsTo('App\Country');
    }

    public function shipping(){
    return $this->belongsTo('App\Country');
    }


    public function comments(){
    return $this->hasMany('App\Comment');
    }

    //experimental shipping
    public function countries(){
    return $this->belongsToMany('App\Country');
    }
}

My Country Model:

    class Country extends Model
    {
    //

    protected $fillable = ['name'];

    public static $rules = array('name'=>'required|min:3');

    //experimental shipping
    public function products(){
    return $this->belongsToMany('App\Product');
    }

    }

This is my Admin Product Controller:

    public function createEntry(ProductsCreateRequest $request)
    {
    //

    $input = $request->all();
    $user = Auth::user();
    $validator = Validator::make(Input::all(), Product::$rules);

    if($file = $request->file('photo_id')){
    $name = time() . $file->getClientOriginalName();
    $file->move('images', $name);
    $photo = Photo::create(['file'=>$name]);
    $input['photo_id'] = $photo->id;
    }


    if($validator->passes()){

    $user->products()->create($input);
    $product->countries()->sync(Input::get('countries'));
    Session::flash('created_listing','The listing has been created');
    return redirect('/admin/products');
    }

    return Redirect::to('admin/products/create')->withErrors($validator)- 
    >withInput();


    }


    public function editEntry($id)
    {
    //

    $product = Product::findOrFail($id);
    $categories = Category::pluck('name', 'id')->all();
    $countries = Country::pluck('name', 'id')->all();


    return view('admin.products.edit', compact('product', 'categories', 
    'countries'));
    }

And finally my create.blade.php for product:

```<h1>Create Products</h1>

<div class="row">


{!! Form::open(['method'=>'POST', 'action'=> 
'AdminProductsController@store','files'=>true]) !!}


<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class'=>'form-control'])!!}
</div>

<div class="form-group">
{!! Form::label('location_id', 'Item location:') !!}
{!! Form::select('location_id', [''=>'Choose Country'] + 
$countries, null, ['class'=>'form-control'])!!}
</div>

<div class="form-group">
{!! Form::label('shipping_id', 'Shipping to:') !!}
{!! Form::select('shipping_id', [''=>'Choose Country'] + 
$countries, null, ['class'=>'form-control'])!!}
</div>


<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id', [''=>'Choose Category'] + 
$categories, null, ['class'=>'form-control'])!!}
</div>


<div class="form-group">
{!! Form::label('availability', 'Availability:') !!}
{!! Form::select('availability', ['1'=>'In Stock', '0'=>'Out Of 
Stock'], null, ['class'=>'form-control'])!!}
</div>

<div class="form-group">
{!! Form::label('quantity', 'Quantity:') !!}
{!! Form::number('quantity', null, ['min'=>1,'max'=>99], 
['class'=>'form-control'])!!}
</div>


<div class="form-group">
{!! Form::label('price', 'Price:') !!}
{!! Form::text('price', null, array('class'=>'form-price')) !!}
</div>


<div class="form-group">
{!! Form::label('photo_id', 'Photo:') !!}
{!! Form::file('photo_id', null, ['class'=>'form-control'])!!}
</div>


<div class="form-group">
{!! Form::label('body', 'Description:') !!}
{!! Form::textarea('body', null, ['class'=>'form-control'])!!}
</div>

<div class="form-group">
{!! Form::submit('Create Product', ['class'=>'btn btn-primary']) !!}       
</div>

{!! Form::close() !!}

</div>

@include('includes.form_error')



My 3 tables are: 

countries, products and country_product (with 'product_id' and 'country_id') - the relationship is already set and working.

In this state everything works.
So my question here is. How do I add the multi-choice checkbox functionality to the already set create and edit pages, so instead of 'shipping_id' in products table I can have an option for the user to select more than one countries the product can ship to?

P.S. I asked something similar yesterday (but it was related to the relationship only which I somehow managed to set-up now). 
Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire