Laravel file upload not working, and have no idea why - laravel

MODEL:
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class product extends Model
{
public function create(Request $request) {
$file = $request->file('photo');
if ( $request->hasFile('photo') && $request->file('photo')->isValid() )
{
$extension = $file->extension();
$name = 'bjdsakbhdebkhdabhkedbhe'.$extension;
$path = $file->storeAs('public/images',$name);
}
else {
return 'error';
}
product::create([
'photo' => $path,
]);
}
protected $fillable = ['name', 'price', 'roast', 'origin', 'photo', 'stock'];
}
CONTROLLER
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\product;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
class adminController extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function create(Request $request) {
ini_set('max_execution_time', 300);
$validatedData = $request->validate([
'photo' => 'required|file|image'
]);
$new = new product;
$new->create($request);
}
}
I am trying to upload a file image. I have reworked the above code several times and an error is thrown. Absolutely NO idea why the file is not uploading. It is not a server error. File size and time allowed have been adjusted.

Why are you calling product::create inside your create method in product class? This causes an infinite recursion.

Related

How to pass variable from Controller to Nova Recource?

I want to pass $defaultFrom from NewsletterController.php:
<?php
namespace App\Http\Controllers;
use App\Mail\NewsletterMail;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class NewsletterController extends Controller
{
public function send()
{
$defaultFrom = 'newsletter#stuttard.de';
DB::table('newsletter_mails')->insert(['from' => $defaultFrom]);
$emails = DB::select('select * from newsletters order by id desc');
foreach ($emails as $email) {
Mail::to($email)->send(new NewsletterMail());
}
}
}
to NewsletterMail.php:
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
class NewsletterMail extends Resource
{
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('From', 'from')->default($defaultFrom)->placeholder($defaultFrom),
];
}
}
I've tried to put public $defaultFrom; above the fields() function or call new NewsletterMail($defaultFrom) but this seems to be wrong syntax. Sorry, I'm a bit new to Laravel.
I assume that you have Newsletter model. Move $defaultFrom to model as public const DEFAULT_FROM = 'newsletter#stuttard.de';. After doing this, you can call it's value in both places using Newsletter::DEFAULT_FROM.

I can't update the field in laravel 5

It is OK in Get, Post, Delete in my laravel code.
But I can't update the field.
function update in BookController.php
$data = $this->request->all();
If show the dd($data), it is null.
What reason?
Help me please.
BookRequest.php Code:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BookRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|max:255',
'coment' => 'required'
];
}
}
BookController.php Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use Illuminate\Http\Response;
use App\Http\Requests\BookRequest;
class BookController extends Controller
{
protected $request;
protected $book;
public function __construct(Request $request, Book $book) {
$this->request = $request;
$this->book = $book;
}
public function update(BookRequest $request, $id) {
$data = $this->request->all();
$book = $this->book->find($id);
$book->name = $data['name'];
$book->coment = $data['coment'];
$book->save();
return response()->json(['status' => Response::HTTP_OK]);
}
}
If i were you i would replace the Controller like below:
<?php
namespace App\Http\Controllers;
use App\Book;
use Illuminate\Http\Response;
use App\Http\Requests\BookRequest;
class BookController extends Controller
{
public function update(BookRequest $request, $id) {
$book = Book::find($id);
$book->update($request->all());
return response()->json(['status' => Response::HTTP_OK]);
}
}
If you have set up Route:model binding then you can simplify Code more better. Below code only works if you have a Route::model setup in your route file web.php.
Check this docs for more details:
https://laravel.com/docs/5.6/routing#route-model-binding
public function update(BookRequest $request, Book $book) {
$book->update($request->all());
return response()->json(['status' => Response::HTTP_OK]);
}
Try this:-
$request->all();
instead of
$this->request->all()
I have solved.
My request: http://127.0.0.1:8000/api/book
POST , key: _method: PUT
Update Code
$data = $request->all();
$book = Boook::find($id);
$book->name = $data['name'];
$book->coment = $data['coment'];
$book->save();
Regards.

