Adding URL to PRODUCT

This commit is contained in:
Bruno F. Fontes 2018-09-16 20:32:32 -03:00
parent 92f901e3a5
commit 614dbeda35
4 changed files with 37 additions and 12 deletions

View File

@ -19,9 +19,10 @@ class ProductController extends Controller
* *
* @return (view) The product view * @return (view) The product view
*/ */
public function store() public function store(Request $request)
{ {
Product::create(['name' => request('product'), 'user_id' => \Auth::id()]); //Just remember to add the fillable on Model to make this work $request->validate(['product' => 'required']);
Product::create(['name' => request('product'), 'user_id' => \Auth::id(), 'url' => request('url') ?? '']); //Just remember to add the fillable on Model to make this work
return redirect('product'); return redirect('product');
} }
@ -30,8 +31,31 @@ class ProductController extends Controller
* *
* @param (int) $id The product id * @param (int) $id The product id
*/ */
public function delete($id) public function delete(Request $request)
{ {
//TODO: Delete all items with all users for the product
$request->validate(['product' => 'required']);
$item = User::find(\Auth::id())
->items()->with('users')->find(request('item'));
$product = $item->product_id;
//Detach users from this item
foreach ($item->users as $user) {
User::findOrFail($user->id)->items()->detach($item->id);
}
//Delete item
$item->delete();
return redirect('product/' . $product);
}
public function patch(Request $request)
{
$request->validate(['item' => 'required', 'name' => 'required']);
$item = User::find(\Auth::id())->items()->find(request('item'));
$item->name = request('name');
$item->save();
return redirect('item/'.request('item'));
} }
/** /**

View File

@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class Product extends Model class Product extends Model
{ {
protected $fillable = ['user_id', 'name']; protected $fillable = ['user_id', 'name', 'url'];
public function items() public function items()
{ {
@ -17,12 +17,4 @@ class Product extends Model
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
//Ótima forma de adicionar um item por dentro do produto. Mas não estou usando por não saber como lidar com a table de UsuáriosPorItem
/*
public function addItem($name)
{
$this->items()->create(compact('name'));
}
*/
} }

View File

@ -17,6 +17,7 @@ class CreateProductsTable extends Migration
$table->increments('id'); $table->increments('id');
$table->string('name'); $table->string('name');
$table->integer('user_id'); $table->integer('user_id');
$table->string('url');
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -18,8 +18,16 @@
<hr class="m-3"> <hr class="m-3">
@endif @endif
<div class="my-4 p-2"> <div class="my-4 p-2">
@if ($item->product->url)
<a href="{{$item->product->url}}" class="link-unstyled" target="_blank" rel="noopener noreferrer">
@endif
<strong>{{$item->name}}</strong> ({{$item->product->name}}) <strong>{{$item->name}}</strong> ({{$item->product->name}})
@if ($item->product->url)
</a>
@endif
@if ($item->used_by) @if ($item->used_by)
@include('home.usedItem') @include('home.usedItem')
@else @else