public static function vincularServico(Request $req) { $contrato = Contrato::find($req->get('contrato')); $contrato->servicos()->attach($req->get('servico'), ['valor' => $req->get('valor')]); $contrato->save(); }
/* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('/', function () { return view('welcome'); }); Route::get('/teste', function () { return \App\Contrato::find(15102140483.0)->servicos()->get(); }); Route::group(['prefix' => 'admin'], function () { Route::get('/dashboard', function () { return view('admin.dashboard'); }); // Clientes Route::get('/cliente', ['as' => 'cliente.index', 'uses' => 'Admin\\ClienteController@index']); Route::get('/cliente/novo', ['as' => 'cliente.novo', 'uses' => 'Admin\\ClienteController@novo']); Route::get('/cliente/editar/{id}', ['as' => 'cliente.editar', 'uses' => 'Admin\\ClienteController@editar']); Route::post('/cliente/cadastrar', ['as' => 'cliente.cadastrar', 'uses' => 'Admin\\ClienteController@cadastrar']); Route::post('/cliente/atualizar', ['as' => 'cliente.atualizar', 'uses' => 'Admin\\ClienteController@atualizar']); Route::post('/cliente', ['as' => 'cliente.pesquisa', 'uses' => 'Admin\\ClienteController@pesquisar']); Route::post('/cliente/pesquisarCliente/', ['as' => 'cliente.pesquisa', 'uses' => 'Admin\\ClienteController@pesquisarAjax']); Route::post('/cliente/excluir', ['as' => 'cliente.excluir', 'uses' => 'Admin\\ClienteController@excluir']); Route::post('/cliente/adctelefone', ['as' => 'cliente.adctelefone', 'uses' => 'Admin\\ClienteController@adcTelefone']);
public function addServico() { if (request()->ajax()) { try { Contrato::vincularServico(request()); $contrato = Contrato::find(request()->get('contrato')); $html = view('admin.contrato.includes.servicos')->with('contrato', $contrato)->render(); return response()->json(['html' => $html]); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()]); } } else { return "Acesso negado"; } }