laravel 5 Call to undefined method Illuminate\Database\Query\Builder::posts() - laravel-5

i want to store this information but that doesn't work i still have this error "Call to undefined method Illuminate\Database\Query\Builder::posts()
"
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required',
'slug'=>'required',
'excerpt'=>'required',
'body'=>'required',
'created_at'=>'date_format:Y-m-d H:i:s',
'team_id'=>'required',
'image'=>'mimes:jpg,jpeg,png,bmp',
]);
$data = $this->handleRequest($request);
$request->user()->posts()->create($data);
return redirect('/backend/blog')->with('message', 'Your post was created successfully!');
}
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image'))
{
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$image->move($destination, $fileName);
$data['image'] = $fileName;
}
return $data;
}

Related

Laravel : Call to a member function move() on string

I am trying to convert a base64 into png using explode() function, and then pass the file to the "uploads" folder, but it's not working. My Code are :
In my laravel controller :
public function verify(Request $request){
$img = $request->image;
$image_parts = explode(";base64,", $img);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$fileName = uniqid() . '.png';
$request->image->move(public_path('uploads'), $image_base64);
return redirect('verification');
}
$data = 'data:image/png;base64,AAAFBfj42Pj4';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('/tmp/image.png', $data);

laravel5 controller issue. Undefined variable: filename

I want to upload a blogger's profile image but I get an error:
Undefined variable: file_name.
I think it is a controller issue. I don't know why the variable is not defined, though I declared the same variable name.
I restarted the server but it did not work.
So please help me out.
BloggerController.php
public function store(Request $request, User $user_id)
{
$user_id = DB::table('users')->where('id', $user_id)->get();
if ($request->hasfile('image')) {
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$file_name = time() . '.' . $ext;
$file->move('bloggers/', $file_name);
}
$blog = Blog::create(
['user_id' => $user_id],
[
'image' => $file_name,
'introduction' => $request->introduction,
]
);
return redirect(route('blogger'));
}
I also changed the store method.
if ($request->hasfile('image')) {
$file_name = $request->file('image')->getClientOriginalName();
$request->file('image')->storeAs('bloggers/', $file_name);
}
My migration
Schema::create('blogs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('bloggers');
$table->string('image');
$table->string('introduction');
$table->timestamps();
});
To be sure that your variable $fillename is available then change your code to this
if($request->hasfile('image')){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = time().'.'.$ext;
$file->move('bloggers/', $filename);
$blog = Blog::create(
['user_id' => $user_id],
[
'image'=>$filename,
'introduction' => $request->introduction,
]
);
}
you must return file address and name in uploader
public function store(Request $request, User $user_id){
$user_id = DB::table('users')->where('id', $user_id)->get();
if($request->hasfile('image')){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$file_name = time().'.'.$ext;
$file->move('bloggers/', $file_name);
}
$blog = Blog::create(
['user_id' => $user_id],
[
'image'=>"/bloggers/".$file_name,
'introduction' => $request->introduction,
]
);
return redirect(route('blogger'));
}
i think its work
You tried accessing the variable out of scope. Make it an external variable by declaring it out of the if function as I show below on line 2 of the code.
...
public function store(Request $request, User $user_id){
$file_name="";
$user_id = DB::table('users')->where('id', $user_id)->get();
if($request->hasfile('image')){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$file_name = time().'.'.$ext;
$file->move('bloggers/', $file_name);
}
$blog = Blog::create(
['user_id' => $user_id],
[
'image'=>"/bloggers/".$file_name,
'introduction' => $request->introduction,
]
);
return redirect(route('blogger'));
}
...

Not updating database and not throwing error in laravel

