after creating my model with model::create facade I use fill method to change the value of "card_tb_name" property but new value doesnt record i should say the "card_tb_name" is declared fillable property .
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
/*------------------------------*/
class RegisteredUserController extends Controller
{
public function store(Request $request)
{
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'card_tb_name' => 'table_name',
]);
$user->fill(array('card_tb_name' => 'Amsterdam to Frankfurt'));
}
}
Try to save() after fill()
$user->fill(array('card_tb_name' => 'Amsterdam to Frankfurt'))->save();
Related
I'm trying to redirect the user to a 'dashboard' page after he has registered. However, when I dump and die the auth()-user(), it returns null. It should display the information of the authenticated user from my database.
Register Controller
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RegisterController extends Controller
{
public function NavRegPage(){
return view('regPage');
}
public function store(Request $request){
$data = request()->validate([
'username' => ['required', 'max:20'],
'password' => ['required', 'min:5', 'max:20' ],
'email' => ['required', 'email'],
'url' => ['required', 'url'],
'dob' => ['required']
]);
$user = new User();
$user->username = request('username');
$user->password = request('password');
$user->email = request('email');
$user->url = request('url');
$user->dob = request('dob');
$user->save();
auth()->attempt($request->only('email', 'password'));
return redirect()->route('dash');
}
Dash controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashController extends Controller
{
public function index(){
dd(auth()->user());
}
}
Web.php
use App\Http\Controllers\RegisterController as RegisterController;
use App\Http\Controllers\DashController as DashController;
use Illuminate\Support\Facades\Route;
Route::get('/register', [RegisterController::class, 'NavRegPage']) ->name('register');
Route::get('dash', [\App\Http\Controllers\DashController::class, 'index']) ->name('dash');
Route::get('/', function () {
return view('index');
});
Route::post('/register', [RegisterController::class, 'store'])->name('register');
I want the dd to show me the authenticated user information so I can continue with my work.
Thank you
in web.php
Route::get('dash', [\App\Http\Controllers\DashController::class, 'index']) ->name('dash')->middleware('auth');
apply ->middleware('auth'); then your auth() will work on that controller
i am new in Laravel, and i have an import list page (this page work very good), this import some list to put on database. But i want to save this list too.
Heres my code (CsvImport.php):
<?php
namespace App\Imports;
use App\Product;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Support\Facades\Auth;
class CsvImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
return Product::query()->forceCreate([
'name' => $row["name"],
'description' => $row["description"],
'category' => $row["category"],
'price' => $row["price"],
'image' => 'no-image.jpg',
'user_id' => Auth::user()->id,
]);
}
}
Some Ideas? :)
i did not find, but i think is better not save.
I simply want to add an action to a form and I am trying as follows:
{{ Form::open(['action'=> ['AuthController#login'], 'method'=>"POST",'class'=>'login-form']) }}
But I am getting the following error:
Action App\Http\Controllers\AuthController#login not defined. (View: D:\server\htdocs\PMS\resources\views\custom_auth\login.blade.php)
I configure laravel collective Html. Whats wrong in my code?
Update:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Auth;
class AuthController extends Controller
{
function show(){
return view('custom_auth.login');
}
public function login(Request $request){
print_r($request); exit;
$this->validate($request,[
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
]);
$user_data = array(
'email' => $request->get('email'),
'password' => $request->get('password')
);
if(Auth::attempt($user_data)){
return redirect('/dashboard');
}else{
return back()->with('error','Wrong Credential');
}
}
}
You don't need to put your action inside an array when using Form helper so try:
{{ Form::open(['action'=> 'AuthController#login', 'method'=>"POST",'class'=>'login-form']) }}
And of course, make sure that a public login() method exists inside your AuthController
Also, do not forget to add this in your routes file, routes/web.php:
Route::post('login', 'AuthController#login');
I hope it helps
Laravel Framework 5.4.35
Contacts Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactEmail;
class ContactsController extends Controller
{
public function index() {
return view('contact.index');
}
public function sendContact (Request $request) {
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
Mail::to('bump#bumpy.net')
->send(new ContactEmail($request));
return redirect('/contact/success');
}
public function success() {
return view('contact.success');
}
}
The Controller that extends:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
When it goes here:
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
I get this output:
(1/1) BadMethodCallException Method validate does not exist
I have the namespace, the classes to be used. The call to the method seems to be ok.
What am I missing?
Care to advise?
If I create a validator instance manually using the Validator facade.
It seems to validate.
You mention your using version 5.4. The method you're using to validats via the request is only from version 5.5.
So you will need to do it like...
$this->validate($request, [
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
Hope this helps. Check out the 5.4v docs rather than the, aster/5.5v
https://laravel.com/docs/5.4/validation#validation-quickstart
Laravel 5.4
$this->validate($request, [
Laravel 5.5
$request->validate([
My controller looks something like this
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Middleware\Role;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\PaymentRequest;
use App\User;
use App\Invoice;
use App\comments;
use Session;
use Validator;
class CollectionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function __construct(){
$this->middleware('role:collector'); // replace 'collector' with whatever role you need.
}
public function postPayment(PaymentRequest $request){
$insertPayment=Input::get();
$payment= new paymentrecieved();
$comment= new comments();
$payment->invoice_id=$insertPayment['invoiceid'];
$payment->recieved_amount=$insertPayment['recieved_amount'];
$payment->refno=$insertPayment['ref_no'];
$payment->date=$insertPayment['date'];
if($insertPayment['adjustmentmode']=='Option')
$payment->adjust_mode='NONE';
else
$payment->adjust_mode=$insertPayment['adjustmentmode'];
$payment->save();
$request->session()->flash('alert-success', 'Payment Has Been inserted Successfully');
return redirect('collection/payment/1');
//dd($insertPayment);
}
}
So in my form the Payment related details are in an array I want to know how I can create multiple row ? and save it all together once .
Thanks & Regards
Try this,
$result = Banner::create([
'name' => $name,
'status' => $status,
'description' => $description,
'category_id' => $category_id,
'ordering' => $ordering,
'link' => $link,
]);
$result = Banner::create($data);
You can pass a multidimensional array in that case you have to use insert
for(..){
$data[] = [
'name' => $name,
'status' => $status,
'description' => $description,
'category_id' => $category_id,
'ordering' => $ordering,
'link' => $link,
];
}
$result = Banner::insert($data)
note: this will not update/create timestamps, so you have to pass that too with insert array.
Hope it helps..