vendredi 17 février 2017

Save select value onchange Laravel

I am building a multi language app with Laravel, and I need the users to be able to log in and select their preferred language(table "languages") from a select field and store in the database("language_id" in table "users") just by selecting it from the field. I have currently no idea how I can achieve this without a form and a submit button. Can somebody explain to me how I can properly do this?

Controller: All available languages are stored in a variable and send to all views.

namespace App\Http\Controllers;

use App\Language;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\View;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function __construct()
    {
        $languages = Language::all();

        View::share('languages', $languages);
    }

}

Menu select: A foreach loop that populates the select field with the languages send via the controller.

                <li>

                        <select class="form-control" id="language" name="language">
                            @foreach ($languages as $language)
                                <option value=""></option>
                            @endforeach
                        </select>
                </li>

Database: "users" table has "language_id"



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lqxSs2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire