Skip to content

Instantly share code, notes, and snippets.

@arvindsvt
Last active April 14, 2024 15:44
Show Gist options
  • Save arvindsvt/5cb3342b2899c7bf14be369ca26d546d to your computer and use it in GitHub Desktop.
Save arvindsvt/5cb3342b2899c7bf14be369ca26d546d to your computer and use it in GitHub Desktop.
request-only-not-work-laravel
https://stackoverflow.com/questions/38271551/why-does-if-statement-with-request-only-not-work-laravel
$request->only('name', 'age', 'cheese');
// ['name' => 'Pingu', 'age' => '22', 'cheese' => 'gorgonzola']
// Retrieve a portion of the validated input data...
$validated = $request->safe()->only(['name', 'email']);
$validated = $request->safe()->except(['name', 'email']);
add this function to our escaped blade statement:
https://stackoverflow.com/questions/78154387/laravel11-mysql-migration-problem-with-collation-general-error-1273-unknown-c
DB_COLLATION=utf8mb4_unicode_ci
{!! html_entity_decode($hello) !!}
https://stackoverflow.com/questions/29253979/displaying-html-with-blade-shows-the-html-code
$ext = pathinfo($file_path, PATHINFO_EXTENSION);
$foo = \File::extension($filename);
$extension = $request->file('file')->extension();
https://stackoverflow.com/questions/38403558/get-an-image-extension-from-an-uploaded-file-in-laravel
$fileName = pathinfo($fullFileName)['filename'];
Equivalent to:
$fileName = pathinfo($fullFileName, PATHINFO_FILENAME);
$extension = $request->file('image')->extension();
$request->image->extension();
https://stackoverflow.com/questions/31081644/how-to-redirect-back-to-form-with-input-laravel-5
<input type="text" name="name" value="{{ old('name') }}" />
return redirect()->back()->withInput();
return Redir $validation = Validator::make($request->all(),[
'name' => ['Required','alpha']
]);
if($validation->passes()){
print_r($request->name);
}
else{
//this will return the errors & to check put "dd($errors);" in your blade(view)
return back()->withErrors($validation)->withInput();
}
Rect::back()->withInput(Input::all());
Redirect::back()->withInput();
return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());
https://stackoverflow.com/questions/38461677/what-is-the-best-practice-to-show-old-value-when-editing-a-form-in-laravel
<option value="Jeff" {{ old('name', $DB->default-value) == 'Jeff' ? 'selected' : '' }}>Jeff</option>
value="{{old('title') ?? $dog->title }}"
https://www.itsolutionstuff.com/post/how-to-convert-json-to-collection-in-laravelexample.html
/* Convert JSON string to a collection */
$dataCollection = collect(json_decode($jsonString, true));
dd($dataCollection);
$array = json_decode($json, true);
// create a new collection instance from the array
collect($array);
// decode json
$response = json_decode($response, true);
//create laravel collection
$collection = collect($response);
$laravelcollection = collect($getjson)->values();
https://stackoverflow.com/questions/45351425/how-to-disable-laravel-eloquent-auto-increment
class UserVerification extends Model
{
protected $primaryKey = 'your_key_name'; // or null
public $incrementing = false;
}
$table->enum('status',['new', '111', 'disabled'])->default('111');
$table->integer('id')->unsigned(); // to remove primary key
$table->primary('id'); //to add primary key
https://stackoverflow.com/questions/36885413/how-to-display-validation-errors-next-to-each-related-input-field-in-laravel-5
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
<label for="name" class="col-sm-3 control-label">Name: </label>
<div class="col-sm-6">
<input class="form-control" required="required" name="name" type="text" id="name">
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
httpreturn redirect()->back()->with(['message' => 'A message to display']);
//medium.com/@eelcoluurtsema/easily-add-bootstrap-alerts-to-laravel-100d07ca7f4c
@if(session()->has('message'))
<div class="alert alert-info">
{{ session('message') }}
</div>
@endif
{!! display_success('The success message to display') !!}
{!! display_error('The error message to display') !!}
{!! display_info('The info message to display') !!}
https://laracasts.com/discuss/channels/laravel/how-to-get-the-validation-message-in-laravel-inside-modal
@if(count($errors)>0)
@foreach ($errors->all() as $error)
<p style="color:white" class="alert alert-danger">{{ $error }}</p>
@endforeach
@endif
https://www.positronx.io/custom-laravel-flesh-message-examples-tutorial/
https://5balloons.info/best-way-to-pass-bootstrap-alert-flash-messages-in-laravel
https://laracoding.com/using-a-model-scope-with-parameters-with-laravel-eloquent/
app/Models/Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function scopeByPriceRange($query, $minPrice, $maxPrice)
{
return $query->whereBetween('price', [$minPrice, $maxPrice]);
}
}
class ProductController extends Controller
{
public function index(Request $request)
{
// Retrieve all products
$products = Product::all();
return view('products.index', compact('products'));
}
public function filter(Request $request)
{
$minPrice = $request->input('min_price');
$maxPrice = $request->input('max_price');
// Validate minimum and maximum prices
$request->validate([
'min_price' => 'nullable|numeric|min:0',
'max_price' => 'nullable|numeric|min:' . ($minPrice ?? 0),
]);
// Build the filtered product list
$products = Product::byPriceRange($minPrice, $maxPrice)->get();
return view('products.index', compact('products'));
}
}
https://laravel.io/index.php/forum/05-08-2014-custom-model-methods
https://stackoverflow.com/questions/37692482/laravel-how-to-add-a-custom-function-in-an-eloquent-model
https://martinjoo.dev/35-eloquent-recipes
https://wpwebinfotech.com/blog/advanced-laravel-eloquent-techniques/
https://stackoverflow.com/questions/32652818/laravel-blade-check-empty-foreach
@forelse($status->replies as $reply)
<p>{{ $reply->body }}</p>
@empty
<p>No replies</p>
@endforelse
https://dekgenius.com/script-code-example/php_example_laravel-if-else-condition-in-blade-file.html
https://medium.com/@advanceidea/laravel-eloquent-tips-894bc104cb98
@unless ( empty($school->website) )
<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>
@endunless
https://stackoverflow.com/questions/27486179/laravel-4-2-blade-check-if-empty
{{ optional($A2)->propertyName }}
https://stackoverflow.com/questions/26732821/displaying-the-error-messages-in-laravel-after-being-redirected-from-controller
https://codehow2.com/laravel/how-to-submit-a-form-using-ajax-in-laravel
public function store(Request $request){
$validator = Validator::make($request->all(), ['name' => 'required', 'email' => 'required|email|unique:applications']);
if ($validator->fails()) {
return response()->json([
'errors' => $validator->errors()
]);
}
$result = Application::create($request->all());
return response()->json(['success' => 'Form submitted successfully.']);
}
$.each(response.errors, function(key, value) {
var el = $(document).find('[name="'+key + '"]');
el.addClass( "is-invalid" );
el.after($('<span style="color:red" class= "err-msg">' + value[0] + '</span>'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment