vendredi 20 novembre 2015

how to update a foreign key column in database table using model laravel 5.1

im a beginner at laravel. never worked with framework before. i have created a database named 'tabletest'. it has two table. one is users table and other is phones table.users table has two column(id and name). phones table has 3 column(id phones and users_id). what i was trying is, i will take input using form and send the inputs to the database tables. though the names and the phones were saved in different tables properly, the users_id, which is the foreign key column, was not updated. it was always 0. what should i do now?

here is my migration files:

2015_11_19_083827_create_users_table.php

    <?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

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

2015_11_19_083943_create_phones_table.php

<?php

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

class CreatePhonesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('phones', function (Blueprint $table) {
            $table->increments('id');
            $table->string('phone');
            $table->unsignedInteger('users_id');
            $table->timestamps();
        });
    }

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

Models are

Users.php

<?php

namespace App\model;

use Illuminate\Database\Eloquent\Model;

use App\model\Phone;

class Users extends Model
{
   protected $table = "users";
   protected $fillable = ['name'];

   public function phone(){
    return $this->hasOne(Phone::class);
   }

}

Phones.php

<?php

namespace App\model;

use Illuminate\Database\Eloquent\Model;

use App\model\Users;

class Phones extends Model
{
    protected $table = "phones";

    protected $fillable = ['phone',
                           'users_id'];

    public function users(){
        return $this->belongsto(Users::class);
    }
   }

Controller is

phoneController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Model\Phones;
use App\Model\Users;

class phoneController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {


    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       return view('home');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {


        Users::create([

            'name'  => $request->name

            ]);

        Phones::create([

            'phone'    => $request->phone
            ]);
    }

    /**
     * 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($id)
    {
        //
    }

    /**
     * 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)
    {
        //
    }
}

screenshot of phones table



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire