Muhammad Rifqi
Muhammad Rifqi

Reputation: 21

laravel passing data from table to table

so i have a form that input data with <option>. the value is come from one database i provide. ( i can make the value from blade.php but the requirement require it from db). and the form will send the data to the main database. i've already make the view and the <option> work. but the trouble comes when i want to submit the data to the main database.

-the main db = blogs ( target column = 'sistem')

-the option db = sistems ( source column = 'nama')

the goal is the value of 'nama' passed to the 'sistem'

this is the view form

<form action="/" method="post" enctype="multipart/form-data">
  // .......
  // .......
          <select name="sistem[]" id="tag_select">
              <option value="0"> Tidak Ada </option>
              @foreach ($sistems as $sistem)
                  <option value="{{$sistem->id}}"> {{$sistem->nama}}</option>
              @endforeach
          </select>
  // .......
  // .......
   </form>

this is the store controller

public function store(Request $request)
{
  // .........
  // .........
  $request -> sistem = array_unique(array_diff($request->sistem, [0]));
  $blog -> sistem      = $request -> sistem;
  // .........
  // .........
  $blog -> save();
}

Upvotes: 0

Views: 958

Answers (3)

Filip Dakowicz
Filip Dakowicz

Reputation: 21

I will show how i store things via post method and it works for me, but you must know that I am noob. Sory if what i writting is obvious for you, but cant do anything more :)

 public function store(request $request)
{
    $this->validatePost();
    $post = new Post(request(
        ['title', 'excerpt','deadline']
    ));
    $post->save();
    return redirect(route('admin.posts'));
}

Upvotes: 0

Filip Dakowicz
Filip Dakowicz

Reputation: 21

Like above and also are you shure that action "/" points to store method?

Upvotes: 1

ThorntonStuart
ThorntonStuart

Reputation: 139

The save() method will save the changes that you make to your Eloquent model. Also you shouldn't need to edit the $request object to achieve the required result here.

Additionally you will need to ensure that this property on your model can be mass assigned (either in the $fillable array or excluded from the $guarded array on the $blog model).

$blog->sistem = array_unique(array_diff($request->sistem, [0]));
$blog->save();

Upvotes: 0

Related Questions