I have an issue while updating the user. When I try to update the user after clicking the save button then it redirect me to the same page and not throwing me any error but also its not updating anything in the database. Below is my code. I have no idea what's going on here. Help me :)
Controller
public function update(ReportRequest $request, $id)
{
$report = Report::findOrFail($id);
$input = $request->all();
if ($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file' => $name]);
$input['photo_id'] = $photo->id;
}
$report->update($input);
return redirect()->back();
}
Route
Route::resource('admin/reports', 'ReportController', ['names'=>[
'index'=>'admin.reports.index',
'create'=>'admin.reports.create',
'edit'=>'admin.reports.edit',
]]);
Models
class Report extends Model
{
protected $fillable = [
'student_id',
'student_name',
'class_id',
'subject',
'teacher_name',
'report_categories_id',
'total_marks',
'obtained_marks',
'percentage',
'position',
'photo_id',
];
public function photo() {
return $this->belongsTo('App\Photo');
}
public function studentsClass() {
return $this->belongsTo('App\StudentsClass', 'class_id');
}
public function student() {
return $this->belongsToMany('App\Student');
}
}
Make sure you have your $fillable properties in your Photo and Report models, otherwise the create() and update() methods won't work as expected.
Check the $fillable fields in the Model as above. If the error persists check your laravel log on storage/logs/laravel.log.
In controller:
public function update(ReportRequest $request, $id){
$report = Report::findOrFail($id);
$input = $request->all();
try{
if ($request->photo_id != '') {
$path = 'images/';
$file = $request->photo_id;
$name = time() . $file->getClientOriginalName();
$file->move($path, $name);
$photo = Photo::create(['file' => $name]);
$report->update(['photo_id' => $photo->id]);
}
return redirect()->back();
}catch(\Exception $e){
return redirect()->back()->with('error_message', $e->getMessage());
}
}

Cannot Update On Laravel 5.5

I get problem.
This is my controller
public function finish(Request $request)
{
$result = $request->input('data');
//$data = json_decode($result, true);
return $this->InvoiceBayar($result);
}
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data['transaction_status'];
$type = $data['payment_type'];
$order_id = $data['order_id'];
$fraud = $data['fraud_status'];
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}
This is my Route
Route::POST('/notification', 'SnapController#finish');
When Payment gateway, send a parameter to me, I cannot update DB.
But when I use POSTMAN. I success update DB
You need to use $request->all() as it will contain all payment gateway data.
public function finish(Request $request)
{
$result = $request->all();
return $this->InvoiceBayar($result);
}
Alternately you can do this
$update = Fee::where('invoice',$order_id)->first();
$update->status = 'Paid';
$update->save();
You should try this:
public function InvoiceBayar($result)
{
$data = json_decode($result, true);
$transaction = $data->transaction_status;
$type = $data->payment_type;
$order_id = $data->order_id;
$fraud = $data->fraud_status;
Fee::where('invoice',$order_id)
->update([
'status' => 'Paid',
]);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}

Laravel 5 - Image Upload and Resize using Intervention Image Package

I want to upload a photo with some posts.
This is my controller
public function store(WisataRequest $request)
{
$input = $request->all();
if ($request->hasFile('gambar')) {
$gambar = $request->file('gambar');
$filename = time() . '.' . $gambar->getClientOriginalExtension();
if ($request->file('gambar')->isValid()) {
Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
$input->gambar = $filename;
$input->save();
}
}
$wisata = Wisata::create($input);
Session::flash('flash_message', 'Berhasil Terkirim');
return redirect('admin_wisata');
}
But when it runs i found an error
Attempt to assign property of non-object
Change
$input->gambar = $filename;
$input->save();
To
$input['gambar']= $filename;
$input variable is not an object, it is an array. You can try accessing gambar in $input by doing $input['gambar']
You can put
$input['gambar']= $filename;
Instead of
$input->gambar = $filename;
$input->save();
OR
public function store(WisataRequest $request)
{
$wista = new Wista;
$wist->name = $request->name;
-----
$wista->save();
if ($request->hasFile('gambar')) {
$gambar = $request->file('gambar');
$filename = time() . '.' . $gambar->getClientOriginalExtension();
if ($request->file('gambar')->isValid()) {
Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
$wista->gambar = $filename;
$wista->save();
}
}
Session::flash('flash_message', 'Berhasil Terkirim');
return redirect('admin_wisata');
}

Resources