S
S
sacred12015-12-30 11:04:37
MySQL
sacred1, 2015-12-30 11:04:37

How to implement ORM in Laravel?

Routing - routes.php file:

Route:post(/add,[
'as'=>'add',
'uses'=>'[email protected]'
]);

The controller itself AddController.php:
class AddController extends BaseController{
$data=Input::all()       //В $data записываем все сохраненные данные с нашей формы в виде ассоциативного массива
People::addPost($data);    //Вызываем функцию в нашей модели,где в качестве параметра у нас наша переменная $data
}

People.php Model:
Class People extends Eloquent{
public $timestamps = false;  // нету столбцов(с датой обновление и загрузки записей) которые по умолчанию в laravel
protected $fillable = array(
'name',
'surname',
'message '
); // добавляем доступ к добавлению к данным столбцам

public static function addPost($data){
$add = People::create($data); //Добавляем наши новые записи в бд
return $add;
}
}

Our view add.blade.php:
....
{{Form:open(array('url'=>action('[email protected]')))}}
{{Form:text('name',null)}}
{{Form:text('surname',null)}}
{{Form:text('message ',null)}}
{{Form:submit('Отправить',null)}}
{{Form::close()}}

And there are two tables 1) peoples (columns - id, name, surname) and 2) information ( with a foreign key, respectively, people_id and message ). Everything is fine when we add to only one table (in this case, name and surname). But how link the database to run a query to two tables? .

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Reistlin, 2015-12-30
@iznaur

I have not worked with Laravel, but if I understand you correctly, you need to link tables

Z
zugo, 2015-12-30
@zugo

In Eloquent, table relationships are written directly into the model:

сlass People extends Eloquent {
    public function information() {
        return $this->hasMany('Information')
    }
}

сlass Information extends Eloquent {
    public function people() {
        return $this->hasOne('People')
    }
}

Later, when a query requires a JOIN, Eloquent can solve this issue, for example, like this:
People::all()->with('information')->get();

D
Dmitry Evgrafovich, 2015-01-02
@Tantacula

And you don’t have the addPeople function in the AddController function and everything is written like that, as you posted it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question