Laravel : Extends Controller not found

when trying to get data and returns it via transformers got an error Class 'App\\Api\\V1\\Controllers\\Auth\\ApiController' not found. Using ApiController to extends.I put APIcontroller to App\\Api\\V1\\Controllers\\Front folder.
LoginController Code and extends it to ApiController:
<?php
namespace App\Api\V1\Controllers\Auth;
use Auth;
use Carbon\Carbon;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\User;
use App\UserDeviceData;
use Illuminate\Http\Request;
use App\Transformers\UserTransformer;
class LoginController extends ApiController // extends to API controller
{
public function login(LoginRequest $request, JWTAuth $JWTAuth)
{
$credentials = $request->only(['email', 'password']);
try {
$token = Auth::guard()->attempt($credentials);
if(!$token) {
return response()->json([
'message' => "Email and password do not match",
'status_code' => 403,
]);
}
$user = Auth::user();
$user->last_login = Carbon::now();
$user->save();
$user = Auth::user();
$user->UserDeviceData()->firstOrCreate([
'device_id' => $request->device_id
]);
} catch (JWTException $e) {
return response()->json([
'message' => "Internal server error",
'status_code' => 500,
]);
}
return $this->response->collection($user, new UserTransformer);
}
}
Api controller code : and set namespace App\Api\V1\Controllers\Front;
<?php
namespace App\Api\V1\Controllers\Front;
use App\Support\Response;
use App\Support\Parameters;
abstract class ApiController extends Controller
{
protected $response;
protected $parameters;
public function __construct(Response $response, Parameters $parameters)
{
$this->response = $response;
$this->parameters = $parameters;
}
}
What is the problem in my code?
As ApiController and LoginController are using a different namespace, you need to use your ApiController in LoginController.
use App\Api\V1\Controllers\Front\ApiController;
You also forgot to use Controller in ApiController:
use App\Http\Controllers\Controller;

laravel Class 'App\Categories' not found

When I try to call a function with categories data $categories = Categories::all(); it returns an error that Class 'App\Categories' not found
Here is my CategoriesController code:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller as BaseController;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Categories;
class CategoriesController extends BaseController
{
public function listcategories() {
$categories = Categories::all();
dd($categories);
return view('admin.list_categories')->with('listcategories',
$categories);
}
Here is my Categories model code ;
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class Categories extends Model
{
protected $fillable = [
'title',
'description',
'flag_image',
'status'
];
const STATUSES = [
'Active' => 'Active',
'Inactive' => 'Inactive',
];
const DEFAULT_STATUS = 'Active';
/**
* Indicates if the model should be timestamped.
*
* #var bool
*/
public $timestamps = false;
public static function rules() {
return [
'title' => 'required|string|max:255',
'status' => 'required|string|in:' . implode(",", Categories::STATUSES)
];
}}
What is the problem ? I can't get it.
Because models are autoloading via composer
in some cases you need to run
composer dump-autoload after changes in order to make it work

Undefined property: Illuminate\Validation\Validator::$errors in laravel

Undefined property: Illuminate\Validation\Validator::$errors in laravel
here is my controller file how to solve it i think here problem is any namespace where is it i do not know please guide me
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
use Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
use App\models\Designation;
use Validator;
use Illuminate\Support\Facades\Response;
class Cdesigination extends Controller
{
public $flight;
public function __construct(){
$this->flight = new Designation;
}
public function index()
{
return view('designation');
}
public function techer(Request $request) {
$Validator =Validator::make(array(
'name'=>Input::get('name'),
'detail'=>Input::get('detail')
),array(
'name' => 'required',
'detail' => 'required'
));
if ($Validator->fails()) {
return Response::json([
'success'=>false,
'error' =>$Validator->errors->toArray()
]);
}
else{
$this->flight->name = $request->name;
$this->flight->detail = $request->detail;
$this->flight->save();
return Response::json([
'success'=>true]);
}
}
$Validator->errors()->toArray()
Errors() is function, so the braces are important

Resources