For some reason I am unable to validate data - php

I am trying to validate my data but for some reason I am getting this error
" Trying to get property 'title' of non-object"
Here's My Controller:-
public function store(Request $request)
{
$data = request()->validate([
'title' => 'required',
'body' => 'required',
]);
Post::create([
'title'=>$data->title,
'body'=>$data->body,
'created_by'=>$request->created_by,
'user_id'=>Auth::user()->id,
'filled_by'=>Auth::user()->uuid,
]);
return redirect('/home');
}

request()->validate([]); will return Array with validated data. You are using $data->title but $data is NOT an Object but Array.
Instead use
'title' => $data['title'],

Related

How to save data from result where eloquent Laravel

I want to save data returned from 'where' eloquent laravel.
My code:
public function duplicate_save(Request $request){
$this->validate($request, [
'bulan_from' => 'required',
'tahun_from' => 'required',
'bulan_to' => 'required',
'tahun_to' => 'required',
]);
$realisasi_keuangan = RealisasiKeuangan::where('bulan', $request->bulan_from)->where('tahun', $request->tahun_from)->get();
RealisasiKeuangan::create($realisasi_keuangan);
return redirect()->route('apps.realisasi-keuangan.index');
}
But the code return error
Illuminate\Database\Eloquent\Builder::create(): Argument #1 ($attributes) must be of type array, App\Models\RealisasiKeuangan given

Call to a member function store() on null Laravel 8

When I click on save to update the data on my edit page, I get the following error.
call to a member function store() on null
public function update(User $user)
{
$this->authorize('update', $user->profile);
$data = request()->validate([
'title' => 'required',
'description' => 'required',
'url' => 'url',
'image' => '',
]);
$imagePath = request('image')
->store('profile', 'public');
$image = Image::make(public_path("storage/{$imagePath}"))
->fit(1000, 1000);
$image->save();
dd($data);
auth()->user->profile->update(array_merge(
['image' => $imagePath]
));
return redirect("/profile/{$user->id}");
}
As a result of $request->file('file') is returning null, and you are trying to call a method on null, resulting in the exception.
Due to the way Laravel handles PUT and PATCH requests, you will need to send your request in a POST request, and supply _method with the value PUT in the header. This is what Laravel expects.
'image' => 'required'

Laravel saving data to two locations

I'm working on a larvel project where the user can create appointments. In addition I've created another model called clients so when a user creates an appointment the users "client" data is saved.
In my appointments controller I have the following: -
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
//create appointment
$apt = new Appointment;
$apt->name = $request->input('name');
$apt->user_id = auth()->user()->id;
$apt->save();
//create client
$client = new Client;
$client->first_name = $request->input('name');
$client->user_id = auth()->user()->id;
$client->save();
return redirect('/appointments')->with('success', 'Appointment created');
}
When saving the data it works and stores the data in the clients table however I know this isn't the cleanest way of saving the data, but what is the "laravel" way of doing this?
There's nothing wrong with your code. It's totally fine keeping it that way.
I prefer to say Model::create() to create models in one statement.
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
Appointment::create([
'name' => request('name'),
'user_id' => auth()->id()
]);
Client::create([
'first_name' => request('name'),
'user_id' => auth()->id,
]);
return redirect('/appointments')->with('success', 'Appointment created');
}
You can also use tap() function:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
tap(Appointment::create(['name' => request('name'), 'user_id' => auth()->id()]), function ($newAppoinment) {
Client::create([
'first_name' => $newAppoinment->name,
'user_id' => auth()->id,
]);
});
return redirect('/appointments')->with('success', 'Appointment created');
}
Or the best approach could be using model events:
class Appointment extends Model
{
public static function boot()
{
parent::boot();
static::created(function ($appointment) {
Client::create([
'user_id' => $appoinment->user_id,
'first_name' => $appoinment->name,
])
});
}
}

SQL default value error when uploading files using laravel

i'm trying to upload multiple images using laravel and so far i've succeded, but the problem is that when i try to save text AND the files i get an error.
So far i've received so many errors that i can't remember all of then, but the latest is:
SQLSTATE[HY000]: General error: 1364 Field 'title' doesn't have a default value (SQL: insert into posts (image, updated_at, created_at) values (["italian.jpg"], 2019-05-23 18:48:22, 2019-05-23 18:48:22))
i've set the sql properly, and as i said, if i remove the image upload it works, and if i remove the text fields it also work, but if i try both i doesnt.
if i remember correctly when i remove the required fields it also works.
public function store(Request $request)
{
//dd($request);
$this->validate($request, [
'title' => 'required|min:3|max:120',
'text' => 'required',
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if($request->hasfile('image')){
foreach($request->file('image') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
} else{
redirect('/posts')->with('Error', 'no image');
}
$post->image=json_encode($data);
$post = Post::create($validatedData);
return redirect('/posts')->with('success', 'yay');
}
i also tried this, but it returns
Creating default object from empty value
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|min:3|max:120',
'category' => 'required|min:3|max:120',
'text' => 'required',
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if($request->hasfile('image'))
{
foreach($request->file('image') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$post = new Post();
$post->image=json_encode($data);
$post->save();
return back()->with('success', 'Yay');
}
i figured it out, hehe. just had to remove these lines:
'image' => 'required',
'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
Thanks anyways.

Laravel Put/Patch Request

Im trying to do a Put/Patch Request, I am using Postman, this is my current Code:
class CustomerController extends Controller
{
public function getAllCustomer()
{
return Customer::get();
}
public function addNewCustomer(Request $request)
{
$validatedData = $request->validate([
'Title' => 'required',
'Name' => 'required|max:255',
'Surname' => 'required|max:255',
'Email' => 'required',
'Phone' => 'required',
'Password' => 'required',
'dateofBirth' => 'required'
]);
return \app\model\Customer::create($request->all());
}
public function update (Request $request , Customer $id)
{
$id->update($request->all());
}
And this my route:
Route::put('Customer/{id}' , 'CustomerController#update');
Im trying to insert some Parameters into Postman, but I think the way I do it is not correct, right now I do it like this:
Im not getting any Errors, but nothing is happening, maybe somebody knows a solution.
I want to Change the Name of the customer.
Thanks!
Try to set x-www-form-urlencoded for body in postman.

Categories